CSES - Exponentiation II
Author: Ryan Chou
Explanation
Fermat's Little Theorem tells us that , so we can calculate with modular exponentiation.
Implementation
Time Complexity:
C++
#include <bits/stdc++.h>using namespace std;using ll = long long;const ll MOD = 1e9 + 7;Code Snippet: Binary Exponentiation (Click to expand)int main() {int test_num;
Python
MOD = int(1e9) + 7for _ in range(int(input())):a, b, c = map(int, input().split())pow_bc = pow(b, c, MOD - 1)print(pow(a, pow_bc, mod))
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!