题目大意:给出一个等差数列,问这个等差数列的第n个素数是什么。
思路:这题主要考如何筛素数,线性筛。详见代码。
CODE:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 1000010
using namespace std;
int prime[MAX],primes;
bool notp[MAX];
int a,d,n;
void Pretreatment()
{
notp[1] = true;
for(int i = 2; i < MAX; ++i) {
if(!notp[i])
prime[++primes] = i;
for(int j = 1; j <= primes && i * prime[j] < MAX; ++j) {
notp[i * prime[j]] = true;
if(i * prime[j] == 0)
break;
}
}
}
int main()
{
Pretreatment();
while(scanf("%d%d%d",&a,&d,&n),a + d + n) {
for(int now = a;; now += d) {
if(!notp[now]) --n;
if(!n) {
printf("%d\n",now);
break;
}
}
}
return 0;
}