Advertisement
yarin0600

Untitled

Nov 13th, 2023
782
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.49 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int powModulu(int a, int b, int m);
  4.  
  5. int main()
  6. {
  7.    int a = 271;
  8.    int b = 321;
  9.    int m = 481;
  10.    std::cout << powModulu(a, b, m) << std::endl;
  11. }
  12.  
  13. int powModulu(int a, int b, int m)
  14. {
  15.    int res = 1;
  16.    int rollingModulu = a % m;
  17.  
  18.    // b is the exponent
  19.    while (b)
  20.    {
  21.       if (b & 1)
  22.       {
  23.          res = (res * rollingModulu) % m;
  24.       }
  25.       rollingModulu = (rollingModulu * rollingModulu) % m;
  26.  
  27.       b >>= 1;
  28.    }
  29.  
  30.    return res;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement