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 |
Re:复杂度卡得有点过分了,不到1e6的复杂度过不了In Reply To:复杂度卡得有点过分了,不到1e6的复杂度过不了 Posted by:ALLACS at 2017-10-06 12:32:44 //尺取法 #include <iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn=2000; const int maxn2=10000+5; bool p[maxn2]; int prime[maxn]; int sum[maxn]; int cnt=0; //素数个数 //素数筛 void is_prime() { memset(p,true,sizeof(p)); memset(prime,0,sizeof(prime)); p[0]=p[1]=false; for(int i=2;i<=maxn2;i++) { if(p[i]) { prime[cnt++]=i; for(int j=2*i;j<=maxn2;j+=i) p[j]=false; } } } //求sum数组 void quick_sum() //得到sum数组,sum数组的大小为1230 { memset(sum,0,sizeof(sum)); for(int i=0;i<cnt;i++) sum[i+1]=sum[i]+prime[i]; } int main() { is_prime(); quick_sum(); int n; while(scanf("%d",&n)==1&&n) { int t=0; for(int e=0;sum[e]+n<=sum[cnt];e++) { int k=lower_bound(sum+e,sum+cnt+1,sum[e]+n)-sum; if(sum[k]==sum[e]+n) t++; } cout<<t<<endl; } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator