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

Dirichlet's Theorem on Arithmetic Progressions POJ - 3006 线性欧拉筛

时间:2019-01-16 22:48:49      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:col   turn   code   rem   cstring   数列   mem   set   names   

 

  题意 给出a d n    给出数列 a,a+d,a+2d,a+3d......a+kd 问第n个数是几 保证答案不溢出

  直接线性筛模拟即可

 

 

 1 #include<cstdio>
 2 #include<cstring>
 3 using namespace std;
 4 bool Is_Primes[1000005];
 5 int Primes[1000005];
 6 int A[1000005];
 7 int cnt;
 8 void Prime(int n){
 9     cnt=0;
10     memset(Is_Primes,0,sizeof(Is_Primes));
11     for(int i=2;i<=n;i++){
12         if(!Is_Primes[i])
13             Primes[cnt++]=i;
14         for(int j=0;j<cnt&&i*Primes[j]<n;j++){
15             Is_Primes[i*Primes[j]]=1;
16             if(i%Primes[j]==0)break;
17         }
18     }
19     
20     memset(Is_Primes,0,sizeof(Is_Primes));
21     for(int i=0;i<cnt;i++){
22         Is_Primes[Primes[i]]=1;
23     }
24 
25 }
26 int main(){
27     int a,b,n;
28     Prime(1000003);
29     while(scanf("%d%d%d",&a,&b,&n)==3&&a+b+n){
30         
31         int ans=0;
32         for(int i=0;i<1000003;i++){
33             A[i]=i*b +a;
34         //    printf("%d %d\n",A[i],i);
35             if(Is_Primes[A[i]])ans++;
36             if(ans==n){
37                 ans=A[i];
38                 break;
39             }
40         }
41         printf("%d\n",ans);
42 
43     }
44     return 0;
45 }

 

Dirichlet's Theorem on Arithmetic Progressions POJ - 3006 线性欧拉筛

标签:col   turn   code   rem   cstring   数列   mem   set   names   

原文地址:https://www.cnblogs.com/ttttttttrx/p/10279617.html

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