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;
}
时间: 2024-10-17 19:32:52

UVA - 10006 - Carmichael Numbers (快速幂+素数判断)的相关文章

POJ3641 Pseudoprime numbers(快速幂+素数判断)

POJ3641 Pseudoprime numbers p是Pseudoprime numbers的条件: p是合数,(p^a)%p=a;所以首先要进行素数判断,再快速幂. 此题是大白P122 Carmichael Number 的简化版 /* * Created: 2016年03月30日 22时32分15秒 星期三 * Author: Akrusher * */ #include <cstdio> #include <cstdlib> #include <cstring&g

UVa 10006 Carmichael Numbers (快速幂 + 素性测试)

Carmichael Numbers Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description  Carmichael Numbers  An important topic nowadays in computer science is cryptography. Some people even think that cryptography is t

uva 10006 Carmichael Numbers

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=947 打出素数表,快速幂取模. 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <algorithm> 5 #define ll long long 6

UVA 10870 - Recurrences(矩阵快速幂)

UVA 10870 - Recurrences 题目链接 题意:f(n) = a1 f(n - 1) + a2 f(n - 2) + a3 f(n - 3) + ... + ad f(n - d), for n > d. 已知前d项求第n项 思路:矩阵快速幂,对应矩阵为 |a1 a2 a3 ... ad| |1 0 0 ... 0 0 0| |0 1 0 ... 0 0 0| |0 0 1 ... 0 0 0| |0 0 0 ... 0 0 0| |0 0 0 ... 1 0 0| |0 0 0

POJ3641 (快速幂) 判断a^p = a (mod p)是否成立

Description Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a (mod p). That is, if we raise a to the pth power and divide by p, the remainder is a. Some (but not very many) non-prime values of p, known as base-a ps

POJ1995 Raising Modulo Numbers(快速幂)

POJ1995 Raising Modulo Numbers 计算(A1B1+A2B2+ ... +AHBH)mod M. 快速幂,套模板 /* * Created: 2016年03月30日 23时01分45秒 星期三 * Author: Akrusher * */ #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> #

POJ 1995 Raising Modulo Numbers (快速幂模板)

Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4938   Accepted: 2864 Description People are different. Some secretly read magazines full of interesting girls' pictures, others create an A-bomb in their cellar, oth

uva 10006 Carmichael

比较基本的数论题目,不过<挑战程序设计>上说这个题有不用幂运算求解的两种方法,一种复杂度为根号n,一种是O(n)预处理,O(1)判定,我还没有想出来.... 快速幂的方法: 1 #include <iostream> 2 #include <cstring> 3 #include <cmath> 4 using namespace std; 5 6 typedef long long ll; 7 const int N = 65001; 8 bool vis

poj Raising Modulo Numbers 快速幂模板

Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 8606   Accepted: 5253 Description People are different. Some secretly read magazines full of interesting girls' pictures, others create an A-bomb in their cellar, oth