programming-challenges Carmichael Numbers (110702) 题解

2 hints: 

  1. need long long to calculate multiple-mode, int may overflow;
  2. check whether the number is a prime at first, as prime need verify all the numbers smaller than it, it consume much time. 
codes: 
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <assert.h>
#include <algorithm>
#include <math.h>
#include <ctime>
#include <functional>
#include <string.h>
#include <stdio.h>
#include <numeric>
#include <float.h>

using namespace std;

vector<bool> isPrime(65010, true); 

void getPrime() {
	int q = sqrt((double)65000); 
	for (int i = 2; i <= q; i++) {
		if (isPrime[i]) {
			for (int j = i + i; j < 65010; j += i) {
				if (isPrime[j]) {
					isPrime[j] = false; 
				}
			}
		}
	}
}

int modMul(int a, int n) {
	int mn = n; 
	long long result = a;
	n--; 
	long long exp = 1, pow = a;
	while (n > 0) {
		pow *= pow; 
		pow %= mn; 
		exp *= 2; 
		if (n >= exp) {
			n -= exp; 
			result *= pow; 
			result %= mn; 
		}
		else {
			n -= 1; 
			exp = 1; 
			pow = a; 
			result *= a; 
			result %= mn; 
		}
		if (n == 0) break;
	}

	return result; 
}

bool isAPrime(int num) {
	return isPrime[num];
}

bool isCalm(int num) {
	for (int f = 2; f < num; f++) {
		if (modMul(f, num) != f) {
			return false;
		}
	}
	return true;
}

int main() {
	getPrime();
	int i; 
	while (cin >> i) {
		if (i == 0) break;
		
		if (!isAPrime(i) && isCalm(i)) 
			cout << "The number " << i << " is a Carmichael number." << endl;
		else
			cout << i << " is normal." << endl; 
	}

	return 0; 
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值