Original question:
Description
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 The original problem removed the useless pictures and texts .... Analysis: Very intuitive template of the extension gcd, directly to A*x+b*y=1 can be, note the size of the number of more than int, with the __int64 code:
#include <stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>__int64 exgcd (__int64 a,__int64 b,__int64&x,__int64 &y) { if(b==0) {x=1; y=0;returnA;} Else{__int64 D=EXGCD (b,a%b,x,y); __int64 T=x; X=y; Y=t-a/b*y; returnD; }}intMain () {__int64 a,b,x,y; while(SCANF ("%i64d%i64d", &a,&b)! =EOF) {__int64 D=EXGCD (a,b,x,y); if(1%d) puts ("Sorry"); Else { while(x<0) x+=b,y-=A; printf ("%i64d%i64d\n", x, y); } } return 0;}
HDU 2669 Romantic Problem Solving experience