HDU 2669 Romantic( 拓欧水 )


**链接:****传送门 **

题意:求解方程 X * a + Y * b = 1 的一组最小非负 X 的解,如果无解输出 "sorry"

思路:裸 exgcd


/*************************************************************************
    > File Name: hdu2669.cpp
    > Author:    WArobot 
    > Blog:      http://www.cnblogs.com/WArobot/ 
    > Created Time: 2017年05月21日 星期日 18时34分40秒
 ************************************************************************/

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

#define ll long long
ll exgcd(ll a,ll b,ll &x,ll &y){
	if( b == 0 ){
		x = 1 ; y = 0 ; return a;
	}
	ll d = exgcd( b , a%b , x , y );
	ll tmp = x;
	x = y;	 y = tmp - a/b*y;
	return d;
}
int main(){
	ll a , b , x , y;
	while(~scanf("%lld%lld",&a,&b)){
		ll d = exgcd( a , b , x , y);
		ll c = 1;
		if( c % d != 0 )	printf("sorry
");
		else{
			x = x * (c/d);
			x = ( x%(b/d) + b/d )%(b/d);
			printf("%lld %lld
",x,(1-a*x)/b);
		}
	}
	return 0;
}
原文地址:https://www.cnblogs.com/WArobot/p/6885586.html