//求gcd(a, b) LL gcd(LL a, LL b) { return b ? gcd(b, a%b) : a; }
//求整数x和y,使得ax+by=d, 且|x|+|y|最小。其中d=gcd(a,b) void gcd(LL a, LL b, LL& d, LL& x, LL& y) { if(!b) { d = a; x = 1; y = 0; } else { gcd(b, a%b, d, y, x); y -= x * (a/b); } }
//计算模n下a的逆。如果不存在逆, 返回-1 LL inv(LL a, LL n) { LL d, x, y; gcd(a, n, d, x, y); return d == 1 ? (x+n)%n : -1; }
原文地址:http://blog.csdn.net/u011686226/article/details/25492949