首页 > 代码库 > POJ3006

POJ3006

 1 #include <iostream> 2 #include <math.h> 3 using namespace std; 4  5 int main() 6 { 7     int N = 1000000; 8     bool prime[N+1]; 9     for (int i = 0; i < N; i++)10     {11         if (i%2 == 0 && i != 2)12             prime[i] = false;13         else14             prime[i] = true;15     }16     prime[1] = false;17     for (int i = 3; i <= sqrt(N); i += 2)18     {19         if (prime[i])20             for (int j = i+i; j <= N; j += i)21                 prime[j] = false;22     }23 24     int a, d, n;25     int seq;26     int nth;27     while (cin >> a >> d >> n)28     {29         seq = a;30         nth = 1;31         if (a == 0 && d == 0 && n == 0)32             return 0;33 34         while (1)35         {36             if (prime[seq] && nth == n)37                 break;38             if (prime[seq])39                 nth++;40             seq += d;41         }42         43         cout << seq << endl;44     }45 46     return 0;47 }

 

POJ3006