重温 + 深入理解扩展欧几里德

欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数  gcd(a,b)由递推所得,自然需要初始值和递推式

核心代码

int gcd( int a, int b )
{
    while( b != 0 )
    {
        int t = b;
        b = a%b;
        a = t;
    }
    return a;
}

 

 

 

扩展欧几里得算法,求不定方程。

对于不定方程ax+by=c,求满足其方程的整数解。
扩展欧几里得能对ax+by=gcd(a,b)计算所有x,y的解,只有满足c%gcd(a,b)==0时,ax+by=c才有整数解,当然解由ax+by=gcd(a,b)计算结果 * (c/gcd(a,b))得到。

 

推理:

1,显然当 b=0,gcd(a,b)=a。此时 x=1,y=0;

2,a>b>0 时

设 ax1+ by1= gcd(a,b);

bx2+ (a mod b)y2= gcd(b,a mod b);

根据朴素的欧几里德原理有 gcd(a,b) = gcd(b,a mod b);

则:ax1+ by1= bx2+ (a mod b)y2;

即:ax1+ by1= bx2+ (a - [a / b] * b)y2=ay2+ bx2- [a / b] * by2;

也就是ax1+ by1 == ay2+ b(x2- [a / b] *y2);

根据恒等定理得:x1=y2; y1=x2- [a / b] *y2;

这样我们就得到了求解 x1,y1 的方法:x1,y1 的值基于 x2,y2.

上面的思想是以递归定义的,因为 gcd 不断的递归求解一定会有个时候 b=0,所以递归可以结束

这样我们得到ax+by=gcd(a,b)的一组解   ax+by=gcd(a,b)计算结果 * (c/gcd(a,b))得到ax + by = c的解

 

计算得到ax+by=c的解,只是ax+by=c的其中的一个解,而其他值满足:
x(i) = x + b/Gcd(a, b) * t 
y(i) = y - a/Gcd(a, b) * t             (其中t为任意整数,两者需同时进行) 

 

 

经典题目  青蛙的约会

两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面。它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止。可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特征,也没有约定见面的具体位置。不过青蛙们都是很乐观的,它们觉得只要一直朝着某个方向跳下去,总能碰到对方的。但是除非这两只青蛙在同一时间跳到同一点上,不然是永远都不可能碰面的。为了帮助这两只乐观的青蛙,你被要求写一个程序来判断这两只青蛙是否能够碰面,会在什么时候碰面。 
我们把这两只青蛙分别叫做青蛙A和青蛙B,并且规定纬度线上东经0度处为原点,由东往西为正方向,单位长度1米,这样我们就得到了一条首尾相接的数轴。设青蛙A的出发点坐标是x,青蛙B的出发点坐标是y。青蛙A一次能跳m米,青蛙B一次能跳n米,两只青蛙跳一次所花费的时间相同。纬度线总长L米。现在要你求出它们跳了几次以后才会碰面。 

Input

输入只包括一行5个整数x,y,m,n,L,其中x≠y < 2000000000,0 < m、n < 2000000000,0 < L < 2100000000。

Output

输出碰面所需要的跳跃次数,如果永远不可能碰面则输出一行"Impossible"

Sample Input

1 2 3 4 5

Sample Output

4

首先根据题意写出等式:(x + m*t)%L = (  y + n*t)%L;

则  x + m*t - ( y + n*t ) = k*L

( n - m )*t + k*L = x - y;

a = n - m,   b = L, c = x - y;

然后进行求解

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <map>
#include <vector>
using namespace std;
int x,y;

int exgcd(int a,int b)//方程ax+by=gcd(a,b)的一对解
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }
    int r=exgcd(b,a%b);
    int tem=x;
    x=y;
    y=tem-a/b*y;
    return r;
}

int main()
{
    int m,n,X,Y,l,a,b,c,d;
    while(~scanf("%d %d %d %d %d",&X,&Y,&m,&n,&l))
    {
        a=n-m;
        b=l;
        c=X-Y;
        cout<<c<<endl;
        d=exgcd(a,b);
        if(c%d!=0)//此条件下,无整数解
        {
            printf("Impossible\n");
            continue;
        }
        x=x/d*c;//ax+by=c的x的一个特解
        int b1=abs(b/d);//ax+by=c的通解x=x1+b1*t(t为任意整数)
        x=(x%b1+b1)%b1;//确定最小正解

        printf("%d\n",x);

     }
    return 0;
 }

 

深入进阶

Ms. Iyo Kiffa-Australis has a balance and only two kinds of weights to measure a dose of medicine. For example, to measure 200mg of aspirin using 300mg weights and 700mg weights, she can put one 700mg weight on the side of the medicine and three 300mg weights on the opposite side (Figure 1). Although she could put four 300mg weights on the medicine side and two 700mg weights on the other (Figure 2), she would not choose this solution because it is less convenient to use more weights. 
You are asked to help her by calculating how many weights are required.

Input

The input is a sequence of datasets. A dataset is a line containing three positive integers a, b, and d separated by a space. The following relations hold: a != b, a <= 10000, b <= 10000, and d <= 50000. You may assume that it is possible to measure d mg using a combination of a mg and b mg weights. In other words, you need not consider "no solution" cases. 
The end of the input is indicated by a line containing three zeros separated by a space. It is not a dataset.

Output

The output should be composed of lines, each corresponding to an input dataset (a, b, d). An output line should contain two nonnegative integers x and y separated by a space. They should satisfy the following three conditions. 

  • You can measure dmg using x many amg weights and y many bmg weights. 
  • The total number of weights (x + y) is the smallest among those pairs of nonnegative integers satisfying the previous condition. 
  • The total mass of weights (ax + by) is the smallest among those pairs of nonnegative integers satisfying the previous two conditions.


No extra characters (e.g. extra spaces) should appear in the output.

Sample Input

700 300 200
500 200 300
500 200 500
275 110 330
275 110 385
648 375 4002
3 1 10000
0 0 0

Sample Output

1 3
1 1
1 0
0 3
1 1
49 74
3333 1

 

题意:有两种类型的砝码质量分别为,要求称出质量为的物品,要求的数量的数量的和

     最小,如果有多个最小值,取最小的。

 

分析:扩展欧几里得求出特解后,把转化为最小正值,即,若

     求出的为负值,则把变正,意思就是砝码放置的位置有左右之分,可以左面的减去右面的,也可以右面

     的减去左面的。同理,再求出为最小合法正值时的解,将这两种情况比较取小的即可。

 

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
int gcd( int a, int b, int &x, int &y )
{
    if( b == 0 )
    {
        x = 1;
        y = 0;
        return a;
    }
    int x1, y1;
    int ans = gcd( b, a%b, x1, y1);
    x = y1;
    y = x1 - a/b*y1;
    return ans;
}
int main()
{
    int a, b ,d;
    while( scanf("%d%d%d", &a, &b, &d) != EOF )
   {
    if( a == 0 && b == 0 && d == 0 )
    {
        return 0;
    }
    int x, y;
    int res = gcd( a, b, x, y );
    int x1 = x*d/res;
    ///x = x + b/res*t;
    ///y = y - a/res*t;
    int m = b/res;
    int m2 = a/res;
    x1 = ( x1%m + m)%m;
    int y1 = abs((d - a*x1)/b);
    int y2 = y*d/res;
    y2 = (y2%m2 + m2)%m2;
    int x2 = abs((d-y2*b)/a);
    if( x1 + y1 > x2 + y2 )
    {
        printf("%d %d\n", x2, y2);
    }
    else
        printf("%d %d\n", x1,y1);
   }
    return 0;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值