discretely @ 2024-07-17 21:46:25
循环i为啥用int也不行,不久循环到1e8吗?
#include<iostream>
using namespace std;
const int N=1e8+10;
bool vis[N];
int cnt=0;
int prim[N];
void init(int n){
for(int i=2;i<=n;++i){//问的是这里
if(!vis[i]){
prim[++cnt]=i;
for(long long j=i*i;j<=n;j+=i) vis[j]=1;
}
}
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n,q;cin>>n>>q;
init(n);
for(int i=1;i<=q;++i){
int x;cin>>x;
cout<<prim[x]<<endl;
}
//cout<<cnt<<endl;
return 0;
}
by Mugino_Shizuri @ 2024-07-17 21:48:21
@discretely i*i 很大
by Yuzilihhh @ 2024-07-17 21:48:29
for(long long j=i*i;j<=n;j+=i) vis[j]=1;
这里i*i会炸
by discretely @ 2024-07-17 21:48:54
@Mugino_Shizuri 我i*i那里不是j嘛,j开了long long啊
by Mugino_Shizuri @ 2024-07-17 21:49:03
改成 for(long long j=1ll*i*i;j<=n;j+=i) vis[j]=1;
行吗?
by Grammar__hbw @ 2024-07-17 21:49:23
@discretely int*int就已经爆了
by Mugino_Shizuri @ 2024-07-17 21:49:40
你是 i*i 先算出来再赋值给 j 的。
by discretely @ 2024-07-17 21:50:01
懂了各位,感谢