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

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


The Balance
Time Limit: 5000MS Memory Limit: 65536K
Total 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 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

Source



以下分析参考自博客:http://blog.youkuaiyun.com/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 1000000007
using 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 1000000007
using 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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值