UVA 10006 - Carmichael Numbers(快速幂取模)
2013-10-19 19:41
316 查看
Carmichael Numbers |
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:
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数(非素数,且满足之间所有数都满足a^n mod n == a)
思路:先用筛选法打出素数表。然后在用快速幂取模的方法判断条件是否成立
代码:
#include <stdio.h> #include <string.h> #include <math.h> long long n; int vis[65005]; void prime() { int s = sqrt(65000 + 0.5); for (int i = 2; i <= s; i ++) { if (!vis[i]) { for (int j = i * i; j <= 65000; j += i) vis[j] = 1; } } } long long pow_mod(long long a, long long n, long long num) { if (n == 1) return a; long long x = pow_mod(a, n / 2, num); long long ans = x * x % num; if (n % 2 == 1) ans = (ans * a) % num; return ans; } int judge() { for (long long i = 2; i < n; i ++) { if (pow_mod(i, n, n) != i) return 0; } return 1; } int main() { prime(); while (~scanf("%lld", &n) && n) { if (vis && judge()) { printf("The number %lld is a Carmichael number.\n", n); } else printf("%lld is normal.\n", n); } return 0; }
相关文章推荐
- Uva 10006 Carmichael Numbers 快速幂
- UVA - 10006 Carmichael Numbers - 快速幂
- 快速幂取模 例题uva10006
- Uva 10006 - Carmichael Numbers【素数+快速幂取模】
- UVA 10006 - Carmichael Numbers (快速幂+筛素数)
- uva 10006 快速幂
- UVa 10006 快速幂运算
- UVA 10006 - Carmichael Numbers 数论(快速幂取模 + 筛法求素数)
- UVA 10006 Carmichael Numbers (暴力+快速幂取模)
- Uva10006【快速幂】java题解
- UVA 10006 - Carmichael Numbers 数论(快速幂取模 + 筛法求素数)
- uva 10006 Carmichael Numbers(快速幂)
- UVA10006-快速幂Carmichael Numbers
- uva 10006 Carmichael Numbers(快速幂+素数)
- UVa 10006 Carmichael Numbers(快速幂取模)
- UVA10006->素数筛&&快速幂取模
- 快速幂--uva10006
- Uva 10006 Carmichael Numbers(数论、快速幂、素数筛法)
- UVa 10006 Carmichael Numbers 【快速幂+素数打表(埃氏筛法)】
- UVA 10006 - Carmichael Numbers(快速幂取模)