MAGCARI

Mod of Power

Aug 2nd, 2022
1,301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.44 KB | None | 0 0
  1. /*
  2.     Task    : Mod Of Power
  3.     Author  : Phumipat C. [MAGCARI]
  4.     Language: C++
  5.     Created : 30 July 2022 [11:03]
  6. */
  7. #include<bits/stdc++.h>
  8. using namespace std;
  9. int mod(int a,int b,int c){
  10.     //base case
  11.     if(b == 1)  return a%c;
  12.     //process
  13.     int res = mod(a,b/2,c);
  14.     if(b%2 == 0)
  15.         return (res*res)%c;
  16.     else
  17.         return (((res * res) % c) * a) % c;
  18. }
  19. int main(){
  20.     int q,a,b,c;
  21.     cin >> q;
  22.     while(q--){
  23.         cin >> a >> b >> c;
  24.         cout << mod(a,b,c) << '\n';
  25.     }
  26.     return 0;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment