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 }
  • 相关阅读:
    【并查集学习笔记】------迟来的总结
    【Ant Trip】题解
    【数星星 Stars】题解
    【From Hero to Zero】题解
    营救 【题解】
    js获取浏览器视窗尺寸
    js基础拖拽二
    js基础拖拽一
    js检测浏览器flash支持
    js操作cookie
  • 原文地址:https://www.cnblogs.com/ttttttttrx/p/10279617.html
Copyright © 2011-2022 走看看