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.
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.
InputThe input contains multiple test cases.
Each case two nonnegative integer a,b (0<a, b<=2^31)
Outputoutput 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
-3 2 Sorry . 7 -3
subject of the pit at the output to be positive ,, x ,, y must be negative, consider also makes sense because X * a + Y * b = 1. a, b, Therefore, x and Y are both greater than 1 to have a less than 0, a is greater than 0, the result will be equal to 1
// directly determined by the formula x and expand Euclidean y, divided by a, b of the greatest common divisor of q, if still is an integer, the output, otherwise, x + = b, y- = a, then judge # the include <the iostream> #include <cstdio> #include <algorithm> the using namespace STD; typedef Long Long LL; LL X, Y; LL exgcd (A LL, LL B) { IF (B == 0 ) { X = . 1 ; Y = 0 ; return A; } LL R & lt = exgcd (B, A% B); LL T = Y; Y = X-(A / B) * Y; X =t; return r; } int main(){ ll a,b; while(scanf("%lld%lld",&a,&b)!=EOF) { ll r=exgcd(a,b); if(r!=1){ puts("sorry"); continue ; } ll x1=x; ll y1=y; x1=(x1+b)%b; // while(x1<0){ // x+=b/r; // } // while(y1>0){ // y1-=a/r; // } y1=(y1-a)%a; printf("%lld %lld\n",x1,y1); } return 0; }