1012 【模板】同余方程

 

分析

模板题

#include<bits/stdc++.h>
using namespace std;

#define int long long

int gcd_ex(int a,int b,int &x,int &y) {
    if(b == 0) {x = 1,y = 0;return a;}
    int d = gcd_ex(b,a%b,y,x);
    y = y - (a / b) * x;
    return d;
}
signed main() {
    int t;cin>>t;while(t -- ) {
        int x,y;
        int a,b;cin>>a>>b;
        int d = gcd_ex(a,b,x,y);
        x = (x % b + b) % b;
        if(d == 1) cout<<x<<endl;
        else cout<<-1<<endl;
    }
}

 

posted @ 2022-07-26 04:51  er007  阅读(23)  评论(0编辑  收藏  举报