萌新求助(一个让人迷惑的玄学问题)
  • 板块灌水区
  • 楼主Catalan_
  • 当前回复4
  • 已保存回复4
  • 发布时间2020/8/23 11:28
  • 上次更新2023/11/6 19:37:02
查看原帖
萌新求助(一个让人迷惑的玄学问题)
194192
Catalan_楼主2020/8/23 11:28

P3383 【模板】线性筛素数

用solve函数完成线性筛,但在最后输出答案时 用for会T3个点,而用while就能A??

而且感觉用ios::sync_with_stdio(false);优化过后, cin,cout比scanf,printf要慢?

萌新迷惑QWQ(下面是代码

#include<bits/stdc++.h>
#define M 100000100
using namespace std;
bool zs[M];
int tot,pri[M/10];
void solve(int n)
{
	for(int i=2;i<=n;++i)
	{
		if(!zs[i]) pri[++tot]=i;
		for(int j=1;j<=tot && i*pri[j]<=n;++j)
		{
			zs[ i*pri[j] ]=true;
			if( i% pri[j] ==0 )break;
		}
		
	}
}
int main()
{
	ios::sync_with_stdio(false);
	int n,m;cin>>n;
	solve(n);
	cin>>m;
	while(m--)//原本写的是for(int i=1;i<=m;++i),T了3个点 
	{
		int a;cin>>a;
		cout<<pri[a]<<endl;
	}
} 

)

2020/8/23 11:28
加载中...