POJ-3641 Pseudoprime numbers---快速幂

时间:2022-02-13 12:41:47

题目链接:

https://vjudge.net/problem/POJ-3641

题目大意:

问p是不是伪素数。伪素数条件:①p不是素数。② ap = a (mod p)。

思路:

直接快速幂模板+素数判断

 #include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<set>
#include<cmath>
using namespace std;
typedef pair<int, int> Pair;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int maxn = +;
int T, n, m;
ll pow(ll a, ll b, ll m)
{
ll ans = ;
while(b)
{
if(b & )ans = (ans % m) * (a % m) % m;
b /= ;
a = (a % m) * (a % m) % m;
}
ans %= m;
return ans;
}
bool noprime(int x)
{
for(int i = ; i <= (int)sqrt(x + 0.5); i++)
{
if(x % i == )return true;
}
return false;
}
int main()
{
int p, a;
while(cin >> p >> a && (p + a))
{
if(noprime(p) && pow(a, p, p) == a)cout<<"yes"<<endl;
else cout<<"no"<<endl;
}
}