学习笔记--快速乘 大法好!!!!
快速乘大法可以防a*b%p爆longlong,在a%p*b%m还有可能爆longlong时,快速乘吧!!!!少年!!!!
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
long long quick_mul(long long x,long long y,long long p)
{
if (y==0) return 0;
if (y==1) return x%p;
long long re;
re=quick_mul(x,y>>1,p);
if ((y&1)==1) return (re+re+x)%p;
else return (re+re)%p;
}
int main()
{
long long x=1000000000,y=1000000000,p=10;
printf("%I64d\n",quick_mul(x,y,p));
return 0;
}
——It's a lonely path. Don't make it any lonelier than it has to be.