UVA - 10006 - Carmichael Numbers (快速幂+素数判断)


题目传送:UVA - 10006




思路:就是快速幂暴力过去就行了,然后要注意点细节,就是快速幂的时候会爆int,然后就是先判断是否为素数,是素数就直接输出结果is normal,不然会超时


AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <cctype>
#define LL long long
#define INF 0x7fffffff
using namespace std;

int n;

int is_prime(int x) {
	if(x == 1) return 0;
	if(x == 2 || x == 3) return 1;
	for(int i = 2; i <= sqrt(x); i ++) {
		if(x % i == 0) return 0;
	}
	return 1;
}

int kmod(int x, int mod) {
	LL ret = 1;
	int tt = x;	
	int t = mod;
	while(t) {
		if(t & 1) ret = (ret * x) % mod;
		x = ((LL)x * x) % mod;		//这里会爆int 
		t >>= 1;
	}
	if(ret == tt) return 0;
	else return 1; 
}

int judge(int x) {
	for(int i = 2; i < n; i++) {
		if(kmod(i, n)) return 0;
	}
	return 1;
}

int main() {
	while(scanf("%d", &n) != EOF) {
		if(n == 0) break;
		
		if(!is_prime(n) && judge(n)) {		//判断素数放前面,自以为没多大影响,结果TLE了一下 
			printf("The number %d is a Carmichael number.\n", n);
		}
		else {
			printf("%d is normal.\n", n);
		}
	}
	return 0;
}













评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值