您的位置:首页 > 其它

【中国剩余定理模板】

2014-07-17 14:58 204 查看
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代表最小正解
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: