POJ 3006 Dirichlet's Theorem on Arithmetic Progressions 素数 难度:0

http://poj.org/problem?id=3006

#include <cstdio>
using namespace std;
bool pm[1000002];
bool usd[1000002];
bool judge(int x)
{
    if(usd[x])return pm[x];
    usd[x] = true;
    if(x == 2) return pm[x] = true;
    if(((x & 1) == 0) || (x < 2))return pm[x] = false;
    for(int i = 3;i * i <= x;i+= 2)
    {
        if(x % i == 0)return pm[x] = false;
    }
    return pm[x] = true;
}

int main(){
    int a,d,n;
    while(scanf("%d%d%d",&a,&d,&n) == 3 && a)
    {
        int cnt = 0;
        for(int tn = 0;tn < n;cnt++)
        {
            if(judge(cnt * d + a))tn++;
            if(tn == n)break;
        }
        printf("%d\n",cnt * d + a);
    }

    return 0;
}

 

posted @ 2015-07-22 22:41  雪溯  阅读(120)  评论(0编辑  收藏  举报