您的位置:首页 > 其它

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

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: