不定方程与扩展欧几里得

时间不多了,先把代码放上来。

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 
 5 using namespace std;
 6 
 7 typedef long long ll;
 8 
 9 ll gcd(ll a, ll b)
10 {
11     return (b == 0)?a:(gcd(b, a%b));
12  } 
13 
14 ll exgcd(ll a, ll b, ll &x, ll &y)
15 {
16     if(b == 0){
17         x = 1; y = 0;
18         return a;
19     }
20     ll t = exgcd(b, a%b, y, x);
21     y -= (a/b)*x;
22     
23     return t;
24  } 
25 
26 int main()
27 {
28     ll x, y;
29     ll a, b, m;
30     cin>>a>>b>>m;
31     // 有解的充要条件 
32     if(m%gcd(a, b) == 0) cout<<"Yes\n"; 
33     ll s = exgcd(a, b, x, y);
34     x = x*m/s;
35     y = y*m/s;
36     for(int t = -100;t<=300;t++){
37         printf("%lld*(%lld) + %lld*(%lld) = %lld\n", a, x, b, y, m);
38         /*
39         x -= b/s;
40         y += a/s;
41         */
42         // 通解, 可往左右走 
43         x -= b/s;
44         y += a/s;
45     }
46     
47     return 0;
48 }

 

posted @ 2019-03-10 10:54  maybeTang  阅读(179)  评论(0编辑  收藏  举报