UVA 10006 Carmichael Numbers(数论+快速幂)
2017-07-17 20:20
405 查看
Description
一个非素数n,如果对于任意2<=a<=n-1的a都有a^n mod n =a,则称n是一个卡迈克尔数,给出一整数n,判断其是否是卡迈克尔数
Input
多组用例,每组用例输入一整数n,以n=0结束输入(2 < n < 65000)
Output
如果n是卡迈克尔数则输出“The number n is a Carmichael number.”,否则输出“n is normal.”
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.
Solution
首先判断n是不是素数,不是素数直接枚举a快速幂算出a^n是否等于a即可,时间复杂度O(nlogn)
Code
一个非素数n,如果对于任意2<=a<=n-1的a都有a^n mod n =a,则称n是一个卡迈克尔数,给出一整数n,判断其是否是卡迈克尔数
Input
多组用例,每组用例输入一整数n,以n=0结束输入(2 < n < 65000)
Output
如果n是卡迈克尔数则输出“The number n is a Carmichael number.”,否则输出“n is normal.”
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.
Solution
首先判断n是不是素数,不是素数直接枚举a快速幂算出a^n是否等于a即可,时间复杂度O(nlogn)
Code
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<cmath> #include<vector> #include<queue> #include<map> #include<set> #include<ctime> using namespace std; typedef long long ll; #define INF 0x3f3f3f3f #define maxn 1111 ll mod_pow(ll a,ll b,ll p) { ll ans=1ll; while(b) { if(b&1)ans=ans*a%p; a=a*a%p; b>>=1; } return ans; } bool check(int n) { for(int i=2;i*i<=n;i++) if(n%i==0)return 0; return 1; } int main() { int n; while(scanf("%d",&n),n) { int flag=1; if(check(n))flag=0; else { for(int i=2;i<n;i++) if(mod_pow(i,n,n)!=i) { flag=0; 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(数论+快速幂)
- UVA10006 Carmichael Numbers(数论)
- Uva10375 Choose and divide(数论、唯一分解定理、快速幂)
- UVA 11609 - Teams(数论+推理+快速幂)
- UVA 10006 - Carmichael Numbers 数论(快速幂取模 + 筛法求素数)
- Uva 10006 Carmichael Numbers(数论、快速幂、素数筛法)
- UVa 10006 Carmichael Numbers(数论&卡米歇尔数&考塞特判别法)
- UVA 10006 - Carmichael Numbers 数论(快速幂取模 + 筛法求素数)
- UVA10006 - Carmichael Numbers(筛选构造素数表+快速幂)
- uva 10006 数论入门题
- 快速幂运算(数论知识)
- UVA 10627 - Infinite Race(数论)
- 数论刷题-uva【10533】 - Digit Primes
- ACM 数论 UVALive 6170 Esspe-Peasee 解二元一次方程 扩张欧几里得算法
- UVa 11889 Benefit(数论)
- uva 10555 - Dead Fraction)(数论)
- 2017年第0届浙江工业大学之江学院程序设计竞赛决赛 Problem D: qwb与神奇的序列(数论+快速幂)
- UVA 10127 Ones (数论) 计算至少要多少位十进制的1能够被n整除
- UVA 11752 - The Super Powers (数论)
- uva 12716 GCD XOR (数论)枚举技巧(筛法) + 思维