Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
同样是对拍了上百组数据没有问题,交上去wa,discuss里的数据都没有问题#include<iostream> #include<cstring> #include<vector> #include<cstdio> #include<cmath> #include<fstream> #define int long long #define MAX 1010000 using namespace std; int relate[MAX],siz; bool isprime[MAX]; vector<int> prime; void init(){ memset(relate,0,sizeof(relate)); memset(isprime,true,sizeof(isprime)); prime.clear(); siz=0; } void filter(int maxa){ init(); for(int i=2;i<=maxa;i++){ if(isprime[i])prime.push_back(i),siz++; for(int j=0;j<siz&&i*prime[j]<=maxa;j++){ isprime[i*prime[j]]=false; if(relate[i*prime[j]]==0)relate[i*prime[j]]=prime[j]; if(i%prime[j]==0)break; } } } int tf[MAX]; signed main(){ int l,u; while(cin>>l>>u){ for(int i=l;i<=u;i++)tf[i-l]=true; if(l==1)tf[0]=false; int k=0; while(k*k<u)k++; filter(k); for(int i=0;i<siz;i++){ int k=prime[i]; for(int j=max(((l-1)/k+1)*k,2*k);j<=u;j+=k){ tf[j-l]=false; } } int maxx=0,maxa,maxb,minn=MAX,mina,minb; int last=-1; for(int i=0;i<u-l+1;i++){ if(tf[i]){ if(last==-1){ last=i;continue; } if(maxx<i-last+1){ maxx=i-last+1; maxa=last+l,maxb=i+l; } if(minn>i-last+1){ minn=i-last+1; mina=last+l,minb=i+l; } last=i; } } if(maxx==0)cout<<"There are no adjacent primes.\n"; else printf("%d,%d are closest, %d,%d are most distant.\n",mina,minb,maxa,maxb); } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator