版本比较
扩展欧几里得
正文
增加 13 行删除 13 行
22 行未改动232425262727282930312 行未改动3435363738383940414215 行未改动5859606162626364656667676869707122 行未改动949596979898991001011029 行未改动11211311411511611711611711811912012120 行未改动14214314414514614614714814915017 行未改动16816917017117217217317417517611 行未改动18818919019119219219319419519617 行未改动21421521621721821821922022122222322422522622722822822923023123254 行未改动287288289290291291292293294295188 行未改动全部展开22 行未改动那么我们可以先对此进行一次辗转相除:
$$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$$2 行未改动$$a \bmod b = a - \lfloor \frac{a}{b} \rfloor \times b$$
推出
$$\begin{aligned} $$\begin{aligned} 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}$$15 行未改动过程:
1. 裴蜀定理判断是否有解:>定理内容:对于正整数 $a,b$,$a\times x+b\times y=c$ 有解的充要条件是 $\gcd(a,b) | c$。2. 对于 exgcd 方法求出的一组特解,需要乘上一个系数 $c \div d$。 2. 对于 exgcd 方法求出的一组特解,需要乘上一个系数 $c \div d$。
2. 如果我们需要求出最小的非负整数解,我们就需要使得 $a = a \div d$,$b = b \div d$,然后使用这两个系数对求出的特解 $x$,$y$ 进行取模。
- 解同余方程
同余方程的一般形式为:$a \times x \equiv c (\bmod b)$。
我们同样可以直接将同余方程转化为上面那种方式求解。
22 行未改动注意判断无解和最终求答案的过程。
::::info[Code]```cpp#include <bits/stdc++.h> #include <bits/stdc++.h>using namespace std;#define int long longstring s, t;int a, b, x, y, mod = 19260817;9 行未改动 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; 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;20 行未改动需要注意的一点是 $m - n$ 和 $y - x$ 可能为负数,需要在为负数的时候调换成正数。
::::info[Code]```cpp#include <bits/stdc++.h> #include <bits/stdc++.h>using namespace std;#define int long longint x, y, m, n, l, xx, yy;int exgcd(int a, int b, int &x, int &y) {17 行未改动 return 0; } xx = xx * (c / d); b /= d; cout << (xx % b + b) % b << '\n'; cout << (xx % b + b) % b << '\n'; return 0;}```::::11 行未改动注意判断无解和无正整数解的情况。
::::info[Code]```cpp#include <bits/stdc++.h> #include <bits/stdc++.h>using namespace std;#define int long longint T, a, b, c, x, y;int exgcd(int a, int b, int &x, int &y) {17 行未改动 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;}```
54 行未改动注意边界的问题以及上下取整的问题。
::::info[Code]```cpp#include <bits/stdc++.h> #include <bits/stdc++.h>using namespace std;#define int long longint T, a1, b1, a2, b2, A, B, C, l, r, x, y, AA, BB;int exgcd(int a, int b, int &x, int &y) {188 行未改动全部展开