POJ 3641 Pseudoprime numbers (快速幂)

题意:给出a和p,判断p是否为合数,且满足a^p是否与a模p同余,即a^p%p与a是否相等

算法:筛法打1万的素数表预判p。再将幂指数的二进制形式表示,从右到左移位,每次底数自乘。

#include <cstdio>
#include <cstring>
typedef long long LL;

int p[10010];
bool np[100010];
int cntp;

void SievePrime(int n) {
	memset(np, true, sizeof(np));
	np[0] = np[1] = false;
	for (int i = 2; i <= n; ++i) {
		if (np[i]) p[cntp++] = i;
		for (int j = i * 2; j <= n; j+=i) {
			np[j] = false;
		}
	}
}

LL Ksm(LL a, LL b, LL p) {
	LL ans = 1;
	while (b) {
		if (b & 1) {
			ans = (ans * a) % p;
		}
		a = (a * a) % p;
		b >>= 1;
	}
	return ans;
}

bool IsPrime(LL a) {
	if (a <= 100000) return np[a];
	for (int i = 0; i < cntp; ++i) {
		if (a % p[i] == 0) return false;
	}
	return true;
}

int main() {
	SievePrime(100000);
	LL a, p;
	while (scanf("%lld%lld", &p, &a) != EOF && p) {
		if (IsPrime(p)) {
			printf("no\n");
		}
		else {
			printf("%s\n", Ksm(a, p, p) == a ? "yes" : "no");
		}
	}

	return 0;
}
时间: 2024-10-06 08:02:57

POJ 3641 Pseudoprime numbers (快速幂)的相关文章

POJ 3641 Pseudoprime numbers 米勒罗宾算法

链接:http://poj.org/problem?id=3641 题意:由费马小定理可得,对于素数p,a^p = a (mod p),但是对于某些非素数p,也有比较小的可能满足a^p = a (mod p),如果满足,则称p是a条件下的伪素数,现给出p,a,问p是不是a条件的伪素数. 思路:首先用米勒 罗宾判断p是不是素数,如果不是,判断a^p = a (mod p)是否成立. 代码: #include <iostream> #include <cstdio> #include

poj 3641 Pseudoprime numbers 【快速幂】

Pseudoprime numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6645   Accepted: 2697 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

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

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

POJ 3641 Pseudoprime numbers(快速幂)

嗯... 题目链接:http://poj.org/problem?id=3641 AC代码: 1 #include<cstdio> 2 #include<iostream> 3 4 using namespace std; 5 6 inline bool is_prime(int x){ 7 if(x == 2) return 1; 8 if(x % 2 == 0) return 0; 9 for(int i = 3; i * i <= x; i += 2){ 10 if(!

poj 3641 Pseudoprime numbers Miller_Rabin测素裸题

题目链接 题意:题目定义了Carmichael Numbers 即 a^p % p = a.并且p不是素数.之后输入p,a问p是否为Carmichael Numbers? 坑点:先是各种RE,因为poj不能用srand()...之后各种WA..因为里面(a,p) ?= 1不一定互素,即这时Fermat定理的性质并不能直接用欧拉定理来判定..即 a^(p-1)%p = 1判断是错误的..作的 #include<iostream> #include<cstdio> #include&l

POJ 3641 Pseudoprime numbers

p是素数直接输出no,然后判断a^p%p和a是否相等. #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<map> #include<algorithm> using namespace std; long long mod_exp(long long a, long long b, long long c) { long long

[POJ 3734] Blocks (矩阵快速幂、组合数学)

Blocks Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3997   Accepted: 1775 Description Panda has received an assignment of painting a line of blocks. Since Panda is such an intelligent boy, he starts to think of a math problem of paint

poj 3070 Fibonacci (矩阵快速幂求斐波那契数列的第n项)

题意就是用矩阵乘法来求斐波那契数列的第n项的后四位数.如果后四位全为0,则输出0,否则 输出后四位去掉前导0,也...就...是...说...输出Fn%10000. 题目说的如此清楚..我居然还在%和/来找后四位还判断是不是全为0还输出时判断是否为0然后 去掉前导0.o(╯□╰)o 还有矩阵快速幂的幂是0时要特判. P.S:今天下午就想好今天学一下矩阵乘法方面的知识,这题是我的第一道正式接触矩阵乘法的题,欧耶! #include<cstdio> #include<iostream>