UVA 10006(快速幂+暴力)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cugsl/article/details/81838305

题意

给你一个数n,问你这个数是不是卡迈克尔数。如果一个数是合数且满足对于每一个比它小的数a都有 a n = a ( n ) ;

分析

暴力判断即可,但是要注意先判断是否是质数,如果是质数一定不是卡迈克尔数,就不需要进行费马检验了。否则亲测超时。

代码

#include <iostream>
#include <cmath>
using namespace std;
typedef long long int ll;
bool flag1 = false, flag2 = false;
int m;
inline ll pow_m(ll a, ll b, ll mod)
{
    ll res = 1;
    while (b)
    {
        if (b & 1) res = (res*a) % mod;
        a = (a*a) % mod;
        b >>= 1;
    }
    res = res % mod;
    return res;
}

int main()
{
    int n;
    ios::sync_with_stdio(false);
    while (cin >> n && n)
    {
        flag1 = false, flag2 = false;
        m = sqrt(n + 0.5);
        for (int i = 2; i <= m; i++)
        {
            if (n%i == 0)
            {
                flag2 = true;
                break;
            }
        }
        if (!flag2) { cout << n << " is normal." << endl; continue; }
        for (int i = 2; i <= n - 1; i++)
        {
            int aa = pow_m(i, n, n);
            if (aa != i)
            {
                flag1 = true;
                break;
            }
        }
        if (!flag1&&flag2)  cout << "The number " << n << " is a Carmichael number." << endl; 
        else cout << n << " is normal." << endl;        
    }
}

猜你喜欢

转载自blog.csdn.net/cugsl/article/details/81838305