You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
python/TangDou/AcWing/NumberTheory/扩展欧几里得解不定方程的步骤.cpp

41 lines
1.4 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
//扩展欧几里得
LL exgcd(LL a, LL b, LL &x, LL &y) {
if (!b) {
x = 1, y = 0;
return a;
}
LL d = exgcd(b, a % b, y, x);
y -= a / b * x;
return d;
}
int main() {
/* 原始方程 4x+6y=10 求x,y的一组解
a=4,b=6,c=10
1. d=gcd(a,b)=gcd(4,6)=2
2. 2|c 10%2=0
3. a/d*x+b/d*x=c/d 4/2*x+6/2*y=10/2
=> 2x+3y=5
4.
ax+by=gcd(a,b)!
2x+3y=1
2x+3y=5!!
x0,y02x+3y=1
x1=5*x0,y1=5*y0!!!!
2x+3y=1 5
2*(x*5)+3*(y*5)=5
x1=x0*5,y1=y0*5~
*/
LL x, y;
exgcd(2, 3, x, y);
printf("2x+3y=1 -> x=%lld y=%lld\n", x, y);
printf("2x+3y=5 -> x=%lld y=%lld", 5 * x, 5 * y);
return 0;
}