扩展的欧几里德求整数解 (纯代码)

扩展的欧几里德求整数解

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值