Uva10006——Carmichael Numbers

这里写图片描述
这里写图片描述

题意:我们把对任意的1<x<n都有x^n≡x(mod n)成立的合数(注意一定要是合数)n称为Carmichael Number.对于给定的整数n,判断它是不是Carmichael Number

快速幂+素数判断(也可以素数打表)

#include<iostream>  
#include<algorithm>  
#include<cmath>
using namespace std;
#define mod 1000000007

typedef long long ll;
ll n;
ll Pow(ll a, ll b)
{
    ll ans=1, base=a;
    while (b != 0)
    {
        if (b & 1)
        {
            ans = ans * base % n;
        }
        base = base * base % n;
        b >>= 1;
    }
    return ans;
}

bool isPrime(ll n)
{
    ll i;
    for (i = 2; i < sqrt(n); i++)
    {
        if (n%i == 0)
            return false;
    }
    if (i == sqrt(n) - 1)
        return true;
}

int main()
{
    ll x;
    while (cin >> n&&n!=0)
    {
        if (isPrime(n))
        {
            cout << n << " is normal." << endl;
            continue;
        }
        for (x = 1; x < n; x++)
        {
            ll p, q;
            p = Pow(x, n) % n;
            q = x % n;
            if (p != q)
            {
                cout << n << " is normal." << endl;
                break;
            }
        }
        if (x == n)
            cout <<"The number "<< n << " is a Carmichael number." << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weifuliu/article/details/79966626