标签:printf lag output with crypto spoj ret 个数 peter
求给定的两个数之间的素数
Translated by @kaiming
Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers!
输入格式:
The input begins with the number t of test cases in a single line (t<=10). In each of the next t lines there are two numbers m and n (1 <= m <= n <= 1000000000, n-m<=100000) separated by a space.
输出格式:
For every test case print all prime numbers p such that m <= p <= n, one number per line, test cases separated by an empty line.
After cluster change, please consider PRINT as a more challenging problem.
//Pro: Spoj PRIME1 - Prime Generator //求给定的两个数之间的素数 #include<iostream> #include<cmath> #include<cstdio> #include<algorithm> using namespace std; const long long N=40000; long long T; long long cnt; long long prime[N+1]; bool flag[N+1]; void init() { flag[1]=1; long long tmp; for(long long i=2;i<=N;++i) { if(!flag[i]) prime[++cnt]=i; for(long long j=1;j<=cnt&&(tmp=i*prime[j])<=N;++j) { flag[tmp]=1; if(i%prime[j]==0) break; } } } long long l,r; bool Flag; int main() { init(); scanf("%d",&T); while(T--) { scanf("%d%d",&l,&r); for(;l<=r;++l) { if(l<=N) { if(!flag[l]) { printf("%d\n",l); continue; } } else { Flag=0; for(long long j=1;prime[j]<=sqrt(l)&& !Flag && j<=cnt;++j) { if(l%prime[j]==0) Flag=1; } if(!Flag) printf("%d\n",l); } } puts(""); } return 0; }
标签:printf lag output with crypto spoj ret 个数 peter
原文地址:https://www.cnblogs.com/lovewhy/p/9246205.html