扩展的欧几里德求整数解
#include<bits/stdc++.h>
#define ll long long
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) a*b/(__gcd(a,b))
using namespace std;
//priority_queue <int,vector<int>,less<int> > Q;
ll qpow(ll a,ll x){ll ans=1;while(x>0){if(x&1){ans=(ans*a);}a=(a*a);x/=2;}return ans;}
const double eps = 1e-7;
const ll inf= 0x3f3f3f3f3f3f3f3f;
const int N = 1e5+5;
const int mod = 1e9+7;
int exgcd(int a,int b,int &x,int &y){
if(b == 0){x=1;y=0;return a;};
int t = exgcd(b,a%b,x,y);
int x0 = x, y0 = y;
x = y0;
y = x0 - (a/b)*y0;
return t;
}
void solve(){
int x,y;
int a=6,b=9,c = 6; //求3x + 9y = 6
int d = gcd(a,b);
if(c % d !=0 ){
printf("无整数解\n");
return ;
}
a /= d; b/=d; c/=d; //用扩展欧几里得 求ax + by = gcd(a,b)
exgcd(a,b,x,y);
x = x*c;
y = y*c;
cout << x <<' ' << y << endl; //一种解
//通解 x + k*b
// y - k*a
// k是整数
}
int main(){
solve();
return 0;
}