【POJ】[3641]Pseudoprime numbers

Problem Description

Loading...

Problem Analysis

From the statement we know

A pseudoprime satisfies:
① p is not prime
② there exists 1 < a < p such that ap = a (mod p)

Now p and a are given
asking whether the current a can make p a pseudoprime
since the numbers are large
the primality test can be done directly
and the power can use modular fast exponentiation

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<stdio.h>
bool prime(int m) {
	for(int i=2; i*i<=m; i++)
		if(m%i==0)
			return false;
	return true;
}
int pow(int a,int b) {
	int MOD=b;
	__int64 r=1,t=a%MOD;
	while(b) {
		if(b&1)
			r=r*t%MOD;
		t=t*t%MOD;
		b>>=1;
	}
	return (int)r;
}
bool judge(int n,int m) {
	if(prime(n))
		return false;
	if(pow(m,n)==m%n)
		return true;
	return false;
}
int main() {
	int p,a;
	while(scanf("%d %d",&p,&a),p||a) {
		if(judge(p,a))
			printf("yes\n");
		else
			printf("no\n");
	}
	return 0;
}

Problem link:【POJ】[3641]Pseudoprime numbers

Loading...