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 Test instructions: give you A and B, ask you to ask for Ax+by=1 X and Y, ask for x is a positive integer, y is an integer ... There is the output, there is no output sorry. The puzzle: The extended Euclidean algorithm is used here. First, using the extended Euclidean algorithm to find X, Y and then determine whether it is greater than 0, if less than 0, through the loop +b until x>0, in the output answer here to give two kinds of code, one is the book Extension Euclid program, one is online (they say is template) ..... The code is as follows: (the main function of the last annotation is to start on its own, but does not consider when X is less than 0, so WA is)
1#include <stdio.h>2 voidgcdintAintBint&d,int&x,int&y)3 {4 if(!b)5 {6D=A;7x=1;8y=0;9 }Ten Else One { AGCD (b,a%b,d,y,x); - //printf ("a=%d b=%d d=%d y=%d x=%d\n", a,b,d,y,x); -Y-=x* (A/b); the //printf ("y=-%d* (%d/%d)%d \ n", x,a,b,y); - } - } - + intMain () - { + inta,b,d,x,y; A while(SCANF ("%d%d", &a,&b) = =2) at { - gcd (a,b,d,x,y); - if(d==1)//D=GCD (A, B), D is a, B, greatest common divisor. - { - if(x>0) -printf"%d%d\n", x, y); in Else - { to while(x<=0)//ask for additional solutions such as: 5x+6y=1 First Solution: X=-1,y=1 the second kind of x=5 y=-4 + {//Here are counted by X=x+b and y=y-a. is equal to (x+b) *a+ (y-a) *b The final formula result is still unchanged -x+=b; they-=A; * } $printf"%d%d\n", x, y);Panax Notoginseng } - } the Else +printf"sorry\n"); A } the return 0; + } - /*int main () $ { $ int a,b,d,x,y; - while (scanf ("%d%d", &a,&b) ==2) - { the gcd (a,b,d,x,y); - //printf ("%d%d%d\n", x,y,d);Wuyi if (x%d==0&&y%d==0&&x/d>0) the printf ("%d%d\n", x/d,y/d); - Else Wu printf ("sorry\n"); - } About return 0; $ }*/
On-line:
1#include <cstdio>2 using namespacestd;3 intEXGCD (intAintBint&x,int&y)4 {5 if(b==0)6 {7x=1;8y=0;9 returnA;Ten } One intR=EXGCD (b,a%b,x,y); A //printf ("a=%d b=%d\n", A, b); - intt=x; - //printf ("t=%d\n", x); thex=y; - //printf ("x=%d\n", y); -y=t-a/b*y; - //printf ("y=%d\n", y); + returnR; - } + intMain () A { at inta,b,x,y,m; - while(SCANF ("%d%d", &a,&b)! =EOF) - { - -m=EXGCD (a,b,x,y); - if(m==1) in { - while(x<0) to { +x+=b; -y-=A; the } *printf"%d%d\n", x, y); $ }Panax Notoginseng - Else theprintf"sorry\n"); + } A return 0; the}
HDU 2669 The first six weeks I question