#include<bits/stdc++.h>usingnamespace std;typedefunsignedlonglongull;typedef __uint128_t L;structFastMod{
ull b, m;FastMod(ull b):b(b),m(ull((L(1)<<64)/ b)){}
ull reduce(ull a){
ull q =(ull)((L(m)* a)>>64);
ull r = a - q * b;// can be proven that 0 <= r < 2*b
return r >= b ? r - b : r;}};
FastMod F(2);intmain(){int M =1000000007; F =FastMod(M);
ull x =10ULL*M+3;
cout << x <<""<< F.reduce(x)<<"\n";// 10000000073 3
}
使用方法:
假设当前程序中需要取模的数为 ,那么就在 main 函数开头处调用 F = FastMod(p);。