#include<iostream>#include<algorithm>usingnamespace std;voidexgcd(int a,int b,int&x,int&y){if (!b){ x =1, y =0;return; }exgcd(b, a % b, y, x); y -= a / b * x;return;}intmain(){int n; cin >> n;while (n -- ){int a, b, x, y; cin >> a >> b;exgcd(a, b, x, y);printf("%d%d\n", x, y); }}
时间复杂度分析
线性同余方程
由
#include<iostream>#include<algorithm>usingnamespace std;voidexgcd(int a,int b,int&x,int&y){if (!b){ x =1, y =0;return; }exgcd(b, a % b, y, x); y -= a / b * x;}intmain(){int n; cin >> n;while (n -- ){int a, b, m; cin >> a >> b >> m;int x, y;exgcd(a, m, x, y);int gcd = a * x + m * y;if (b % gcd ==0) cout << (longlong)b / gcd * x % m << endl; else cout <<"impossible"<< endl; }}