Advertisement
_Mizanur

BigMod

Nov 15th, 2022
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. typedef vector<int> vi;
  5.  
  6. #define rapid_io() ios::sync_with_stdio(false);cin.tie(0);
  7. #define endl '\n'
  8. #define pb push_back
  9. #define all(a) (a).begin(),(a).end()
  10. #define rall(a) (a).rbegin(),(a).rend()
  11. ll recursive(ll b,ll p,ll m)
  12. {
  13.     if(p==0)
  14.         return 1;
  15.     else if(p%2==0)
  16.     {
  17.       return recursive(((b*b)%m),p/2,m);
  18.     }
  19.     else
  20.     {
  21.         return (b*recursive((b*b)%m,(p-1)/2,m)%m);
  22.     }
  23. }
  24. int main()
  25. {
  26.     rapid_io();
  27.     ll b,p,m;
  28.     while(cin>>b>>p>>m){
  29.  
  30.       cout<<recursive(b,p,m)<<endl;
  31.     }
  32.  
  33.  
  34.     return 0;
  35. }
  36.  
  37.  
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement