Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2669
Problem descriptionthe 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.
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 5110 4434 79
Sample output
2 -3sorry7 -3
Authoryifenfei
Sourcehdu girls' session Open-who said women are not as good as men?
PS:
Extended Euclidean template questions;
Appendix: Extended Euclidean explanation: http://blog.csdn.net/u012860063/article/details/39760003
The Code is as follows:
# Include <cstdio> # include <cstring> # include <cmath> typedef _ int64 ll; ll exgcd (ll a, LL B, ll & X, ll & Y) {If (B = 0) {x = 1; y = 0; return a;} else {ll r = exgcd (B, A % B, x, y ); ll T = x; X = y; y = T-A/B * Y; return r ;}int main () {ll a, B, X, Y; while (scanf ("% i64d % i64d", & A, & B )! = EOF) {ll d = exgcd (a, B, x, y); If (1% d) // printf ("Sorry \ n") cannot be divisible "); else {While (x <0) x + = B, Y-= A; printf ("% i64d % i64d \ n", x, y) ;}} return 0 ;}
HDU 2669 romantic (Extended Euclidean)