时间复杂度O(n)

int tab[N],num=0;
bool notzhi[N]={1,1};
for(int i=2;i<=N;++i){
	if (notzhi[i]==0)
		tab[num++]=i;
	for(int j=0;j<num&&i*tab[j]<=N;++j){
		notzhi[i*tab[j]]=1;
		if (i%tab[j]==0)
			break;
	}
}

这样子能保证每个合数一定被最小质因子给筛掉,而且仅被筛一次,从而保证复杂度为O(n)

相关文章:

  • 2021-12-13
  • 2022-12-23
  • 2022-12-23
  • 2021-05-27
  • 2022-01-25
  • 2022-12-23
  • 2022-12-23
  • 2021-10-09
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-03-05
  • 2021-07-25
  • 2021-08-19
相关资源
相似解决方案