学习中国剩余定理之前必须学会求解逆元: 跳转
一.中国剩余定理:
记得小时候听过一个故事,就是韩信想知道自己有多少士兵,然后就先让士兵排成x列,余出来a个人,后来又让士兵排成y列,余出来b个人,后来又让士兵排成z列,余出来c个人,最后韩信直接算出了自己的士兵数。。Orz~
呃呃呃,感觉有点像小学的时候做过的奥数题。。。
假使x,y,z分别为3,5,7,a,b,c分别为2,3,2,那么我们问最小士兵数是多少?
注意一点!!!x,y,z必须满足两两互质!!!
根据逆元的性质,我们可以知道:
3✖️5✖️mod_reverse(15, 7) ≡ 1 (mod 7) (1)
3✖️7✖️mod_reverse(21, 5) ≡ 1 (mod 5) (2)
5✖️7✖️mod_reverse(35, 3) ≡ 1 (mod 3) (3)
把(1)乘2,(2)乘3,(3)乘2,得:
2✖️3✖️5✖️mod_reverse(15, 7) ≡ 2 (mod 7) (4)
3✖️3✖️7✖️mod_reverse(21, 5) ≡ 3 (mod 5) (5)
2✖️5✖️7✖️mod_reverse(35, 3) ≡ 2 (mod 3) (6)
我们设:
a = 2✖️3✖️5✖️mod_reverse(15, 7)
b = 3✖️3✖️7✖️mod_reverse(21, 5)
c = 2✖️5✖️7✖️mod_reverse(35, 3)
因此我们可以得到:
(a + b + c) % 3 = a % 3 + b % 3 + c % 3 = 0 + 0 + 2 = 2;
(a + b + c) % 5 = a % 5 + b % 5 + c % 5 = 0 + 3 + 0 = 3;
(a + b + c) % 7 = a % 7 + b % 7 + c % 7 = 2 + 0 + 0 = 2;
所以说,士兵数为a + b + c就满足该条件
代码实现如下:
//省略扩展欧几里得求逆元的函数
ll china (ll n, ll *a, ll *m) { //m[i]代表3,5,7等除数,a[i]代表2,3,2等余数
ll M = 1; //M代表所有除数的乘积
ll res = 0;
for (int i = 0; i < n; i++) {
M *= m[i];
}
for (int i = 0; i < n; i++) {
ll w = M / m[i];
ret = (ret + a[i] * w * mod_reverse(w, m[i])) % M;
//若上面的式子爆longlong,可利用快速乘来求解
}
return (ret + M) % M;
}
二.中国剩余定理扩展:
前边注意中已经标注了,中国剩余定理的前提是x,y,z(也就是说除数)必须两两互质。如果不互质我们又该怎么办呢?
我们假设x ≡ a1 (mod n1)
x ≡ a2 (mod n2)
合并两式,得:n1k1 = n2k2 + a2 - a1
两边同除gcd(n1, n2),得:
n1k1 / gcd(n1, n2) = n2k2 / gcd(n1, n2) + (a2 - a1) / gcd(n1, n2)
由于n1 / gcd(n1, n2) 与n2 / gcd(n1, n2)互质,因此:
n1k1 / gcd(n1, n2) ≡ (a2 - a1) / gcd(n1, n2) (mod n2 / gcd(n1, n2))
同除n1 / gcd (n1, n2),得:k1 ≡ mod_reverse(n1 / gcd(n1, n2), n2 / gcd(n1, n2))✖️ (a2 - a1) / gcd(n1, n2) (mod n2 / gcd(n1, n2))
k1 = mod_reverse(n1 / gcd(n1, n2), n2 / gcd(n1, n2))✖️ (a2 - a1) / gcd(n1, n2) + n2 / gcd(n1, n2)✖️y
把k1代入x = n1k1 + a1,得:
x = mod_reverse(n1 / gcd(n1, n2), n2 / gcd(n1, n2))✖️ (a2 - a1) / gcd(n1, n2)✖️n1 + n1n2 / gcd(n1, n2)✖️y
呃呃呃,解x就求出来了…
这是两个式子的联立,那么多个呢?
对于多个,我们先求出两个数的解x,把x带入到a2中,并把n2替换为lcm(n1, n2),然后继续求2,3的解,直到递推到n结束。最后n - 1和 n的解x即为答案。
代码如下:
const int maxn=1e5+5;
int n;
ll m[maxn],a[maxn]; //m[i]为除数,a[i]为余数
//求gcd
ll gcd (ll a, ll b) {
return a % b == 0? b: gcd(b, a % b);
}
//扩展欧几里得算法
ll extend_gcd (ll a, ll b, ll &x, ll &y) {
if (a == 0 && b == 0) return -1;
if (b == 0) {
x = 1;
y = 0;
return a;
}
ll d = extend_gcd(b, a % b, y, x);
y -= a / b * x;
return d;
}
//求a关于n的逆元
ll mod_reverse (ll a, ll n) {
ll x, y;
ll d = extend_gcd(a, n, x, y);
if (d == 1) return (x % n + n) % n;
return -1;
}
//中国剩余定理扩展
ll work(){
int flag = 0;
for (int i = 2; i <= n; i++) {
ll n1 = m[i - 1], n2 = m[i];
ll a1 = a[i - 1], a2 = a[i];
ll d = gcd(n1, n2);
if ((a2 - a1) % d != 0) {
flag = 1;
break;
}
m[i] = m[i - 1] / d * m[i];
ll x = (mod_reverse(n1 / d, n2 / d) * (a2 - a1) / d) % (n2 / d) * n1 + a1;
x = (x % m[i] + m[i]) % m[i];
a[i] = x;
}
if (flag) return -1;
return a[n];
}
大概就是这样~
转载请注明出处!!!
如果有写的不对或者不全面的地方 可通过主页的联系方式进行指正,谢谢