hdu 2669 Romantic (乘法逆元)

Romantic

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2835    Accepted Submission(s): 1117


Problem Description
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

 

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
77 51
10 44
34 79
 
 
Sample Output
2 -3
sorry
7 -3
 
Author
yifenfei
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  2668 2674 2671 2670 2672 
 

WA了几次,郁闷。乘法逆元模板题。注意x要为正数。

 1 //15MS    248K    584 B    C++
 2 #include<stdio.h>
 3 __int64 extend_euclid(__int64 a,__int64 b,__int64 &x,__int64 &y)
 4 {
 5     if(b==0){
 6         x=1;y=0;
 7         return a;
 8     }
 9     __int64 ans=extend_euclid(b,a%b,x,y);
10     __int64 temp=x;
11     x=y;
12     y=temp-a/b*y;
13     return ans;
14 }
15 int main(void)
16 {
17     __int64 a,b,x,y;
18     while(scanf("%I64d%I64d",&a,&b)!=EOF)
19     {
20         __int64 flag=extend_euclid(a,b,x,y);
21         if(flag==1){
22             while(x<0){
23                 x+=b;y-=a;
24             }
25             printf("%I64d %I64d
",x,y);
26         }else puts("sorry");
27     }
28     return 0;
29 }
原文地址:https://www.cnblogs.com/GO-NO-1/p/3783242.html