Advertisement
mickypinata

SMMR-T033: Power Mod

Nov 17th, 2020 (edited)
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.69 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long lli;
  5.  
  6. int main(){
  7.  
  8.     int Q;
  9.     scanf("%d", &Q);
  10.     for(int q = 1; q <= Q; ++q){
  11.         int base, exp, mod;
  12.         scanf("%d %d %d", &base, &exp, &mod);
  13.         vector<lli> exponent(30, 0);
  14.         exponent[0] = base % mod;
  15.         for(int i = 1; i <= 29; ++i){
  16.             exponent[i] = (exponent[i - 1] * exponent[i - 1]) % mod;
  17.         }
  18.         lli ans = 1;
  19.         for(int i = 29; i >= 0; --i){
  20.             if(exp - (1 << i) >= 0){
  21.                 ans *= exponent[i];
  22.                 ans %= mod;
  23.                 exp -= (1 << i);
  24.             }
  25.         }
  26.         cout << ans << "\n";
  27.     }
  28.  
  29.     return 0;
  30. }
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement