zoukankan      html  css  js  c++  java
  • Dirichlet's Theorem on Arithmetic Progressions POJ

      题意 给出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
    ",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
    ",ans);
    42 
    43     }
    44     return 0;
    45 }
  • 相关阅读:
    MATLAB01
    Diffie-Hellman 密钥交换
    古典密码
    正则表达式
    装饰器初析
    进制转换的栈实现
    Log4j(异常日志)
    2018/6/6
    2018.1.1T19B3-u4
    2018.1.1T19-B3-U3jiangyi
  • 原文地址:https://www.cnblogs.com/ttttttttrx/p/10279617.html
Copyright © 2011-2022 走看看