Last active
September 17, 2023 13:51
-
-
Save NamPE286/733c207309e862ac00681fe8eb3d14dc to your computer and use it in GitHub Desktop.
Modular binary exponentation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
typedef long long ll; | |
ll powMod(ll a, ll b, ll m) { | |
if (b == 0) { | |
return 1; | |
} | |
ll x = powMod(a, b / 2, m) % m; | |
if (b % 2) { | |
return ((x * x) % m * a) % m; | |
} else { | |
return (x * x) % m; | |
} | |
} | |
int main() { | |
ios::sync_with_stdio(0); | |
cout.tie(0); | |
cin.tie(0); | |
ll t; | |
cin >> t; | |
while (t--) { | |
ll a, b; | |
cin >> a >> b; | |
cout << powMod(a, b, 1e9 + 7) << '\n'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment