The Balance(扩张欧几里得求线性方程ax+by=c或模线性方程ax=b (mod n)的完善模板)

来源:互联网 发布:压缩路径的加权算法 编辑:程序博客网 时间:2024/05/16 14:36

Link:http://poj.org/problem?id=2142


The Balance
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 5132 Accepted: 2256

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

Source

Japan 2004


以下分析参考自博客:http://blog.csdn.net/crescent__moon/article/details/19114095

题意:求ax+by=c中|x|+|y|的最小值。

编程思想:通过exgcd可以求出一组解:x0=x0*(c/d),y0=y0*(c/d).(式子中d为gcd(a,b))
而x的所有解是x=x0+b/d*t,y的所有解是:y=y-a/d*t。|x|+|y|= | x + b/d*t | + | y - a/d*t |。
这个关于t的函数的最小值应该在|y - a/d*t|为零附近,即t=y*d/a 。在 y*g/a 附近的两整数点里取t,再直接验证这两点即可。这道题就是在a > b的情况下求使y1等于0附近的两个解。

AC code:
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <vector>#include <string>#include <queue>#include <stack>#include <algorithm>#define PI acos(-1.0)#define LINF 1000000000000000000LL#define eps 1e-8#define LL long long#define MAXN 100010 #define MOD 1000000007using namespace std;const int INF=0x3f3f3f3f;LL exgcd(LL A,LL &x,LL B,LL &y){    LL x1,y1,x0,y0;    x0=1;y0=0;    x1=0;y1=1;    LL r=(A%B+B)%B;    LL q=(A-r)/B;    x=0;y=1;    while(r)    {        x=x0-q*x1;        y=y0-q*y1;        x0=x1;        y0=y1;        x1=x;y1=y;        A=B;B=r;r=A%B;        q=(A-r)/B;    }    return B;}LL ABS(LL x){return x>=0?x:-x;}LL x,y,m,n,L,a,b,c,ans;  int main(){    while(scanf("%lld%lld%lld",&a,&b,&c)!=EOF)      {    if(a==0&&b==0&&c==0) break;    //a=n-m;    //b=L;         //c=x-y;         LL x0,y0,flag=0,x,y;        if(a<b){flag=a;a=b;b=flag;flag=1;}//默认令a>=b,flag=1说明有交换,则对应的系数x和y最后输出时也要交换后输出         LL d=exgcd(a,x0,b,y0);//返回a、b的最大公约数         //求ax+by=c         if(c%d!=0)//无解         {        printf("Impossible\n");}else{x0=x0*(c/d);//x的一个解        y0=y0*(c/d);//y的一个解        LL t=y0/(a/d),minx=INF,miny=INF,minn=INF;        for(int i=t-1;i<=t+1;i++)        {        //x的所有解是x=x0+b/d*t,y的所有解是:y=y-a/d*t。            x=x0+b/d*i;            y=y0-a/d*i;            //printf("%d %d\n",x,y);//打印所有解             if(ABS(x)+ABS(y)<minn)            {                minn=ABS(x)+ABS(y);                minx=ABS(x);                miny=ABS(y);            }            /*if(x>=0)            {            minx=min(minx,x);}            if(y>=0)            {            miny=min(miny,y);}*/        }        if(flag)printf("%lld %lld\n",miny,minx);        else printf("%lld %lld\n",minx,miny);       /* if(flag)printf("%lld\n",miny);        else printf("%lld\n",minx);*/        //printf("%lld\n",minx);  }    }    return 0;}

附上求模线性方程模板:

如果是模线性方程ax=b (mod n),令d=exgcd(a,n),该方程有解的充要条件为 d | b ,即 b% d==0

方程ax=b(mod n)的最小解 :x=(x*(b/d))%n 

方程ax=b(mod n)的最整数小解: x=(x%(n/d)+n/d)%(n/d)

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <vector>#include <string>#include <queue>#include <stack>#include <algorithm>#define PI acos(-1.0)#define LINF 1000000000000000000LL#define eps 1e-8#define LL long long#define MAXN 100010 #define MOD 1000000007using namespace std;const int INF=0x3f3f3f3f;LL exgcd(LL A,LL &x,LL B,LL &y){    LL x1,y1,x0,y0;    x0=1;y0=0;    x1=0;y1=1;    LL r=(A%B+B)%B;    LL q=(A-r)/B;    x=0;y=1;    while(r)    {        x=x0-q*x1;        y=y0-q*y1;        x0=x1;        y0=y1;        x1=x;y1=y;        A=B;B=r;r=A%B;        q=(A-r)/B;    }    return B;}LL ABS(LL x){return x>=0?x:-x;}//求ax=b(mod n)等价于ax+ny=b,故可以这样转化过来 int main(){long long A,B,C,k,x,y;     while(scanf("%I64d%I64d%I64d%I64d",&A,&B,&C,&k)!=EOF)      {    if(!A&&!B&&!C&&!k)break;     long long a,b,n,d;     a=C;    b=B-A;         n=(long long)1<<k;d=exgcd(a,x,n,y); //求a,n的最大公约数d=gcd(a,n)和方程d=ax+by的系数x、y   if(b%d)printf("FOREVER\n"); else  { x=(x*(b/d))%n;//方程ax=b(mod n)的最小解  x=(x%(n/d)+n/d)%(n/d); //方程ax=b(mod n)的最整数小解  printf("%I64d\n",x);  } /* 不知为何,下面的输出会RE!!!         /*LL x0,y0,flag=0;        if(a<b){flag=a;a=b;b=flag;flag=1;}//默认令a>=b,flag=1说明有交换,则对应的系数x和y最后输出时也要交换后输出         LL d=exgcd(a,x0,b,y0);//返回a、b的最大公约数         //求ax+by=c         if(c%d!=0)//无解         {        printf("FOREVER\n");}else{x0=x0*(c/d);//x的一个解        y0=y0*(c/d);//y的一个解        LL t=y0/(a/d),minx=LINF,miny=LINF,minn=LINF;        for(int i=t-1;i<=t+1;i++)        {        //x的所有解是x=x0+b/d*t,y的所有解是:y=y-a/d*t。            x=x0+b/d*i;            y=y0-a/d*i;            //printf("%d %d\n",x,y);//打印所有解            /* if(ABS(x)+ABS(y)<minn)            {                minn=ABS(x)+ABS(y);                minx=ABS(x);                miny=ABS(y);            }*/           /* if(x>=0)            {            minx=min(minx,x);}            if(y>=0)            {            miny=min(miny,y);}        }        if(flag)printf("%lld\n",miny);        else printf("%lld\n",minx);       /* if(flag)printf("%lld\n",miny);        else printf("%lld\n",minx);*/        //printf("%lld\n",minx);  //}    }    return 0;}



0 0
原创粉丝点击