UVA-10006 Carmichael Numbers

来源:互联网 发布:淘宝联盟定金 编辑:程序博客网 时间:2024/06/05 09:35

题目传送门
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:
a^n mod n = a

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.

题目的意思就是给出n ,看它是否满足下面两个条件
1. n是合数 .
2.对于区间 [ 2 , n ) 的任意数a ,都使得 pow (a , n) % n == a成立;
首先判断n是不是合数,我使用素数筛进行一次性记录判断。
然后用快速幂遍历所有小于n的a(2<=a

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef long long ll;const int maxn=65000+10;bool isprime[maxn];int prime[maxn];int tot,mod;ll mypow(ll a,ll n){    ll res=(ll)1;    while(n)    {        if(n&1)        res=res*a%mod;        a=a*a%mod;        n>>=1;    }    return res;}void prime_match(){    tot=0;    fill(isprime,isprime+maxn,true);    isprime[0]=isprime[1]=false;    for(int i=2;i<maxn;i++)    {        if(isprime[i])        {            prime[tot++]=i;        }        for(int j=0;j<tot&&i*prime[j]<maxn;j++)        {            isprime[i*prime[j]]=false;            if(!(i%prime[j]))            break;        }    }}int main(){    int n,i;    prime_match();    while(scanf("%d",&n)==1&&n)    {        if(isprime[n])        {            printf("%d is normal.\n",n);            continue;        }        else        {            mod=n;            for(i=2;i<n;i++)            {                int cnt=mypow(i,n);                if(cnt!=i)                break;            }            if(i>=n)            {                printf("The number %d is a Carmichael number.\n",n);            }            else            {                printf("%d is normal.\n",n);            }        }    }    return 0;} 
原创粉丝点击