|
1 |
| -void exgcd(int a, int b, int& x, int& y) |
| 1 | +#include <cstdio> |
| 2 | +#define int long long |
| 3 | + |
| 4 | +int exgcd(int a, int b, int& x, int& y) |
2 | 5 | {
|
3 |
| - if (b == 0) return (void)(x = 1, y = 0); |
4 |
| - exgcd(b, a % b, y, x), y -= (a / b) * x; |
| 6 | + if (b == 0) return x = 1, y = 0, a; |
| 7 | + int ret = exgcd(b, a % b, y, x); |
| 8 | + y -= (a / b) * x; |
| 9 | + return ret; |
| 10 | +} |
| 11 | + |
| 12 | +bool linearEquation(int a, int& x, int b, int& y, int c) |
| 13 | +{ |
| 14 | + int g = exgcd(a, b, x, y); |
| 15 | + if (c % g) return false; |
| 16 | + int scale = c / g; |
| 17 | + x = x * scale; |
| 18 | + y = y * scale; |
| 19 | + // x minimum positive root |
| 20 | + int mod = b / g; |
| 21 | + x = x % mod; |
| 22 | + if (x <= 0) x += mod; |
| 23 | + y = (c - a * x) / b; |
| 24 | + return true; |
| 25 | +} |
| 26 | + |
| 27 | +int LinearModEquation(int a, int& x, int b, int p) |
| 28 | +{ |
| 29 | + int y; |
| 30 | + linearEquation(a, x, p, y, b); |
| 31 | + return x % p; |
| 32 | +} |
| 33 | + |
| 34 | +int a, b; |
| 35 | +signed main() |
| 36 | +{ |
| 37 | + scanf("%lld%lld",&a,&b); |
| 38 | + int x, y; |
| 39 | + linearEquation(a, x, b, y, 1); |
| 40 | + printf("%lld\n", x); |
| 41 | + return 0; |
5 | 42 | }
|
0 commit comments