题意:给出p, a两个整数,判断p是否是以a为底的假素数,也就是要满足a^p=a(mod p).
可以用快速幂取模求出a^p来做.
#include <cstdio>
using namespace std;
bool is_prime(int n){
for(int i = 2; i * i <= n; ++i){
if(n % i == 0)return false;
}
return true;
}
long long mod_pow(long long a, long long b, long long c){
long long res = 1, t = a;
while(b){
if(b & 1)res = res * t % c;
t = t * t % c;
b >>= 1;
}
return res % c;
}
int main(int argc, char const *argv[]){
int p, a;
while(scanf("%d%d", &p, &a) == 2){
if(!p && !a)break;
if(is_prime(p)){//If p is a base-a pseudoprime, p has to be a nonprime, fair enough.
printf("no\n");
}
else{
if(mod_pow(a, p, p) == a){
printf("yes\n");
}else{
printf("no\n");
}
}
}
return 0;
}