扩展欧几里得最小整数解

我们先讨论a,b,c都大于0的情况 (都为整数)

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. int exgcd(int a,int b,int &x,int &y)  
  2. {  
  3.     int d=a;  
  4.     if(b!=0)  
  5.     {  
  6.         d=exgcd(b,a%b,y,x);  
  7.         y-=(a/b)*x;  
  8.     }  
  9.     else  
  10.     {  
  11.         x=1;  
  12.         y=0;  
  13.     }  
  14.     return d;  
  15. }  


可以用扩展欧几里德算出 一个特解 x0 , y0; 

满足下列方程

 a*x0+b*y0=gcd(a,b);

如果c%gcd==0  那么此方程有解,否则没有解

若有解

方程两边同时乘以   c/gcd(a,b)  得    (a*c/gcd(a,b))*x0+(b*c/gcd(a,b))*y0=c;

这时得出方程的一个解   x1=x0*c/gcd(a,b)     y1=y0*c/gcd(a,b) 

求最小整数解 意思把x1变到减少到不能减少为止  也就是把x0 减少到不能减少为止

若x0减小x,那么方程左边 整体会减少  (a*c/gcd(a,b))*x   此时 y0 需要增加相应的数使得等式平衡

而假设 y0增加了y  总体增加了  (b*c/gcd(a,b))*y  此时 (a*c/gcd(a,b))*x==(a*c/gcd(a,b))*y

而且x,y为整数  我们可以得到  x/y==b/gcd(a,b)  /   a/gcd(a,b) 

这时  x每次减少 b/gcd(a,b)   y只需增加 a/gcd(a,b)  就可以使得等式平衡。  那为什么我们不约掉gcd(a,b)?

因为x越小,我们得到的最小整数解就会越小。。。。

这时我们让x0不断减 x  (x=b/gcd(a,b)) 直到    x0-i*x>=0 && x0-(i+1)*x<0  (i为减x的次数) 这时得到的就是最小整数解

我们可以让 outcome=x0%x  这时就是答案了(等等 我们又错过了什么?)

假如x0为负数怎么办?  所得outcome=x0%x   为负数(c语言的规则) 这时我们只需要x0+x就可以得到正数了

综上所述:我们可以把x0为正,为负的情况综合起来,得到的表达式如下:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. int main()  
  2. {  
  3.      d=exgcd(a,b,x,y);  
  4.      if(c%d==0)  
  5.      {   
  6.           x*=c/d;   
  7.              t=b/d;  
  8.          x=(x%t+t)%t;  
  9.          printf("%d\n",x);   
  10.      }  
  11. }  

终于来到正题了。。。a,b,c可以为负数,怎么办?这时gcd(a,b) 都可能是负数

其实。。。。。。。

当t<0 时  我们把  t=-t  然后按照上面的方法做就行了。。。。。。。。。。。(没理由。。。尴尬敲打

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include<cstdio>  
  2. #include<cstring>  
  3. using namespace std;  
  4. #define LL long long  
  5. LL exgcd(LL a,LL b,LL &x,LL &y)  
  6. {  
  7.     LL d=a;  
  8.     if(b!=0)  
  9.     {  
  10.         d=exgcd(b,a%b,y,x);  
  11.         y-=(a/b)*x;  
  12.     }  
  13.     else  
  14.     {  
  15.         x=1;  
  16.         y=0;  
  17.     }  
  18.     return d;   
  19. }  
  20. int main()  
  21. {  
  22.     LL a,b,c,d,x,y;  
  23.     while(~scanf("%lld%lld%lld",&a,&b,&c))  
  24.     {  
  25.         if(a==0&&b==0)  
  26.         {  
  27.             if(c==0)  
  28.             printf("0\n");  
  29.             else  
  30.             printf("-1\n");  
  31.             continue;   
  32.         }  
  33.         if(a==0)  
  34.         {  
  35.             if(c%b==0)  
  36.             printf("0\n");  
  37.             else  
  38.             printf("-1\n");  
  39.             continue;             
  40.         }  
  41.         if(b==0)  
  42.         {  
  43.             if(c%a==0&&c/a>=0)  
  44.             printf("%lld\n",c/a);  
  45.             else  
  46.             printf("-1\n");  
  47.             continue;  
  48.         }  
  49.         d=exgcd(a,b,x,y);  
  50.         if(c%d!=0)  
  51.         {  
  52.             printf("-1\n");  
  53.         }  
  54.         else  
  55.         {  
  56.             x*=c/d;  
  57.             LL t=b/d;  
  58.             if(t<0)  
  59.             t=-t;  
  60.             x=(x%t+t)%t;  
  61.             printf("%lld\n",x);  
  62.         }  
  63.     }   
  64. }  
下面附上一道题目:

扩展欧几里德

Time Limit: 1000ms
Memory Limit: 65536KB
64-bit integer IO format:  %lld      Java class name:  Main

Sample Input

1 1 1

Sample Output

0

猜你喜欢

转载自blog.csdn.net/qq_20200047/article/details/71159677