Untitled

 avatar
unknown
c_cpp
5 months ago
642 B
3
Indexable
#include <iostream>
using namespace std;

const int MOD = 1000000007;

// Function to compute binary exponentiation a^b % MOD
long long binpow(long long a, long long b, int mod) {
    long long result = 1;
    a = a % mod;  // We need to take modulo of a to avoid overflow
    while (b > 0) {
        if (b % 2 == 1) {
            result = (result * a) % mod;  // If b is odd, multiply a to result
        }
        a = (a * a) % mod;  // Square a
        b /= 2;  // Divide b by 2
    }
    return result;
}

int main() {
    long long a, b;
    cin >> a >> b;
    cout << binpow(a, b, MOD) << endl;
    return 0;
}
Editor is loading...
Leave a Comment