#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;
}
在非负数的环境下可以用这个来卡常:
inline LL mul(LL a,LL b,LL p){
LL r = a*b-(LL)((long double)a/p*b+0.5)*p;
return r < 0 ? r + p : r;
}