码迷,mamicode.com
首页 > 其他好文 > 详细

uva10006-Carmichael数

时间:2016-09-02 23:13:11      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:

题目链接 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数

标签:

原文地址:http://www.cnblogs.com/ZengWangli/p/5835801.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!