Skip to content

Instantly share code, notes, and snippets.

@NamPE286
Last active September 17, 2023 13:51
Show Gist options
  • Save NamPE286/733c207309e862ac00681fe8eb3d14dc to your computer and use it in GitHub Desktop.
Save NamPE286/733c207309e862ac00681fe8eb3d14dc to your computer and use it in GitHub Desktop.
Modular binary exponentation
#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