#include<bits/stdc++.h>
using namespace std;
long long b, a, p, k, ans = 1, c;
int main()
{
scanf("%lld%lld%lld", &b, &p, &k);
a = b; c = p;
while (p > 0)//快速幂
{
if (p % 2 != 0)
ans = ans * b % k;//如果p为单数,乘到ans里面去,然后取模
b=b * b % k;//每次运算都取模
p = p >> 1; //用位运算除2,可能会快一点
}
ans %= k;
printf("%lld^%lld mod %lld=%lld", a, c, k, ans);
return 0;
}
P1226 【模板】快速幂 | 取余运算 fastpow函数 二进制拆分法函数部分
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL fp( LL in,LL n,LL p ) // LL fastpow
{
LL ans=1;
while(n!=0)
{
if(n&1) ans=(ans%p*in%p)%p;
in=(in%p*in%p)%p;
n>>=1;
}
return ans%p;
}
P1226 【模板】快速幂 | 取余运算 fastpow函数 二进制拆分法
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL fp( LL in,LL n,LL p ) // LL fastpow
{
LL ans=1;
while(n!=0)
{
if(n&1) ans=(ans%p*in%p)%p;
in=(in%p*in%p)%p;
n>>=1;
}
return ans%p;
}
int main()
{
LL a,b,p;
while(cin>>a>>b>>p)//如果输入的变量不为空
{
printf("%lld^%lld mod %lld=%lld\n",a,b,p,fp(a,b,p));
}
return 0;
}