Description
The 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.
Input
The input contains multiple test cases.
Each case, nonnegative integer A, b (0<a, b<=2^31)
Output
Output 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 Input
77 5110 4434 79
Sample Output
2-3sorry7-3
Problem Solving Ideas:The main idea
is to input two numbers, a, b let us find the satisfying equation x*a + y*b = 1 x, y if not found on the output sorry, where x is nonnegative, Y is an integer. This topic is actually an extension of the Euclidean theorem of the template title, we directly use the extension Euclidean theorem can be solved.
Program code:
#include <iostream> #include <cstdio>using namespace STD;//X*A+Y*B=GCD (A, B) =gcd (b,a%b) =x*b+y* (a%b) =x*b+ y* (a-a/b*b) =y*a+x*b-a/b*y*b=y*a+ (x-a/b*y) *b//to get a one-time division after X=y;y=x-a/b*y;int extend_euclid (int a,int b,int &x,int &y) //extended Euclidean, can use 64 bits as far as 64 bits with 32 bit possible over range { int res, TMP; if (!b) {X=1;y=0;return A;} res = Extend_euclid (b, a%b,x, y); TMP = X-A/B*Y;X = Y;y = tmp; return res; }int Main () { int a,b,x,y,z;while (scanf ("%d%d", &a,&b)!=eof) {z=extend_euclid (a,b,x,y); Z is a, B, greatest common divisor if (z==1) { while (x<0) { x=x+b/1; Here, X is the least nonnegative integer (x can be 0) Y=Y-A/1; Use A*x+b*y=1 to get y } cout<<x<< "" <<y<<endl;} else cout<< "Sorry" <<ENDL;} return 0;}
ACM Linear equation