// 最基本求一个素数(on),(osqrt(n)) #include<bits/stdc++.h> using namespace std; int main() { int n; cin>>n; for(int i=2;i<n;i++)//o(n) if(n%i==0){ cout<<"no"; return 0; } for(int i=2;i<=n/i;i++)//o(sqrt(n)) if(n%i==0){ cout<<"no"; return 0; } cout<<"yes"; return 0; }
//埃氏筛,一次性求n之前所有素数(on) #include<bits/stdc++.h> using namespace std; const int N=1e5+10; int n,f[N],a[N],res,maxx=-1; map<int,int>mp; bool vis[N]; int main() { cin>>n; for(int i=0;i<n;i++) cin>>a[i],maxx=max(maxx,a[i]); for(int i=2;i<=maxx;i++){ if(!vis[i]){ mp[i]=1; for(int j=i;j<=maxx;j+=i) vis[j]=true; } } for(int i=0;i<=n;i++) if(mp.count(a[i])) cout<<a[i]<<" "; return 0; }
//欧拉筛(线性筛)(时间复杂度最快):https://www.luogu.com.cn/problem/P3383 //https://www.bilibili.com/video/BV1hR4y1u7e1/?spm_id_from=333.337.search-card.all.click&vd_source=b44efb267aea765a23ee5e2d900d1d5a #include<bits/stdc++.h> using namespace std; const int N=1e8+10; int n,res,k,prime[N],num; bool vis[N]; int main() { scanf("%d%d",&n,&k); for(int i=2;i<=n;i++){ if(!vis[i]) prime[++res]=i; for(int j=1;prime[j]<=n/i;j++){ vis[prime[j]*i]=true; if(!(i%prime[j])) break; } } while(k--){ scanf("%d",&num); printf("%d\n",prime[num]); } return 0; }
标签:maxx,埃氏,int,namespace,素数,main,根号 From: https://www.cnblogs.com/o-Sakurajimamai-o/p/17477376.html