Romantic(扩展欧几里得)

分手后的思念是犯贱 2022-06-17 08:37 268阅读 0赞

The 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

C175-1002-1.jpg

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.

Input

The input contains multiple test cases.
Each case two nonnegative integer a,b (0<a, b<=2^31)

Output

output 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

  1. 77 51
  2. 10 44
  3. 34 79

Sample Output

2 -3

sorry

7 -3

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<algorithm>
  4. using namespace std;
  5. __int64 x,y;
  6. __int64 exgcd(__int64 a,__int64 b,__int64 &x,__int64 &y)
  7. {
  8. if(b==0)
  9. {
  10. x=1;
  11. y=0;
  12. return a;
  13. }
  14. __int64 r=exgcd(b,a%b,x,y);
  15. __int64 temp=x;
  16. x=y;
  17. y=temp-a/b*y;
  18. return r;
  19. }
  20. int main()
  21. {
  22. __int64 n,m;
  23. while(~scanf("%I64d %I64d",&n,&m))
  24. {
  25. x=y=0;
  26. __int64 gys=exgcd(n,m,x,y);
  27. if(gys!=1)
  28. printf("sorry\n");
  29. else
  30. {
  31. if(x<0)
  32. {
  33. x+=m;//因为为负数
  34. y-=n;
  35. }
  36. printf("%I64d %I64d\n",x,y);
  37. }
  38. }
  39. return 0;
  40. }

发表评论

表情:
评论列表 (有 0 条评论,268人围观)

还没有评论,来说两句吧...

相关阅读

    相关 扩展算法

    问题描述: 求解二元一次方程ax+by=c。 问题分析: 上述的二元一次方程可以用同余方程来进行描述:ax≡cmod(b) 两个问题可以进行转换,但是都可以用扩展的欧几

    相关 扩展

    GCD 定义 欧几里德算法又称辗转相除法,是指用于计算两个正整数a,b的最大公约数。 结论与证明 对于a,b两个正整数(a>b),gcd(a,b)=gcd(a