Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=2669
Romantic
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 4179 Accepted Submission (s): 1745
Problem DescriptionThe Sky is Sprite.
The Birds is a Fly in the Sky.
The wind is Wonderful.
Blew Throw the Trees
Trees is Shaking, Leaves is falling.
Lovers Walk passing, and so is you.
................................ Write in 中文版 class by Yifenfei
Girls is clever and bright. In HDU every girl like math. Every girl like to solve math problem!
Now, you have 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, nonnegative integer A, b (0<a, b<=2^31)
Outputoutput nonnegative integer x and integer Y, if there is more answers than the X smaller one would be choosed. If no answer put "sorry" instead.
Sample Input77 5110 4434 79
Sample output2-3sorry7-3
Authoryifenfei: Expand Euclid Algorithm, Number theory content, simple proof, gcd (X,Y,&A,&B) required is Ax+by = gcd (x, y), in the case of GCD (Y,X%Y,A1,B1), a1y + B1x%y = gcd (x, y), with a1y + b1 (x (x/y) *y) = gcd (x, y) with a = B1, B = a1-b1 (x/y), then if written as gcd (Y,x%y, B1,A1) Then there is a = A1,b in recursive backtracking (b1-) A1 (x/y) then you can not do a re-processing, B = b-a (x/y) can this problem requires a positive number, then on the basis of x every time you add a group of numbers, so that x becomes larger, y becomes smaller, so that ax + by = k unchanged, set x = X+x ', y = Y+y '; Y ' = 0; So you can take x ' = b, y ' =-A; The following is the code:
1#include <cstdio>2#include <iostream>3 using namespacestd;4 #definell Long Long5 ll GCD (ll A, ll b)6 {7 returnb==0? A:GCD (b,a%b);8 }9ll EXTEND_GCD (ll X, ll y, ll &a, LL &b)Ten { One if(y==0){ AA =1; -b =0; - returnx; the } -ll d = extend_gcd (y, x percenty, B, a); -B = B-(x/y) *A; - returnD; + } - intMain () + { A ll A, B; at while(~SCANF ("%i64d%i64d",&a,&b)) - { - if(GCD (A, b)! =1) -Puts"Sorry"); - Else { - ll x, y; in EXTEND_GCD (a,b,x,y); - while(x<=0){ tox+=b; +y-=A; - } theprintf"%i64d%i64d\n", x, y); * } $ }Panax Notoginseng return 0; -}
hdu_2669 Romantic (Extended Euclid)