zoukankan      html  css  js  c++  java
  • UVa 10006 Carmichael Numbers

    Carmichael Numbers 

    An important topic nowadays in computer science is cryptography. Some people even think that cryptography is the only important field in computer science, and that life would not matter at all without cryptography.  Alvaro is one of such persons, and is designing a set of cryptographic procedures for cooking paella. Some of the cryptographic algorithms he is implementing make use of big prime numbers. However, checking if a big number is prime is not so easy. An exhaustive approach can require the division of the number by all the prime numbers smaller or equal than its square root. For big numbers, the amount of time and storage needed for such operations would certainly ruin the paella.

    However, some probabilistic tests exist that offer high confidence at low cost. One of them is the Fermat test.

    Let a be a random number between 2 and n - 1 (being n the number whose primality we are testing). Then, n is probably prime if the following equation holds: 

     

    egin{displaymath}a^n mod n = a
end{displaymath}

     

    If a number passes the Fermat test several times then it is prime with a high probability.

    Unfortunately, there are bad news. Some numbers that are not prime still pass the Fermat test with every number smaller than themselves. These numbers are called Carmichael numbers.

    In this problem you are asked to write a program to test if a given number is a Carmichael number. Hopefully, the teams that fulfill the task will one day be able to taste a delicious portion of encrypted paella. As a side note, we need to mention that, according to Alvaro, the main advantage of encrypted paella over conventional paella is that nobody but you knows what you are eating.

     

    Input 

    The input will consist of a series of lines, each containing a small positive number n ( 2 < n < 65000). A number n = 0 will mark the end of the input, and must not be processed.

     

    Output 

    For each number in the input, you have to print if it is a Carmichael number or not, as shown in the sample output.

     

    Sample Input 

    1729
    17
    561
    1109
    431
    0
    

     

    Sample Output 

    The number 1729 is a Carmichael number.
    17 is normal.
    The number 561 is a Carmichael number.
    1109 is normal.
    431 is normal.
    

     

    先用埃拉托斯特尼筛法打素数表,对每一个询问若不是素数,再用快速幂检验是否为Carmichael Number

     

     1 #include<iostream>
     2 
     3 using namespace std;
     4 
     5 bool isPrime[70000];
     6 long n;
     7 
     8 void initial()
     9 {
    10     fill(isPrime,isPrime+70000,true);
    11     isPrime[0]=isPrime[1]=false;
    12 
    13     for(int i=4;i<70000;i+=2)
    14         isPrime[i]=false;
    15 
    16     for(int i=3;i<70000;i+=2)
    17         if(isPrime[i])
    18             for(int j=i*2;j<70000;j+=i)
    19                 isPrime[j]=false;
    20 }
    21 
    22 long mod_pow(long x,long n,long mod)
    23 {
    24     long res=1;
    25     while(n>0)
    26     {
    27         if(n&1)
    28             res=res*x%mod;
    29         x=x*x%mod;
    30         n>>=1;
    31     }
    32     return res;
    33 }
    34 
    35 int main()
    36 {
    37 
    38     initial();
    39 
    40     while(cin>>n)
    41     {
    42         if(n==0)
    43             break;
    44         if(isPrime[n])
    45         {
    46             cout<<n<<" is normal."<<endl;
    47             continue;
    48         }
    49         else
    50         {
    51             int i;
    52             for(i=2;i<n;i++)
    53             {
    54                 if(mod_pow(i,n,n)!=i)
    55                 {
    56                     cout<<n<<" is normal."<<endl;
    57                     break;
    58                 }
    59             }
    60             if(i==n)
    61                 cout<<"The number "<<n<<" is a Carmichael number."<<endl;
    62         }
    63     }
    64 
    65 
    66     return 0;
    67 }
    [C++]
  • 相关阅读:
    进程
    并发编程小结
    操作系统发展史
    基于socketsever实现并发的socket编程
    UDP套接字
    粘包问题及解决
    socket套接字编程
    TCP协议与三次握手四次挥手
    OSI七层协议
    互联网的组成
  • 原文地址:https://www.cnblogs.com/lzj-0218/p/3235895.html
Copyright © 2011-2022 走看看