對於ax+by=c,求得使x爲最小得非負整數。

#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int N = 1e5+10;
const int mod = 10000;
const ll inf = 10000000000000000;

//1
int random(int n){
    return (ll)rand()*rand()%n;
}

inline int read(){
	int f=1,num=0;char ch=getchar();
	while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(0!=isdigit(ch)) num=(num<<1)+(num<<3)+ch-'0',ch=getchar();
	return num*f;
}

void exgcd(ll a,ll b,ll &x,ll &y){
    if(b == 0ll){
        x = 1ll;y = 0ll;
        return;
    }
    exgcd(b,a%b,x,y);
    ll t = x;
    x = y;
    y = t-a/b*y;
}

ll gcd(ll n,ll m){
    ll r = n%m;
    while(r){
        n = m;m = r;r = n%m;
    }
    return m;
}
int main(){
    ll a,b,c,x,y;
    while(cin >> a >> b >> c){
        exgcd(a,b,x,y);
        ll t = gcd(a,b);
        if(c%t){
            printf("Impossible\n");
        }else {
            x = x*c/t;y = y*c/t;

            cout<<x<<" "<<y<<endl;
            ll p = (b/gcd(a,b));
            x = x%p;
            cout<<(x+p)%p<<endl;
        }
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章