POJ 3641 Pseudoprime numbers(快速幂)

嗯...

 

题目链接:http://poj.org/problem?id=3641

 

AC代码:

 1 #include
 2 #include
 3 
 4 using namespace std;
 5 
 6 inline bool is_prime(int x){
 7     if(x == 2) return 1;
 8     if(x % 2 == 0) return 0;
 9     for(int i = 3; i * i <= x; i += 2){
10         if(!(x % i)) return 0;
11     }
12     return 1;
13 }
14 
15 inline long long quick_mod(long long a, long long b, long long m){
16     long long ans = 1;
17     while(b){
18         if(b & 1) ans = ans * a % m;
19         a = a * a % m;
20         b >>= 1;
21     }
22     return ans;
23 }
24 
25 int main(){
26     long long m, n;
27     while(~scanf("%lld%lld", &m, &n) && m + n){
28         if(is_prime(m)){
29             printf("no\n");
30             continue;
31         }
32         long long ans;
33         ans = quick_mod(n, m, m);
34         if(ans == n) printf("yes\n");
35         else printf("no\n");
36     }
37     return 0;
38 }
AC代码

 

转载于:https://www.cnblogs.com/New-ljx/p/11515361.html

你可能感兴趣的:(POJ 3641 Pseudoprime numbers(快速幂))