poj 2142 The Balance (扩展欧几里得 数学)

来源:互联网 发布:caffe 继续训练 编辑:程序博客网 时间:2024/06/04 01:23

题目链接:poj 2142

The Balance

Time Limit: 5000MS Memory Limit: 65536K

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

扩展欧几里得多解第一题。参考了别人的题解,写博客整理一下思路。

脱下题目马甲,问题就是给你三个数a, b, c,找出一个等式满足以下要求:
1. a * x + b * y = c;
2. |x| + |y| 最小.

直接套扩展欧几里得公式,得出最初解x0, y0.

接下来的思路是要避免枚举的(初中)数学分析:

a * x + b * y = c 的解集为:    x = x0 + b / d * t;    y = y0 - a / d * t;其中t为任意整数。让我们假定a > b(输入不符就交换a, b),设函数:z = |x| + |y| = |x0 + b / d * t| + |y0 - a / d * t|;通过简单分析可知,随着t的增大,y单减,x单增,而我们使a > b,因此:    y > 0时,函数z单调递减;    y < 0时,函数z单调递增;所以在满足y = 0这个条件时,求出的x, y是最优解。但本题求的结果是整数解,所以要先求出近似解t = y0 * d / a,然后再在其左右分别寻找一个解,三个解比较得出最优解。

a过之后发现这道题的时限实在是宽松,说不定枚举也能过?

#include <iostream>#include <vector>#include <cstdio>#include <string>#include <cstring>#include <algorithm>#define MAX 100005#define LL long long#define MM 10005#define INF 0x7f7f7f7fusing 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;}

运行结果(应该是能0ms过的,然而poj上0ms看人品):
这里写图片描述

0 0