uva10006-Carmichael数

Posted 啊嘞

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了uva10006-Carmichael数相关的知识,希望对你有一定的参考价值。

题目链接 http://acm.hust.edu.cn/vjudge/problem/19411

 

解题思路

快速幂, 筛法求素数。

 

代码

#include<iostream>
#include<cstdio>
#include<string.h>
#include<cmath>
typedef long long ll; 
using namespace std;
const int maxS = 65005;
bool isPrime[maxS];
ll PowMod(int a, int b, int c)
{
    ll result = 1;
    ll base = a % c;
    while(b) {
        if(b & 1) result = (result * base) % c;
        base = (base * base) % c;
        b = b >> 1;
    }
    return result;
}
int main()
{
    int flag, n, m;
    memset(isPrime, 1, sizeof(isPrime));
    isPrime[0] = isPrime[1] = false;
    m = sqrt(maxS) + 0.5;
    for(int i=2; i<m; i++) {
        if(isPrime[i]) {
            for(int j=i*i; j<maxS; j+=i)
                isPrime[j] = false;
        }
    }
    cin >> n;
    while(n) {
        flag = false;
        if(isPrime[n]) { cout <<  n << " is normal." << endl; cin >> n; continue; }
        for(int i=2; i<n; i++) if(PowMod(i, n, n) != i) {
            flag = true;
            break;
        }
        if(!flag) cout << "The number " << n << " is a Carmichael number." << endl; 
        else cout <<  n << " is normal." << endl;
        cin >> n;
    }
    return 0;
}

 

以上是关于uva10006-Carmichael数的主要内容,如果未能解决你的问题,请参考以下文章

UVa580 Critical Mass

UVa 11419 SAM I AM (最小覆盖数)

[UVa10188]Automated Judge Script

UVa 1620 懒惰的苏珊(逆序数)

UVA 11542 高斯消元

uva10635(最长上升子列)