#快速乘
题:计算a*b%p,其中1<=a,b,p<=1e18;很明显,我们就算开long long也是会爆的,然后,高精度?想想,高精度乘法加高精度取模,多麻烦(用C语言实现,你会Java、Python的话当我没说)
所以,我们用快速乘的方法解决这个问题。
快速乘和快速幂的思想类似,只是将乘换成加。
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int N=1e7+7;
const int mod=1e9+7;
typedef long long ll;
ll qmul(ll a,ll b,ll p)
{
ll ans=0;
while(b)
{
if(b&1)
{ans+=a;ans%=p;}
a+=a;
b>>=1;
}
return ans;
}
int main()
{
IOS;
ll a,b,p;
cin>>a>>b>>p;
cout<<qmul(a,b,p)<<endl;
}