Description
求a的b次方,取模mod(1<=a,b,mod<=1e18)
Input:多组输入,每组数据一行,3个正整数,分别为a,b,mod
Output:每组数据输出一行为答案
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
/long long qmod(ll a,ll b,int c)
{
if(b==1) return a;
if(b&1) return aqmod(a,b-1,c)%c;
else
{
ll m=qmod(a,b/2,c);
return m*m%c;
}
} /用二分还是超时
ll qmod(ll a,ll b,ll c)//快速幂
{
int r=1;
while(b)
{
if(b&1) r=ar%c;
a=a*a%c;
b>>=1; b的二进制形式删除最后一位
}
return r;
}
int main()
{
long long a,b,c;
cin>>a>>b>>c;
if(a>c) a=a%c;
if(c==1) cout<<“0”<<endl;
else
{
ll ans=qmod(a,b,c);
cout<<ans<<endl;
}
}
参考资料: https://blog.csdn.net/qq_42265608/article/details/89475457