快速幂

2024-08-10

采用分治算法,通过位运算快速求出幂

#include <bits/stdc++.h>
#define io cin.tie(0), cout.tie(0), ios::sync_with_stdio(false)
#define LL long long
#define ULL unsigned long long
#define EPS 1e-8
#define INF 0x7fffffff
#define SUB -INF - 1
using namespace std;
LL fastbow(LL a, LL n, LL mod)
{
    LL ans = 1;
    a %= mod;
    while (n)
    {
        if (n & 1)
            ans = (ans * a) % mod;
        a = (a * a) % mod;
        n = n >> 1;
    }
    return ans;
}
int main()
{
    io;
    printf("%lld\n", fastbow(3, 3, INF));
    return 0;
}