POJ-3641 Pseudoprime numbers---快速幂
题目链接:
https://vjudge.net/problem/POJ-3641
题目大意:
问p是不是伪素数。伪素数条件:①p不是素数。② ap = a (mod p)。
思路:
直接快速幂模板+素数判断
1 #include<iostream> 2 #include<vector> 3 #include<queue> 4 #include<algorithm> 5 #include<cstring> 6 #include<cstdio> 7 #include<set> 8 #include<cmath> 9 using namespace std; 10 typedef pair<int, int> Pair; 11 typedef long long ll; 12 const int INF = 0x3f3f3f3f; 13 const int maxn = 1000+10; 14 int T, n, m; 15 ll pow(ll a, ll b, ll m) 16 { 17 ll ans = 1; 18 while(b) 19 { 20 if(b & 1)ans = (ans % m) * (a % m) % m; 21 b /= 2; 22 a = (a % m) * (a % m) % m; 23 } 24 ans %= m; 25 return ans; 26 } 27 bool noprime(int x) 28 { 29 for(int i = 2; i <= (int)sqrt(x + 0.5); i++) 30 { 31 if(x % i == 0)return true; 32 } 33 return false; 34 } 35 int main() 36 { 37 int p, a; 38 while(cin >> p >> a && (p + a)) 39 { 40 if(noprime(p) && pow(a, p, p) == a)cout<<"yes"<<endl; 41 else cout<<"no"<<endl; 42 } 43 }
越努力,越幸运