首页 技术 正文
技术 2022年11月9日
0 收藏 711 点赞 2,733 浏览 3106 个字

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.

[暑假集训–数论]poj2142 The Balance

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,求ax+by==c,并且输出|a|+|b|最小的方案,如果|a|+|b|相同者输出|ax|+|by|最小的方案

先解出个可行解,然后调整x至x是最小正数,y跟着动,用这个更新下答案。

然后调x到x是最小负数,更新答案

还有y是最小正数、最小负数的情况,更新答案

 Select Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define mkp(a,b) make_pair(a,b)
#define pi 3.1415926535897932384626433832795028841971
#define int long long
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
LL a,b,c;
inline int exgcd(int a,int b,int &x,int &y)
{
if (!b){x=;y=;return a;}
int gcd=exgcd(b,a%b,x,y);
int t=x;x=y;y=t-a/b*y;
return gcd;
}
inline LL LLabs(LL x){return x<?-x:x;}
inline void work()
{
LL x,y,ans1=,ans2=;
int tt=exgcd(a,b,x,y);
if (c%tt!=)return;
x=x*c/tt;y=y*c/tt;
int aa=a/tt,bb=b/tt;
int d=(x-(x%bb+bb)%bb)/bb;
x-=d*bb;y+=d*aa;
if (LLabs(x)+LLabs(y)<ans1+ans2||(LLabs(x)+LLabs(y)==ans1+ans2&&LLabs(x)*a+LLabs(y)*b<=ans1*a+ans2*b))ans1=LLabs(x),ans2=LLabs(y);
x-=bb;y+=aa;
if (LLabs(x)+LLabs(y)<ans1+ans2||(LLabs(x)+LLabs(y)==ans1+ans2&&LLabs(x)*a+LLabs(y)*b<=ans1*a+ans2*b))ans1=LLabs(x),ans2=LLabs(y);
d=(y-(y%aa+aa)%aa)/aa;
x+=d*bb;y-=d*aa;
if (LLabs(x)+LLabs(y)<ans1+ans2||(LLabs(x)+LLabs(y)==ans1+ans2&&LLabs(x)*a+LLabs(y)*b<=ans1*a+ans2*b))ans1=LLabs(x),ans2=LLabs(y);
x+=bb;y-=aa;
if (LLabs(x)+LLabs(y)<ans1+ans2||(LLabs(x)+LLabs(y)==ans1+ans2&&LLabs(x)*a+LLabs(y)*b<=ans1*a+ans2*b))ans1=LLabs(x),ans2=LLabs(y);
printf("%I64d %I64d\n",ans1,ans2);
}
main()
{
while (~scanf("%I64d%I64d%I64d",&a,&b,&c)&&a+b+c)work();
}

poj 2142

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,118
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,590
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,435
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,206
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,842
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,927