Uva 10006-Carmichael Numbers(快速幂)

题目链接:点击打开链接

题意:给一个数n,问这个数是不是Carmichael Numbers,Carmichael Numbers的定义为:一个数n如果不是素数且对于对于任意的    2=<a<n 都满足 a^n%n=a,那么这个数就是Carmichael Numbers,否则不是。快速幂暴力解决。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define maxn 65002
#define _ll __int64
#define ll long long
#define INF 0x3f3f3f3f
#define Mod 10000007
#define pp pair<int,int>
#define ull unsigned long long
using namespace std;
ll n;
bool pri[maxn];
void init()
{
	memset(pri, 1, sizeof(pri));
	pri[0] = 0;
	pri[1] = 0;

	for (int i = 2; i * i <= maxn; i++) {
		if (pri[i]) {
			for (int j = i * i; j <= maxn; j += i) {
				pri[j] = 0;
			}
		}
	}
}
ll pow_mod(ll a, ll n, ll p)
{
	if (n == 0) {
		return 1;
	}

	ll ans = pow_mod(a, n / 2, p);
	ans = ans * ans % p;

	if (n & 1) {
		ans = ans * a % p;
	}

	return ans;
}
void solve()
{
	if (pri[n]) {
		printf("%d is normal.\n", n);
		return ;
	}

	for (ll i = 2; i < n; i++) {
		if (pow_mod(i, n, n) != i) {
			printf("%d is normal.\n", n);
			return ;
		}
	}

	printf("The number %d is a Carmichael number.\n", n);
}
int main()
{
	init();

	while (~scanf("%lld", &n) && n) {
		solve();
	}

	return 0;
}
时间: 2024-12-25 13:43:19

Uva 10006-Carmichael Numbers(快速幂)的相关文章

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

题目传送:UVA - 10006 思路:就是快速幂暴力过去就行了,然后要注意点细节,就是快速幂的时候会爆int,然后就是先判断是否为素数,是素数就直接输出结果is normal,不然会超时 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #inclu

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 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

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

UVA 11609 Teams 组合数学+快速幂

In a galaxy far far away there is an ancient game played among the planets. The specialty of the gameis that there is no limitation on the number of players in each team, as long as there is a captain inthe team. (The game is totally strategic, so so