扩展欧几里得

 877. 扩展欧几里得算法 - AcWing题库

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define ll long long
#define PII pair
#define TUP tuple
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;

int gcd(int n, int m, int& x, int& y) {
    if (m == 0) {
        x = 1, y = 0;
        return n;
    }
    int d = gcd(m, n % m, y, x);
    y -= n / m * x;
    return d;
}

void solve() {
//换行是个好习惯
    int n, m;
    cin >> n >> m;
    int x, y;
    //if (n < m) swap(n, m);
    gcd(n, m, x, y);
    cout << x << " " << y << endl;
    return;
}

int main() {
    int t = 1;
    scanf("%d", &t);
    while (t--) {
        solve();
    }
    return 0;
}

你可能感兴趣的:(数学知识,数论)