【中国剩余定理模板】

LL a[maxn], b[maxn], n;
void extend_gcd(LL a, LL b, LL &d, LL &x, LL &y)
{
    if(b == 0) { d = a, x = 1, y = 0; }
    else { extend_gcd(b, a%b, d, y, x), y -= x*(a/b); }
}
void extend_chinese_reminder(LL &a1, LL &b1)
{
	LL x, y, g, tmp, i, a2, b2;
	for(i = 1; i < n; i++) {
		a2 = a[i], b2 = b[i];
		extend_gcd(a1, a2, g, x, y);
		tmp = a2/g;
		x = x*(b2-b1)/g;
		x = (x%tmp+tmp)%tmp;
		b1 = a1*x+b1;
		a1 = (a1*a2)/g;
		b1 = (b1%a1+a1)%a1;
	}
}
//最后传出去的a1代表循环节,b1代表最小正解

你可能感兴趣的:(【中国剩余定理模板】)