poj3641

简单题

快速幂

View Code
#include <iostream>
#include
<cstdio>
#include
<cstdlib>
#include
<cstring>
using namespace std;

long long a, p;

long long power(long long a, long long n)
{
long long ret = 1;
for (long long m = a; n > 0; n >>= 1, m = m * m % p)
if (n & 1)
ret
= ret * m % p;
return ret;
}

bool prime(int a)
{
for (int i = 2; i * i <= a; i++)
if (a % i == 0)
return false;
return true;
}

int main()
{
//freopen("t.txt", "r", stdin);
while (scanf("%lld%lld", &p, &a), a | p)
{
long long x = power(a, p);
if (x == a && !prime(p))
printf(
"yes\n");
else
printf(
"no\n");
}
return 0;
}

你可能感兴趣的:(poj)