扩展欧几里得

· · 算法·理论

扩展欧几里得算法

我们来看一种在数论中最最基础也非常常用的一种工具,扩展欧几里得算法。

前置知识:

  1. 欧几里得算法(辗转相除法):
int gcd(int x, int y) {
  if (y == 0) return x;
  return gcd(y, x % y);
}

原理:\gcd(a, b) = \gcd(b, a \bmod b)。

  1. 整数除法和取余运算:这个就不用多说了,很简单直接跳过。

  2. 扩展欧几里得算法理论推导:

我们设现在有一个线性组合 a\times x + b\times y = \gcd(a, b),其中 a,b,c 均为整数,求其一组整数解 x, y。

那么我们可以先对此进行一次辗转相除:

b \times x_1 + (a \bmod b)\times y_1 = \gcd(b, a \bmod b)

又由

\gcd(a, b) = \gcd(b, a \bmod b)

得到

b\times x_1 + (a \bmod b)\times y_1 = a\times x + b\times y

因为

a \bmod b = a - \lfloor \frac{a}{b} \rfloor \times b

推出

a\times x + b\times y &= b\times x_1 + (a \bmod b)\times y_1 \\ &= b\times x_1 + a\times y_1 - \lfloor \frac{a}{b} \rfloor \times b\times y_1 \\ &= a \times y_1 + b \times (x_1 - \lfloor \frac{a}{b} \rfloor \times y_1) \end{aligned}

所以由待定系数法,得 $\begin{cases} x = y_1\ y = x_1 - \lfloor \frac{a}{b} \rfloor \times y_1\ \end{cases}

### 理论应用: - 解不定方程的一组特解 我们模板得到的是:$a \times x + b \times y = \gcd(a,b)$ 的而一组特解。 但是我们常规经常需要求解的则是:$a \times x + b \times y = c$ 的最小非负整数解。 过程: 1. 裴蜀定理判断是否有解: >定理内容:对于正整数 $a,b$,$a\times x+b\times y=c$ 有解的充要条件是 $\gcd(a,b) | c$。 2. 对于 exgcd 方法求出的一组特解,需要乘上一个系数 $c \div d$。 2. 如果我们需要求出最小的非负整数解,我们就需要使得 $a = a \div d$,$b = b \div d$,然后使用这两个系数对求出的特解 $x$,$y$ 进行取模。 - 解同余方程 同余方程的一般形式为:$a \times x \equiv c (\bmod b)$。 我们同样可以直接将同余方程转化为上面那种方式求解。 扩展欧几里得代码模板实现: ```cpp int exgcd(int a, int b, int &x, int &y) { // 注意这里的取指符,我们在回溯的时候需要用到 if (b == 0) { x = 1, y = 0; return a; } int x1, y1; int d = exgcd(b, a % b, x1, y1); // 递归 x = y1; y = x1 - (a / b) * y1; // 推导的世子 return d; } ``` ### 例题 ### [P2613 【模板】有理数取余](https://www.luogu.com.cn/problem/P2613) 这道题就是将同余方程转化为不定方程的经典应用,需要注意 $a$ 和 $b$ 的数据范围非常大,可以使用字符串读入的方法边读入边取模,然后在最后使用扩展欧几里得求出答案。 注意判断无解和最终求答案的过程。 ::::info[Code] ```cpp #include <bits/stdc++.h> using namespace std; #define int long long string s, t; int a, b, x, y, mod = 19260817; int exgcd(int a, int b, int &x, int &y) { if (b == 0) { x = 1, y = 0; return a; } int x1, y1; int d = exgcd(b, a % b, x1, y1); x = y1; y = x1 - (a / b) * y1; return d; } signed main() { cin >> s >> t; for (int i = 0; i < s.size(); i++) a = (a * 10 + s[i] - '0') % mod; for (int i = 0; i < t.size(); i++) b = (b * 10 + t[i] - '0') % mod; int d = exgcd(b, mod, x, y); if (a % d != 0) { cout << "Angry!"; return 0; } x = x * (a / d); mod /= d; cout << (x % mod + mod) % mod << '\n'; return 0; } ``` :::: ### [P1516 青蛙的约会](https://www.luogu.com.cn/problem/P1516) 题目意思就是给定 $x$,$y$,$m$,$n$,$L$,求出最小的非负整数 $t$ 使得 $x + m \times t\equiv y + n \times t (\bmod L)$。 我们发现整个式子其实未知数只有 $t$,那么我们可以将其化简使用扩展欧几里得求解。 $$ x + m \times t\equiv y + n \times t (\bmod L)$$ $$(m - n) \times t \equiv y - x (\bmod L)$$ 发现可以使用上面告知的方式直接求出答案。 需要注意的一点是 $m - n$ 和 $y - x$ 可能为负数,需要在为负数的时候调换成正数。 ::::info[Code] ```cpp #include <bits/stdc++.h> using namespace std; #define int long long int x, y, m, n, l, xx, yy; int exgcd(int a, int b, int &x, int &y) { if (b == 0) { x = 1, y = 0; return a; } int x1, y1; int d = exgcd(b, a % b, x1, y1); x = y1; y = x1 - (a / b) * y1; return d; } signed main() { cin >> x >> y >> m >> n >> l; int a = m - n, c = y - x, b = l; if (a < 0) a = -a, c = -c; int d = exgcd(a, b, xx, yy); if (c % d != 0) { cout << "Impossible"; return 0; } xx = xx * (c / d); b /= d; cout << (xx % b + b) % b << '\n'; return 0; } ``` :::: ### [P5656 【模板】二元一次不定方程 (exgcd)](https://www.luogu.com.cn/problem/P5656) 题目意思简单明了,由前面的推导过程可以得知 $x$ 与 $y$ 的循环节分别是 $b$ 和 $a$,那么解的个数就可以很好的求出来了。 首先 $x$ 和 $y$ 的正整数最小值解很好求,其次可以发现,当 $x$ 取最小值时,$y$ 取最大值,同理当 $y$ 取最小值时,$x$ 取最大值。 所以可以直接使用 $y$ 的最大值判断是否有正整数解。 然后发现我们已经求出了 $x$ 的最大值和最小值解,又我们知道 $x$ 的循环节,所以可以直接用最大值减去最小值除以循环节加一来求出答案。 注意判断无解和无正整数解的情况。 ::::info[Code] ```cpp #include <bits/stdc++.h> using namespace std; #define int long long int T, a, b, c, x, y; int exgcd(int a, int b, int &x, int &y) { if (b == 0) { x = 1, y = 0; return a; } int x1, y1; int d = exgcd(b, a % b, x1, y1); x = y1; y = x1 - (a / b) * y1; return d; } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> T; while (T--) { cin >> a >> b >> c; int d = exgcd(a, b, x, y); if (c % d != 0) { cout << -1 << '\n'; continue; } int xx = x * (c / d), yy = y * (c / d); b /= d, a /= d; int x0 = ((xx % b + b) % b ? (xx % b + b) % b : b); int y0 = (c - a * x0 * d) / (b * d); int y1 = ((yy % a + a) % a ? (yy % a + a) % a : a); int x1 = (c - b * y1 * d) / (a * d); int cnt = (x1 - x0) / b + 1; if (y0 > 0) cout << cnt << ' ' << x0 << ' ' << y1 << ' ' << x1 << ' ' << y0 << '\n'; else cout << x0 << ' ' << y1 << '\n'; } return 0; } ``` :::: ### [CF710D Two Arithmetic Progressions](https://codeforces.com/problemset/problem/710/D) 纯推式子题 + 数据恶心 + 坑点多。 我们直接对题目给的式子下手: $$a_1 \times k + b_1 = a_2 \times l + b_2$$ $$a_1 \times k - a_2 \times l = b_2 - b_1$$ 那么很明显可以发现这个式子已经满足可以进行扩展欧几里得的形式了。 所以令 $A = a_1$,$B = -a_2$,$C = b_2 - b_1$。 转化为 $$A \times k + B \times l = C$$ 求出一组特解 $k_0$,$l_0$。 写出可以用其表达的通解: 设出一个变量 $t$ 且 $t$ 为整数。 $$\begin{cases} k = k_0 + t \times B\\ l = l_0 + t \times A \end{cases} $$ 所以题目要求的答案转变为 $t$ 的个数。 将给出的通解带回原式中,设原式值为 $p$,则: $$\begin{aligned} p &= a_1 \times k + b_1 \\ &= a_1 \times (k_0 + t \times B) + b_1 \end{aligned} $$ 又 $L \le p \le R$。 带入得: $$L \le b_1 + a_1 \times (k_)o + t \times B) \le R$$ $$L - b_1 - a_1 \times k_0 \le a_1 \times B \times t \le R - b_1 - a_1 \times k_0$$ $$\lceil \frac{L - b_1 - a_1 \times k_0}{a_1 \times B} \rceil \le t \le \lfloor \frac{R - b_1 - a_1 \times k_0}{a_1 \times B} \rfloor$$ 又因为 $t$ 为整数,所以可以直接解出 $t$ 的范围和个数。 注意边界的问题以及上下取整的问题。 ::::info[Code] ```cpp #include <bits/stdc++.h> using namespace std; #define int long long int T, a1, b1, a2, b2, A, B, C, l, r, x, y, AA, BB; int exgcd(int a, int b, int &x, int &y) { if (b == 0) { x = 1, y = 0; return a; } int x1, y1; int d = exgcd(b, a % b, x1, y1); x = y1; y = x1 - (a / b) * y1; return d; } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> a1 >> b1 >> a2 >> b2 >> l >> r; A = a1, B = -a2, C = b2 - b1; int d = exgcd(A, B, x, y); if (C % d != 0) { cout << 0 << '\n'; return 0; } x = x * (C / d), y = y * (C / d); A /= d, B /= d; AA = abs(A), BB = abs(B); x = (x % BB + BB) % BB, y = (C - A * d * x) / (B * d); if (y < 0) y = (y % AA + AA) % AA, x = (C - B * d * y) / (A * d); l = max(0LL, (l - b1 - a1 * x + a1 * BB - 1) / (a1 * BB)); r = floor(1.0 * (r - b1 - a1 * x) / (a1 * BB)); cout << max(r - l + 1, 0LL) << '\n'; return 0; } ``` :::: ### 练习题: 1. Billiard CodeForces - 982E ::::info[Code] ```cpp #include <bits/stdc++.h> using namespace std; using LL = long long; LL n, m, sx, sy, x, y, z, g, vx, vy; void A(LL a, LL b) { LL c = x / b - (x % b <= 0); x -= c * b, y -= c * a; } void C(LL a, LL b, LL c) { if (b) { C(b, a % b, c); if (g) { z = x, x = -y, y = -z - a / b * y; A(a / g, b / g); } } else if (c % a == 0) { g = a, x = c / a, y = 0; } } int main() { cin.tie()->sync_with_stdio(false); cin >> n >> m >> sx >> sy >> vx >> vy; if (!vx) { (sx == 0 || sx == n) ? (cout << sx << " " << (vy > 0 ? m : 0) << '\n') : (cout << -1 << '\n'); } else if (!vy) { (sy == 0 || sy == m) ? (cout << (vx > 0 ? n : 0) << " " << sy << '\n') : (cout << -1 << '\n'); } else { (vx < 0) && (vx = 0, sx = n - sx); (vy < 0) && (vy = 0, sy = m - sy); C(n, m, sx - sy); g ? (cout << (x & 1 ^ vx ? 0 : n) << " " << (y & 1 ^ vy ? 0 : m) << '\n') : (cout << -1 << '\n'); } return 0; } /* x , y >= 0 nx - ny = sx - sy */ ``` :::: 2. Line CodeForces - 7C ::::info[Code] ```cpp #include <bits/stdc++.h> using namespace std; using LL = long long; LL a, b, c, x, y, z, g; void A(LL a, LL b) { LL dx = b / g, dy = a / g, v = x / dx; x -= dx * v, y += dy * v; } void C(LL a, LL b) { if (b) { C(b, a % b); z = x, x = y, y = z - a / b * y; if (g) { A(a, b); } } else if (c % a == 0) { g = abs(a), x = -c / a; } } int main() { cin >> a >> b >> c; if (!a) { c % b == 0 ? (cout << "0 " << -c / b) : (cout << -1); } else if (!b) { c % a == 0 ? (cout << "0 " << -c / a) : (cout << -1); } else { C(a, b); g ? (cout << x << ' ' << y) : (cout << -1); } return 0; } ``` :::: 3. 荒岛野人 洛谷 - P2421 ::::info[Code] ```cpp #include <bits/stdc++.h> using namespace std; using LL = long long; const int kMaxN = 16; LL n, m, c[kMaxN], p[kMaxN], l[kMaxN], x, y, z, g; void A(LL a, LL b) { LL c = x / b - (x % b < 1); x -= c * b, y -= c * a; } void C(LL a, LL b, LL c) { if (b) { C(b, a % b, c); if (g) { z = x, x = -y, y = -z - a / b * y; A(a / g, b / g); } } else if (c % a == 0) { g = a, x = c / a, y = 0; } } int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> c[i] >> p[i] >> l[i]; m = max(m, c[i]); } for (;; m++) { bool b = 1; for (int i = 1; i <= n && b; i++) { for (int j = i + 1; j <= n && b; j++) { g = 0; C(p[i] - p[j], m, c[j] - c[i]); b &= !g || x > min(l[i], l[j]); } } if (b) { cout << m << '\n'; return 0; } } return 0; } /* 最 min(m) Ci + x * Pi = Cj + x * Pj (mod m) Ci + x * Pi - Cj - x * Pj - My = 0 (Pi - Pj) * x - My = Cj - Ci 无解 */ ``` ::::