poj 3006 Dirichlet's Theorem on Arithmetic Progressions

http://162.105.81.212/JudgeOnline/problem?id=3006

 

又是一个跟素数有关的题目-_-,

这两天贴那个素数打表模板都不知道贴几次了,囧

 

题目中最后一句话很有用:

FYI, it is known that the result is always less than 106 (one million) under this input condition.

所以先打表(1-10^6),

然后再逐个判断就OK了

 

#include<iostream> using namespace std; const int MAX=1000000; bool isprime[MAX+1]; int prime[MAX]; int pnum; void getprime() { int i,j; memset(isprime,0,sizeof(isprime)); pnum=0; for(i=2;i<=MAX;i++) { if(!isprime[i]) prime[pnum++]=i; for(j=0;j<pnum&&prime[j]*i<=MAX;j++) { isprime[prime[j]*i]=1; if(i%prime[j]==0) break; } } isprime[0]=1;isprime[1]=1; } int main() { int a,d,n,i,j; getprime(); while(scanf("%d%d%d",&a,&d,&n)&&(a||d||n)) { i=j=0; while(prime[i]<a && i<pnum) i++; while(j<n) { if((prime[i]-a)%d==0) j++; i++; } printf("%d/n",prime[i-1]); } return 0; }

你可能感兴趣的:(poj 3006 Dirichlet's Theorem on Arithmetic Progressions)