POJ poj 2142 The Balance 扩展欧几里得 |x|+|y|最小
The Balance
Time Limit: 5000MS |
Memory Limit: 65536K |
|
Total Submissions: 8784 |
Accepted: 3817 |
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
算法分析:
题意:
砝码有a,b的重量,去称去c重量,问最小的|x|+|y|
实现
ax+by=c,扩展欧几里德方程,关键就是求出最后的|x|+|y|的最小解,做的时候想的是,求出x的最小整数解x1,根据方程求出y1,在求出y的最小整数解y2,然后求出对应的x2,注意y1和x2可能为负,取反,(砝码左右分正负)
然后就过了,但是其实想想不对,这样不能保证|x|+|y|是最小的。
正规解法:
让我们假定a > b(输入不符就交换a, b),设函数:
z = |x| + |y| = |x0 + b / d * t| + |y0 - a / d * t|;
通过简单分析可知,随着t的增大,y单减,x单增,而我们使a > b,因此:
y > 0时,函数|y|单调递减,a>b,|x|影响不到|z|的增减性,函数|z|单调递减;
y < 0时,函数|z|单调递增;
所以在满足y = 0这个条件时,求出的x, y是最优解。
但本题求的结果是整数解,所以要先求出近似解t = y0 * d / a,然后再在其左右分别寻找一个解,三个解比较得出最优解。
代码实现:
方法一:
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL;
LL exgcd(LL a, LL b, LL &x0, LL &y0)
{
if(!b)
{
x0 = 1; y0 = 0;
return a;
}
int ans=exgcd(b, a%b, x0, y0);
int t = x0;
x0 = y0;
y0 = t-a/b*y0;
return ans;
}
int main()
{
LL a,b,c;
while(scanf("%lld%lld%lld",&a,&b,&c))
{
LL x0,y0;
if(a==0&&b==0&&c==0)break;
LL gcd=exgcd(a,b,x0,y0);
LL t=b/gcd;
LL x1=x0*c/gcd;
x1=(x1%t+t)%t; //求出x0的最小正数解
LL y1=(c-a*x1)/b;
if(y1<0) y1=-y1;
LL t1=a/gcd;
LL y2=y0*c/gcd; //求出y0的最小正数解
y2=(y2%t1+t1)%t1;
LL x2=(c-b*y2)/a;
if(x2<0) x2=-x2;
if(x1+y1<=x2+y2)
cout<<x1<<" "<<y1<<endl;
else
cout<<x2<<" "<<y2<<endl;
}
return 0;
}
方法二:
#include <iostream>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#define MAX 100005
#define LL long long
#define MM 10005
#define INF 0x7f7f7f7f
using namespace std;
int x, y;
int exgcd(int a, int b)
{
if(!b)
{
x = 1;
y = 0;
return a;
}
int d = exgcd(b, a % b);
int tmp = x;
x = y;
y = tmp - a / b * y;
return d;
}
void solve(int a, int b, int c)
{
int d, ansx, ansy, Min, tx, ty;
bool flag = false;
if(a < b) //交换
{
swap(a, b);
flag = true;
}
d = exgcd(a, b);
x *= c / d, y *= c / d;
int t = d * y / a;
Min = INF;
for(int i = t - 1; i <= t + 1; i++)
{
tx = x + b / d * i;
ty = y - a / d * i;
if(Min > abs(tx) + abs(ty))
{
Min = abs(tx) + abs(ty);
ansx = tx, ansy = ty;
}
}
if(flag)
printf("%d %d\n", abs(ansy), abs(ansx));
else
printf("%d %d\n", abs(ansx), abs(ansy));
}
int main()
{
int a, b, d;
while(scanf("%d %d %d", &a, &b, &d) && a + b + d)
solve(a, b, d);
return 0;
}
上一篇: 没有了摩擦力会怎样?
下一篇: 华为新手机还能用安卓系统吗?官方明确表态