码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-07-23 00:25:43      阅读:120      评论:0      收藏: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;
}

 

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

标签:

原文地址:http://www.cnblogs.com/xuesu/p/4668916.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!