码迷,mamicode.com
首页 > 其他好文 > 详细

poj 2142

时间:2017-02-17 15:05:04      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:mon   pre   sum   datasets   ati   and   possible   oss   har   

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*x1+d=b*y1;
b*x2+d=a*y2;
求minn(x1+y1,x2+y2);
分别用扩展欧几里得求出x1,y1,x2,y2;然后比较两大小,输出较小的一组;

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cstdlib>
#include<string>
#define eps 0.000000001
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
ll abs1(ll n){
    if(n<0)n=-n;
    return n;
}
ll gcd(ll a,ll b){
    if(b==0)return a;
    else{
        return gcd(b,a%b);
    }
}
ll exgcd(ll a,ll b,ll &x,ll &y){
    if(b==0){
        x=1;y=0;return a;
    }
    ll r=exgcd(b,a%b,x,y);
    int t=y;
    y=x-(a/b)*y;
    x=t;
    return r;
}
int main(){
    ll a,b,c;ll x1,y1,x2,y2;
    while(scanf("%I64d%I64d%I64d",&a,&b,&c)!=EOF){
        if(a==0&&b==0&&c==0)break;
        ll r1=exgcd(a,b,x1,y1);
        ll r2=exgcd(b,a,x2,y2);
        x1=x1*c/r1;
        x2=x2*c/r2;
        ll t1=b/r1;
        x1=(x1%t1+t1)%t1;
        y1=abs1((a*x1-c)/b);
        ll t2=a/r2;
        x2=(x2%t2+t2)%t2;
        y2=abs1((b*x2-c)/a);
        if(x1+y1<x2+y2)cout<<x1<<" "<<y1<<endl;
        else{
            cout<<y2<<" "<<x2<<endl;
        }
    }
}

 

poj 2142

标签:mon   pre   sum   datasets   ati   and   possible   oss   har   

原文地址:http://www.cnblogs.com/Aa1039510121/p/6409667.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!