Link Search Menu Expand Document

Binary Exponential

Table of contents

  1. Thuật toán
  2. Code

Thuật toán

Lũy thừa nhanh

Code

int binpow(int a, int n, int mod = (int) 1e9 + 7){
    int res = 1;
    while (n) {
        if (n & 1)
            res = 1ll * res * a % mod;
        a = 1ll * a * a % mod;
        n >>= 1;
    }
    return res;
}