快速幂算法
题目一——P1226 【模板】快速幂 - 洛谷
这题其实非常简单
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL call(LL a,LL b,LL p)
{
LL ret=1;
while(b)
{
if(b&1)
{
ret=(ret*a)%p;//注意这里是*
}
a=a*a%p;
b>>=1;
}
return ret;
}
int main()
{
LL a,b,p;
cin>>a>>b>>p;
printf("%lld^%lld mod %lld=%lld",a, b, p, call(a, b, p));
}
题目二——P10446 64位整数乘法 - 洛谷
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL call(LL a,LL b,LL p)
{
LL ret=0;
while(b)
{
if(b&1)
{
ret=(ret+a)%p;//注意是+
}
a=(a+a)%p;//a要翻两倍
b>>=1;
}
return ret;
}
int main()
{
LL a,b,p;
cin>>a>>b>>p;
cout<<call(a,b,p)<<endl;
}