zoukankan      html  css  js  c++  java
  • Dirichlet's Theorem on Arithmetic Progressions C语言 Aizu 1141

    Problem A:

    Good evening, contestants.

    If a and d are relatively prime positive integers, the arithmetic sequence beginning with a and increasing by d, i.e., a, a + d, a + 2d, a + 3d, a + 4d, ..., contains infinitely many prime numbers. This fact is known as Dirichlet's Theorem on Arithmetic Progressions, which had been conjectured by Johann Carl Friedrich Gauss (1777 - 1855) and was proved by Johann Peter Gustav Lejeune Dirichlet (1805 - 1859) in 1837.

    For example, the arithmetic sequence beginning with 2 and increasing by 3, i.e.,

    2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 41, 44, 47, 50, 53, 56, 59, 62, 65, 68, 71, 74, 77, 80, 83, 86, 89, 92, 95, 98, ... ,

    contains infinitely many prime numbers

    2, 5, 11, 17, 23, 29, 41, 47, 53, 59, 71, 83, 89, ... .

    Your mission, should you decide to accept it, is to write a program to find the nth prime number in this arithmetic sequence for given positive integers a, d, and n.

    As always, should you or any of your team be tired or confused, the secretary disavow any knowledge of your actions. This judge system will self-terminate in three hours. Good luck!

    Input

    The input is a sequence of datasets. A dataset is a line containing three positive integers a, d, and n separated by a space. a and d are relatively prime. You may assume a <= 9307, d <= 346, and n <= 210.

    The end of the input is indicated by a line containing three zeros separated by a space. It is not a dataset.

    Output

    The output should be composed of as many lines as the number of the input datasets. Each line should contain a single integer and should never contain extra characters.

    The output integer corresponding to a dataset a, d, n should be the nth prime number among those contained in the arithmetic sequence beginning with a and increasing by d.

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

    Sample Input

    367 186 151
    179 10 203
    271 37 39
    103 230 1
    27 104 185
    253 50 85
    1 1 1
    9075 337 210
    307 24 79
    331 221 177
    259 170 40
    269 58 102
    0 0 0
    

    Output for the Sample Input

    92809
    6709
    12037
    103
    93523
    14503
    2
    899429
    5107
    412717
    22699
    25673
    

    在POJ上我使用stdbool.h无法通过编译。

    这道题的难点判断素数。我使用的离线计算的方法,使用一个1000000的布尔数组来存储1000000以内哪些是素数(1000000肯定不是),然后对于每个需要判断的数如57,只需比较a[57]是否为1,若为1则是,否则不是。

    在判断素数时,我除了使用了一个bool数组a[],还使用了一个类型为int的数组b[],首先将a[]中所有元素置1,然后从a[2]开始遍历,若a[i]==1,将i存到数组b中,并从b[0]开始遍历 ,         直到遍历完b中所有数或b[j]*i<=1000000为止,每次循环将a[b[j]*i]置0,这样就排除了所有素数的倍数,剩下的就是素数。

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 //POJ上不能使用stdbool.h 
     5 #include <stdbool.h>
     6 #define MAX 1000001
     7 bool isprime[MAX];
     8 int p[MAX];
     9 
    10 void prime(){
    11     int i,j,np=0;
    12     memset(isprime,0,sizeof(isprime));
    13     memset(p,0,sizeof(p));
    14     for(i=2;i<=MAX;i++){
    15         if(!isprime[i]) p[np++]=i;
    16         for(j=0;j<np&&p[j]*i<=MAX;j++){
    17             isprime[p[j]*i]=1;
    18             if(i%p[j]==0) break;
    19         }
    20     }
    21     isprime[1]=1;
    22     isprime[0]=1;
    23 }
    24 
    25 int main(void){
    26     int a,d,n;
    27     int i;//用于循环
    28     int j,dq;//当前是第几个素数和当前的数的值
    29     prime();
    30     while(scanf("%d %d %d",&a,&d,&n)==3){
    31         if(a==0&&d==0&&n==0) break;
    32         j=0;
    33         for(i=0;;i++){
    34             dq=a+i*d;
    35             if(isprime[dq]==0){
    36                 j++;
    37                 if(j==n){
    38                     printf("%d
    ",dq);
    39                     break;
    40                 }
    41             }
    42         }
    43     }
    44     return 0;
    45 }
    View Code
  • 相关阅读:
    ES6的reduce( )方法 可以数组求和、数组去重、二维数组转一维数组、计算数组中每个元素出现的次数
    防止/禁止页面后退
    CSS预处理器SASS将迁移到Dart Sass
    FormData 对象的使用
    javascript中编码与解码的decodeURI()、decodeURIComponent()区别
    Chrome谷歌浏览器调试技巧小结
    javaweb:tomcat&servlet
    java:Druil连接池
    java:c3p0连接池的使用
    java:JDBC的使用方式
  • 原文地址:https://www.cnblogs.com/20174317zhuyuan/p/9389347.html
Copyright © 2011-2022 走看看