POJ 1061 && POJ 2142 两道扩展欧几里得简单题

来源:互联网 发布:中端单反推荐 知乎 编辑:程序博客网 时间:2024/05/16 19:38

额 扩展欧几里得在暑假集训的数学周就刷过,但是又是一味的套模板,导致到了最后BALL东西没学到。先把这算法的一些基本的东西写一下o(╯□╰)o

最简单的: gcd(a,b)=gcd(b,a%b)

证明:不妨设a=kb+r 则r=amodb ,若gcd(a,b)=d,则有a%d=0&&b%d=0又因为r=a-kb所以r%d=0 故gcd(b,a%b)=d则有 gcd(a,b)=gcd(b,a%b)。

然后对于形如ax+by=gcd(a,b)的形式

证明:设a>b

1.若b=0 则x=1,y=0 gcd(a,b)=a;

2.若a*b!=0不妨设  ax1+by1=gcd(a,b),通过前面的等量转换有:bx2+(a%b)y2=gcd(b,a%b)===>bx2+(a-(a/b)*b)*y2=ax1+by1。

  再有对应相等有:x1=y2,y1=x2-(a/b)*y2,一直递归下去,总能得到1的形式


第一大应用:求解不定方程的解

形如 ax+by=c的二元不定方程,首先若c%gcd(a,b)!=0则不可能有整数解。

若等于零 则我们先求ax+by=gcd(a,b)的一组特解不妨设为x0,y0 则ax+by=c的一组特解可以写成形如x=x*c/gcd(a,b) y=y*c/gcd(a,b)的形式,

进而求出ax+by=c的通解为:x=x+b*t/gcd(a,b) y=y-a*t/gcd(a,b),t是任意整数

关于求出x>0的解,这里主要讨论x>0的最小解。为了求该x,主要是确定通解中t的值应为多少,如果x原来就大于0,那么直接对b/gcd(a,b)取模即可,如果x<0,那么可以让-x对b/gcd(a,b)取模,之后取反再加上b/gcd(a,b)。其实如果用程序,那么可以统一解决,设t=b/gcd(a,b),那么有x'=(x%t+t)%t,然后再求对应的y'即可.(这一段是复制学长当年写的o(╯□╰)o)



POJ 1061

青蛙的约会
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 86528 Accepted: 15207

Description

两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面。它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止。可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特征,也没有约定见面的具体位置。不过青蛙们都是很乐观的,它们觉得只要一直朝着某个方向跳下去,总能碰到对方的。但是除非这两只青蛙在同一时间跳到同一点上,不然是永远都不可能碰面的。为了帮助这两只乐观的青蛙,你被要求写一个程序来判断这两只青蛙是否能够碰面,会在什么时候碰面。
我们把这两只青蛙分别叫做青蛙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
题目意思描述是中文。。 很明显了,假设最小需要t步则 (x+mt)恒等于(y+nt)mod(L)写成一般形式有(n-m)*t+kL=x-y

很自然的转化成了ax+by=c的x的最小正整数解的形式。。。

于是就很简单了。。 不过记住用long long  这个暑假因为犯过错。。 所以记住了。

#include<cstdio>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>using namespace std;long long a,b,c,d,x,y;long long m,n,xx,yy,l;long long t;long long ex_gcd(long long a,long long b,long long &x,long long &y){    long long res,tmp;    if(b==0)    {        x=1;        y=0;        return a;    }    res=ex_gcd(b,a%b,x,y);    tmp=x;    x=y;    y=tmp-(a/b)*y;    return res;}int main(){    while(cin>>x>>y>>m>>n>>l)    {        a=n-m;        b=l;        c=x-y;        d=ex_gcd(a,b,xx,yy);//ax+by=gcd(a,b)的解        if(c%d!=0)        {            cout<<"Impossible"<<endl;        }        else        {            xx=xx*(c/d);//ax+by=c的解            t=b/d;            xx=(xx%t+t)%t;//最小正整数解            cout<<xx<<endl;        }    }    return 0;}

POJ 2142


The Balance
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 3777 Accepted: 1665

Description

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 200500 200 300500 200 500275 110 330275 110 385648 375 40023 1 100000 0 0

Sample Output

1 31 11 00 31 149 743333 1
题目更加通俗 求ax+by=d且|x|+|y|要最小

实际上写成|x+b*t/g|+|y-a*t/g|最小。。 观察发现只要尽可能的使得a*t/g=y就能保证最小了 然后我们可以得到t=y*g/a,接着我们对t附近的一些数进行扫描就可以得到最小值。

当然听说可以把t t+1 t-1三个值带入就可以求得极值不用扫描也可以。。

#include<cstdio>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>using namespace std;int a,b,d;int ex_gcd(int a,int b,int &x,int &y){    int tmp;    int res;    if(b==0)    {        x=1;        y=0;        return a;    }    res=ex_gcd(b,a%b,x,y);    tmp=x;    x=y;    y=tmp-(a/b)*y;    return res;}int main(){    while(cin>>a>>b>>d)    {        if(a==b&&b==0&&d==0)            return 0;        int flag=0;        if(a<b)//看看有没有交换        {            swap(a,b);            flag=1;        }        int x0,y0;        int g=ex_gcd(a,b,x0,y0);       int  x=x0*d/g;      int   y=y0*d/g;        int t=y*g/a;        int x1,y1;        int xx,yy;        int mi=999999999;        for(int i=t-5;i<t+5;i++)//扫描        {            x1=abs(x+b*i/g);            y1=abs(y-a*i/g);            if(x1+y1<mi)            {                mi=x1+y1;                xx=x1;                yy=y1;            }        }        if(flag)        {            cout<<yy<<" "<<xx<<endl;        }        else        {            cout<<xx<<" "<<yy<<endl;        }    }    return 0;}


0 0