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:32ms水过(筛素数法)In Reply To:32ms水过(筛素数法) Posted by:dinysirius at 2009-03-03 13:47:50 //又小优化一下,16ms #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 ){ if(isprime[i]){ for(j = 3 ; j <= ::max / i ; j += 2){ 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