Tuesday, February 4, 2020

POJ.3641 Pseudoprime numbers

1.Problem
http://poj.org/problem?id=3641

2.Idea
Prime num check and Fast Power

3.Source
 bool is_prime(ll n)  
 {  
      for (ll i = 2; i*i <= n; i++) {  
           if (n % i == 0) return false;  
      }  
      return n != 1;  
 }  
 ll mod_pow(ll x, ll n, ll mod)  
 {  
      if (n == 0) return 1;  
      ll res = mod_pow(x * x % mod, n / 2, mod);  
      if (n & 1) res = res * x % mod;  
      return res;  
 }  
 int main()  
 {  
      while (true) {  
           ll p, a;  
           cin >> p >> a;  
           if (a + p == 0) break;  
           if (!is_prime(p) && a - mod_pow(a, p, p) == 0) cout << "yes" << endl;  
           else cout << "no" << endl;  
      }  
      return 0;  
 }  

No comments:

Post a Comment