UVA 10006 Carmichael Numbers(数论+快速幂)

52 篇文章 0 订阅
0 篇文章 0 订阅

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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值