The Balance
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.
You are asked to help her by calculating how many weights are required.
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.
The end of the input is indicated by a line containing three zeros separated by a space. It is not a dataset.
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.
No extra characters (e.g. extra spaces) should appear in the output.
- 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.
700 300 200 500 200 300 500 200 500 275 110 330 275 110 385 648 375 4002 3 1 10000 0 0 0
1 3 1 1 1 0 0 3 1 1 49 74 3333 1
这是一道拓展欧几里得的数论基础题,题意是给你两种质量的重物,你要用天平和这两种重物衡量出第三个重物,这两种重物可以使用多个,要求输出这两种重物各需多少个,并且要满足这两个重物的总个数尽可能少,并且总质量尽可能小。我们可以设a重物需要x个,b重物需要y个,然后要搭配出c重物的质量,换成式子就是ax+by=c,直接就可以套用拓欧公式。
欧几里得公式:int gcd(int a,int b){return b?gcd(b,a%b):a;}
欧几里得公式就是求取a,b的最大公约数,而拓展欧几里得公式就是在这个的基础上进行的。
拓展欧几里得公式:对于不完全为 0 的非负整数 a,b,gcd(a,b),必然存在整数对 x,y ,使得 gcd(a,b)=ax+by。
int exgcd(int a, int b, int &x, int &y)//拓展欧几里得
{
if (b == 0)
{
x = 1;
y = 0;
return a;
}
int ans = exgcd(b, a%b, x, y);
int TT = x;
x = y;
y = TT - a / b*y;
return ans;//ans是a,b的最大公约数
}
调用这个公式后,x和y的值就是ax+by=gcd(a,b)的解,x0=c/ans*x和y0=c/ans*y就是ax+by=c的解,当然,这只是一个特解,但是可以根据它得到通解为x0+ k*b/ans, y0-k*a/ans,k为任意常数。这道题里因为要求
这两个重物的总个数尽可能少,并且总质量尽可能小。所以我们要对k进行一个循环判断,得出最优解,AC代码如下。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 1e9 + 5;
const int MAXN = 1e6 + 5;
const int MOD = 1e9 + 7;
const double eps = 1e-7;
using namespace std;
int exgcd(int a, int b, int &x, int &y)
{
if (b == 0)
{
x = 1;
y = 0;
return a;
}
int ans = exgcd(b, a%b, x, y);
int TT = x;
x = y;
y = TT - a / b*y;
return ans;
}
int main()
{
int X, Y;
int a, b, d;
int x, y;
int xx, yy;
int M;
while (scanf("%d%d%d",&a,&b,&d)!=EOF)
{
X = INF;
Y = INF;
if (a == 0 && b == 0 && d == 0)
break;
M = exgcd(a, b, x, y);
x = d*x / M;
y = d*y / M;
for (int k = -100000;k<100000 ; k++)//这里对k的循环应该有更好的确定范围的方法,不过在这题里1e5足够了,而且不超时
{
xx = x + k*b / M;
yy = y - k*a / M;
if (abs(xx) + abs(yy) < abs(X) + abs(Y))
{
X = xx;
Y = yy;
}
if (abs(xx) + abs(yy) == abs(X) + abs(Y))
{
if(abs(xx)*a+abs(yy)*b<abs(X)*a + abs(Y)*b)
{
X = xx;
Y = yy;
}
}
}
printf("%d %d\n", abs(X), abs(Y));
}
}