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.
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
题目类型:二元一次不定方程
算法分析:题目中要求ax+by尽可能小,即方程右边只有d(ax+by=d),才可能满足条件。此时可以使用扩展欧几里得算法分别求出不定方程的最小非负特解x0和y0。然后分别求出此时的y和x。最后比较两个x、y的和的大小。取较小的那个x和y输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define lson rt << 1, l, m #define rson rt << 1 | 1, m + 1, r const int INF = 0x7FFFFFFF; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 50000 + 66; long long ex_gcd (long long a, long long b, long long &x, long long &y) { if (b == 0) {x = 1; y = 0; return a;} long long tt = ex_gcd(b, a % b, y, x); y = y - a / b * x; return tt; } int main() { // ifstream cin("aaa.txt"); // freopen ("aaa.txt", "r", stdin); long long a, b, d; while (cin >> a >> b >> d) { if (a == 0 && b == 0 && d == 0) break; long long tt, xx, yy; tt = ex_gcd(a, b, xx, yy); // cout << xx << " " << yy << endl; long long maxval_posx = -1, maxval_posy = -1; xx = xx * d / tt; yy = yy * d / tt; // cout << xx << " " << yy << endl; long long tx = xx % (b / tt); if (xx < 0) tx += b / tt; long long ty = (d - a * tx) / b; if (ty < 0) ty = -ty; long long maxval = tx + ty; maxval_posx = tx, maxval_posy = ty; ty = yy % (a / tt); if (ty < 0) ty += a / tt; // cout << ty << endl; tx = (d - b * ty) / a; if(tx < 0) tx = -tx; if (maxval > tx + ty) { maxval_posx = tx; maxval_posy = ty; } cout << maxval_posx << " " << maxval_posy << endl; } return 0; } |
- « 上一篇:poj2115
- poj2201:下一篇 »