Tuesday, February 4, 2020

POJ.1995 Raising Modulo Numbers

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

2.Idea
Fast Power.

3.Source
 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()  
 {  
      int z;  
      cin >> z;  
      while (z--) {  
           ll sum = 0;  
           ll mod;  
           cin >> mod;  
           int h;  
           cin >> h;  
           for (int i = 0; i < h; i++) {  
                ll a, b;  
                cin >> a >> b;  
                sum = (sum + mod_pow(a, b, mod)) % mod;  
           }  
           cout << sum << endl;  
      }  
      return 0;  
 }  

No comments:

Post a Comment