Uva 10006 - Carmichael Numbers【素数+快速幂取模】
2016-09-06 17:35
537 查看
Carmichael Numbers |
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:
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 inthe 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,问你能否保证对于任意的a (2<a<n)使得 a^n%n==a
满足的话就是Carmichael number。
素数筛选+快速幂取模就可以了。
注意:虽然n在int范围内,但是进行幂运算以后就有可能超过int范围,所以要用long long。
AC代码:
#include <bits/stdc++.h> using namespace std; const int maxn=65000+5; bool is[maxn]; typedef long long LL; void getPrime() { memset(is,true,sizeof(is)); for(int i=2; i*2<=maxn; i++) { if(is[i]) { for(int j=i+i; j<maxn; j+=i) is[j]=false; } } } LL quick_mod(int a,int b,int mod) { LL ans=1; LL t=a; while(b) { if(b&1) ans=ans*t%mod; t=t*t%mod; b>>=1; } return ans; } /** LL quick_mod(LL a,LL n,LL m) //递归求快速幂 { if(n==0) return 1; LL x=quick_mod(a,n/2,m); LL ans=x*x%m; if(n%2==1) ans=ans*a%m; return ans; } */ int main() { int n; getPrime(); ios::sync_with_stdio(false); //freopen("data/10006.txt","r",stdin); //freopen("data/10006out.txt","w",stdout); while(cin>>n,n) { if(is ) { printf("%d is normal.\n",n); continue; } bool flag=true; for(int i=2; i<n; i++) { if(quick_mod(i,n,n)!=i) { flag=false; break; } } if(flag) printf("The number %d is a Carmichael number.\n",n); else printf("%d is normal.\n",n); } return 0; }
相关文章推荐
- UVA 10006 - Carmichael Numbers (快速幂+筛素数)
- uva 10006 Carmichael Numbers(快速幂+素数)
- UVA10006->素数筛&&快速幂取模
- UVa 10006 Carmichael Numbers 【快速幂+素数打表(埃氏筛法)】
- 【素数 && 快速幂】UVA - 10006 Carmichael Numbers
- uva10006 Carmichael Numbers(卡迈克尔数+素数打表)
- UVA 10006(卡迈克尔数+素数打表)
- UVA 10006 - Carmichael Numbers 数论(快速幂取模 + 筛法求素数)
- UVA 10006 Carmichael Numbers (伪素数)
- UVA10006-快速幂Carmichael Numbers
- Uva 10006-Carmichael Numbers(快速幂)
- UVA 10006(素数判断+快速幂)
- UVA 10006 Carmichael Numbers(快速幂取模)
- Uva 10006 Carmichael Numbers 快速幂
- UVA 10006 Carimichael Numbers(快速幂)
- UVA 10006 - Carmichael Numbers(快速幂取模)
- UVA10006 快速幂
- uva 10006 快速幂
- Carmichael Numbers - UVa 10006 素数判断
- uva 10006 Carmichael Numbers(快速幂)