lynlovehyl

终于过了,通解之类的也搞出来了,继续努力吧

算法模板

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 //扩展欧几里得算法,求解ax+by=gcd(a,b)的一组解x1y1
 6 void exgcd(int a, int b, int &x, int &y)
 7 {
 8     if(!b)//为gcd(a,0)的情况
 9     {
10         x = 1;
11         y = 0;
12         return;
13     }
14     exgcd(b, a % b, x , y);
15     int t = x;
16     x = y;
17     y = t - a / b * y;
18 }
19 //辗转相除求余数法
20 int gcd(int a, int b)
21 {
22    if(b == 0)
23         return a;
24     return gcd(b, a % b);
25 }
26 int main()
27 {
28     int a = 47,  b = 30;
29     int c = 1;
30     int r = gcd(a, b);
31     if(c % r)
32         cout << "no";
33     a /= r;
34     b /= r;
35     c /= r;
36     int x1, y1;
37     exgcd(a, b, x1, y1);
38     cout << x1 << " " << y1 << endl;
39     return 0;
40     //通解x=c*x1+b*t,y=c*y1-a*t
41 }
View Code
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 using namespace std;
 6 //扩展欧几里得算法,求解ax+by=gcd(a,b)的一组解x1y1
 7 void exgcd(long long a, long long b, long long &x, long long &y)
 8 {
 9     if(!b)//为gcd(a,0)的情况
10     {
11         x = 1;
12         y = 0;
13         return;
14     }
15     exgcd(b, a % b, x , y);
16     long long t = x;
17     x = y;
18     y = t - a / b * y;
19 }
20 long long gcd(long long a, long long b)
21 {
22    if(b == 0)
23         return a;
24     return gcd(b, a % b);
25 }
26 int main()
27 {
28     long long x, y, m, n, l;
29     while(cin >> x >> y >> m >> n >> l)
30     {
31         long long a = n - m, b = l, c = x - y;
32         long long r = gcd(a, b);
33         if(c % r != 0)
34         {
35             cout << "Impossible" << endl;
36             continue;
37         }
38         a /= r;
39         b /= r;
40         c /= r;
41         long long x1, y1;
42         exgcd(a, b, x1, y1);
43         long long t = c * x1 / b;
44         x1 = c * x1 - b * t;
45         if(x1 < 0)
46             x1 += b;
47         cout << x1 << endl;
48     }
49     return 0;
50 }

 

分类:

技术点:

相关文章: