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 |
32ms水过(筛素数法)典型空间换时间的算法 #include <iostream> #include <memory.h> using namespace std; const int max = 1000005; bool isprime[::max]; int main(){ int i,j; memset(isprime,true,sizeof(isprime)); for(i = 3 ; i <= 1000 ; i += 2 ){ for(j = 3 ; j <= ::max / i ; j += 2){ if(isprime[i]){ isprime[i * j] = false; } } } for(i = 4 ; i <= ::max ; i += 2 ){ isprime[i] = false; } isprime[1] = isprime[0] = false; int a,d,n; while(cin >> a >> d >> n && !(!a && !d && !n)){ int num[250] = {0}; int j = 1; for(int i = a ; j <= n ; i += d){ if(isprime[i]){ num[j++] = i; } } cout << num[n] << 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