surajmateti

modinveersse

Jun 6th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.27 KB | None | 0 0
  1. ll modularExponentiation(ll x,ll n,ll M)
  2. {
  3.     ll result=1;
  4.     while(n>0)
  5.     {
  6.         if(n % 2 ==1)
  7.             result=(result * x)%M;
  8.         x=(x*x)%M;
  9.         n=n/2;
  10.     }
  11.     return result;
  12. }
  13. ll modinv(ll A)
  14. {
  15.     return modularExponentiation(A,m-2,m);
  16. }
Add Comment
Please, Sign In to add comment