POJ 3006 Dirichlet's Theorem on Arithmetic Progressions(素数筛的简单应用)

来源:互联网 发布:mac美国官网价格 编辑:程序博客网 时间:2024/06/06 00:12
题目大意:给一个等差数列,让你在这个数列中找出第k个素数。
由于数据很小,并有多组输入,所以直接素数筛打表然后即可,暴力也可以过。
#include<stdio.h>#include<math.h>#define maxn 1000000int is_prime[maxn]={0};int main(){    int i,j,k,a,d,n,ans;    is_prime[1]=1;    is_prime[0]=1;    //每一个合数a,其必定存在小于等于根号a的质因子    for(i=2;i<=1000;i++)//预处理        if(!is_prime[i])            for(j=i+i;j<=maxn;j+=i)                is_prime[j]=1;    while(scanf("%d%d%d",&a,&d,&n)&&!(!a&&!d&&!n))    {        int count=0;        for(i=a;;i+=d)        {            if(!is_prime[i])                count++;            if(count==n)            {                printf("%d\n",i);                break;            }        }    }    return 0;}

原创粉丝点击