扩欧的原理就不多讲了,具体讲讲程序
由原理可知 gcd(a,b,x1,y1) = gcd(b,a%b,y2,x2-(a/b)*y2)
递归求出y2 然后 x1 = y2
求出x2 然后 y1 = x2 再 y1 -= (a/b)*y2 就可以了
上代码
#include<iostream> using namespace std; int gcd(long long a,long long b,long long &x,long long &y){ if(b==0){ x=1;y=0;return a; } int q = gcd(b,a%b,y,x); y-=a/b*x; return q; } int main(){ long long a,b; long long x=0,y=0; cin>>a>>b; gcd(a,b,x,y); cout<<x<<y; }