拓展欧几里得

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

void exgcd(int a,int b,LL &x,LL &y){
    if(b==0){x=1, y=0; return;}
    LL tx,ty;
    exgcd(b,a%b,tx,ty);
    x=ty;
    y=tx-(a/b)*ty;
}

int main(){
    LL a,b,x,y;
    cin>>a>>b;
    exgcd(a,b,x,y);
    x=(x%b+b)%b;
    cout<<x<<endl;
    return 0;
}

原文地址:https://www.cnblogs.com/codetogether/p/7214140.html