Advertisement
jasonpogi1669

Binary Exponentation Recursion using C++

Dec 6th, 2021
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.44 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int MOD = (int) 1e9 + 7;
  6.  
  7. int Power(int a, int b) {
  8.     if (b == 0) {
  9.         return 1;
  10.     }
  11.     if (b == 1) {
  12.         return a;
  13.     }
  14.     int ret = Power(a, b / 2);
  15.     if (b % 2 == 0) {
  16.         return (ret * ret) % MOD;
  17.     } else {
  18.         return ((ret * ret) % MOD * a) % MOD;
  19.     }
  20. }
  21.  
  22. int main() {
  23.     ios::sync_with_stdio(false);
  24.     cin.tie(0);
  25.     int x, y;
  26.     cin >> x >> y;
  27.     cout << Power(x, y) << '\n';
  28.     return 0;
  29. }
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement