UVA

来源:互联网 发布:工业互联网 阿里云 编辑:程序博客网 时间:2024/06/10 21:27

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:
an 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满足
x^n % n = x    (1<x<n)
那么这个数是 Carmichael number.
给出一个n 问是不是Carmichael number.


解题思路
首先必须是合数, 然后满足上述式子就可以, 然后就是快速幂了。
快速幂借助二进制的思想, 将指数 用2进制表示, 例如
X^22 = X^16 * X^4 * X^2
22 的二进制 = 10110
从最低位开始,如果当前位是1 就乘以 x的 当前位的权值次方,否则不用乘,将样例带入代码加深理解

代码
#include <iostream>#include <cstdio>#include <map>#include <cmath>#include <string.h>#include <algorithm>#include <set>#include <sstream>#include <vector>#include <queue>#include <stack>using namespace std;const int maxn = 1000+10;const int INF = 0x3f3f3f3f;long long mod_power(long long x,long long n,long long mod){///x^n % mod    long long res = 1;    while(n>0){        if(n&1) res = res * x % mod;///当前这第y位如果是1  说明要乘以x^y        ///下一位        x = x * x % mod;        n >>= 1;    }    return res;}///例如 X^22 = X^16 * X^4 * X^2/// 22 的二进制 = 10110int main(){    int n;    while( scanf("%d",&n) && n != 0){        bool f  = 0;        for(int i = 2; i <= sqrt(n); ++i){            if(n%i == 0) {f = 1;break;}///和数        }        if(!f) {printf("%d is normal.\n",n); continue;}        f = 1;        for(int x = 2; x <= n-1; ++x){            if(x != mod_power(x,n,n)){                f = 0;                break;            }        }        if(f) printf("The number %d is a Carmichael number.\n",n);        else printf("%d is normal.\n",n);    }    return 0;}


1 0
原创粉丝点击