题意:判断P是否为素数,是即输出no,不是就计算a的p次方是否等于a,是就输出yes,否则输出no;
key:快速幂,判断素数,两个函数即可;
/*快速幂, Carmicharl numbers*/ #include <iostream> #include <stdio.h> #include <algorithm> using namespace std; typedef long long ll; ll a, p; bool ok_prime(ll t) //如果是素数,直接输出no { if(t == 2) return 1; else{ for(int i = 2; i * i < t; i++){ if(t % i == 0) return 0; } return 1; } } ll mod_pow(ll n, ll m) { ll res = 1; ll l; l = m; while(l > 0){ if(l & 1){ res = res * n % m; } n = n * n % m; l >>= 1; } return res; } int main() { while(scanf("%l64d%l64d", &p, &a) != EOF){ if(a == 0 && p == 0) break; if(ok_prime(p)) printf("no "); else{ if(mod_pow(a, p) == a) printf("yes "); else printf("no "); } } return 0; }