倍增求快速冪,乘法,O(1) long long 乘法
阿新 • • 發佈:2019-02-01
#include<cstdio> #include<algorithm> #include<stdlib.h> #include<climits> #include<cstring> #include<ctime> #define MAXN LONG_LONG_MAX>>1 using namespace std; long long Mul(long long tmp,long long res,long long mod){ long long ret=0; while(res){ if(res&1) ret=(ret+tmp)%mod; if(tmp>MAXN)tmp=tmp-(mod-tmp); else tmp=(tmp<<1)%mod; res>>=1; } return ret; } LL mul(LL a,LL b,LL c){ return ((a*b-(LL)((long double)a/c*b+1e-8)*c)%c+c)%c; } LL Pow(LL a,LL b,LL c,LL ret=1) {for(;b;b>>=1,a=mul(a,a,c))if(b&1)ret=mul(ret,a,c);return ret;}
O(1) long long 乘法:
inline LL mul(LL x,LL y,LL P)
{
return (x*y-(LL)((long double)x/P*y)*P+P)%P;
}