P1226 【模板】快速幂

url: https://www.luogu.com.cn/problem/P1226

tag:
快速幂,模板

代码:

#include <iostream>
using namespace std;
typedef long long LL;
int main()
{
    LL a, k, p;
    cin >> a >> k >> p;
    LL kk = k;
    LL aa = a;
    LL res = 1;
    while (k)
    {
        if (k & 1) res = (LL)res * a % p;
        k >>= 1;
        a = (LL)a * a % p;
    }
    printf("%lld^%lld mod %lld=%lld",aa, kk, p, res);
    return 0;
}
添加新评论