Zhao70's Blog

linear_equation

看了半天各种大手子解析, 也许是数学基础不好吧,熟练背诵吧。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std;
int exgcd(int a, int b, int& x, int& y)
{
if(b == 0)
{
x = 1;
y = 0;
return a;
}
int r = exgcd(b, a % b, x, y);
int t = x;
x = y;
y = t - (a / b) * y;
return r;
}
bool linear_equation(int a, int b, int c, int &x, int &y)
{
int d = exgcd(a, b, x, y);
if(c % d)
return false;
int k = c / d;
x *= k; y *= k;
return true;
}
int main(int argc, char const *argv[])
{
int x, y;
if(linear_equation(47, 30, 1, x, y))
cout << x << " " << y << endl;
return 0;
}