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;
}
上一篇:Dirichlet's Theorem on Arithmetic Progressions 分类: POJ 2015-06-12 21:07 7人阅读 评论(0) 收藏


下一篇:Odoo(OpenERP)配置文件openerp-server.conf详解