Catalog
快速幂模板

适用于 b的 p次方 % k

#include<bits/stdc++.h>
using namespace std;
long long b, a, p, k, ans = 1, c;
int main()
{
scanf("%d%d%d", &b, &p, &k);
a = b; c = p;
while (p > 0)
{
if (p % 2 != 0)
ans = ans * b%k;
b = b * b % k;
p = p >> 1;
}
printf("%lld %lld mod %lld=%lld", a, c, k, ans%k);
return 0;
}
Author: superzhaoyang
Link: http://yoursite.com/2019/11/20/快速幂模板/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Donate
  • 微信
  • 支付宝
��