求模乘法的逆元

2019-04-13 21:43发布

void exgcd(LL a, LL b, LL& d, LL& x, LL& y) { if(b == 0) { d = a; x = 1; y = 0; } else { exgcd(b, a%b, d, y, x); y -= x*(a/b); } } LL inv(LL a, LL mod) { LL d, x, y; exgcd(a, mod, d, x, y); return d == 1 ? (x+mod)%mod : -1; }