Romantic

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3660    Accepted Submission(s): 1493


Problem Description
The Sky is Sprite.
The Birds is Fly in the Sky.
The Wind is Wonderful.
Blew Throw the Trees
Trees are Shaking, Leaves are Falling.
Lovers Walk passing, and so are You. 
................................Write in English class by yifenfei

 

Girls are clever and bright. In HDU every girl like math. Every girl like to solve math problem!
Now tell you two nonnegative integer a and b. Find the nonnegative integer X and integer Y to satisfy X*a + Y*b = 1. If no such answer print "sorry" instead.
 

Input
The input contains multiple test cases.
Each case two nonnegative integer a,b (0<a, b<=2^31)
 

Output
output nonnegative integer X and integer Y, if there are more answers than the X smaller one will be choosed. If no answer put "sorry" instead. 
 

Sample Input
77 51 10 44 34 79
 

Sample Output
2 -3 sorry 7 -3
 

Author
yifenfei
 

Source
 

Recommend
lcy

#include<cstdio>
#include<iostream>
#define ll long long int  
using namespace std;
ll gcd(ll &x, ll &y, ll a, ll b)
{
	if (b == 0)
	{
		x = 1;
		y = 0;
		return a;
	}
	ll r = gcd(x, y, b, a%b), t;
	t = x;
	x = y;
	y = t - (a / b)*y;
	return r;
}
int main()
{
	ll a, b;
	while (cin >> a >> b)
	{
		ll x, y;
		ll g = gcd(x, y, a, b);
		if (g == 1)
		{
			if (x > 0)
			{
				printf("%lld %lld\n", x, y);
			}
			else
			{
				printf("%lld %lld\n", (x%b + b) % b, (y%a - a) % a);
			}
		}
		else
		{
			cout << "sorry" << endl;
		}
	}
	return 0;
}




这里呢主要是注意输出有一个条件就是x要是一个非负整数;

对于这种情况,首先我们要进行一次判断:若x>0;则直接输出;若x<0,则需要进行一些变化;

因为当这种情况下成立时a(x+bn)+b(y-an)=1也是成立的;

于是我们进行变换;x=(x%b+b)%b;y=(y%a-a)%a;就行了;