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 and divide by p, the remainder
is a. Some (but not very many) non-prime values of p, known as base-pseudoprimes, have this property for some a. (And some, known as Carmichael Numbers, are base-a pseudoprimes for all a.)

Given 2 < p ≤ 1000000000 and 1 < a < p, determine whether or not p is a base-a pseudoprime.

Input

Input contains several test cases followed by a line containing "0 0". Each test case consists of a line containing p and a.

Output

For each test case, output "yes" if p is a base-a pseudoprime; otherwise output "no".

Sample Input

3 2
10 3
341 2
341 3
1105 2
1105 3
0 0

Sample Output

no
no
yes
no
yes
yes

题意:给出两个数p,a 如果p是素数输出no 否则判断a的p次方对p取模之后是不是等于a

代码:

#include <stdio.h>
#include <math.h>
#define LL __int64

int is_prime(int n){
	if(n < 2) return 0;
	for(int i = 2; i <= sqrt(n+0.0); i++){
		if(n%i == 0) return 0;
	}
	return 1;
}

int fast(int p, int a){
	LL r = p, t = 1, mod = p;
	while(r > 0){
		if(r&1) t = ((t%mod)*(a%mod))%mod;
		a = ((a%mod)*(a%mod))%mod;
		r >>= 1;
	}
	return t%mod;
}

int main(){
	int p, a;
	while(scanf("%d%d", &p, &a), p||a){
		if(is_prime(p)){
			printf("no\n"); continue;
		}
		else if(fast(p, a) == a){
			printf("yes\n");
		}
		else printf("no\n");
	}
	return 0;
} 
时间: 2024-10-13 05:30:21

poj 3641 Pseudoprime numbers 【快速幂】的相关文章

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)

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

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>