Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10883 Accepted Submission(s):
4610
1 #include<stdio.h> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<math.h> 6 #include<string> 7 #define ll long long 8 #define inf 0x3f3f3f3f 9 using namespace std; 10 // ax + by = gcd(a,b) 11 ll exgcd(ll a, ll b, ll &x, ll &y)//扩展欧几里德定理 12 { 13 if(b==0)//终有一次a%b传进来是0,递归出口 14 { 15 x=1;y=0; 16 return a; 17 } 18 ll q=exgcd(b,a%b,y,x); 19 //最终递归出来,y1=1,x1=0 20 y=y-(a/b)*x; 21 //后面的y相当于下一个递归的x2,x相当于下一个递归的y2,符合推导公式 22 //x1=y2; y1=x2-[a/b]*y2; 23 return q; 24 } 25 26 int main() 27 { 28 ll a,b; 29 while(scanf("%lld %lld",&a,&b)!=EOF) 30 { 31 ll x,y; 32 ll gcd=exgcd(a,b,x,y); 33 if(gcd==1) 34 { 35 while(x<0) 36 { 37 x=x+b; 38 y=y-a; 39 } 40 printf("%lld %lld\n",x,y); 41 } 42 else printf("sorry\n"); 43 } 44 return 0; 45 }
原文:https://www.cnblogs.com/shoulinniao/p/10359297.html