题目:Prime Generator
思路:分段筛素数
#include <cstdio> #include <iostream> #include <cmath> #include <algorithm> #include <cstring> #include <map> using namespace std; #define maxn 40000 int n_prime=0; bool vis[maxn]; int prime[4210]; bool cnt[100010]; void Prime() {memset(vis,true,sizeof(vis));vis[0]=vis[1]=0;for(int i=2;i<maxn;i++)if(vis[i]){prime[++n_prime]=i;for(int j=2*i;j<maxn;j+=i)vis[j]=0;}//cout<<n_prime<<":"<<prime[n_prime]<<endl; } int main() {Prime();int t;scanf("%d",&t);int tag=0;while(t--){if(tag)printf("\n");elsetag=1;long long l,r;scanf("%lld%lld",&l,&r);memset(cnt,true,sizeof(cnt));for(int i=1;i<=n_prime;i++){int tmp=l/prime[i];while(tmp<2||tmp*prime[i]<l)tmp++;for(int j=tmp*prime[i];j<=r;j+=prime[i]){if(j>=l&&j<=r)cnt[j-l]=0;}}if(l==1)cnt[0]=0;for(int i=0;i+l<=r;i++)if(cnt[i])cout<<i+l<<endl;}return 0; }