[暑假集训--数论]poj2142 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 end of the input is indicated by a line containing three zeros separated by a space. It is not a dataset.
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.
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
[暑假集训--数论]poj2142 The Balance的更多相关文章
- [暑假集训--数论]hdu2136 Largest prime factor
Everybody knows any number can be combined by the prime number. Now, your task is telling me what po ...
- [暑假集训--数论]hdu1019 Least Common Multiple
The least common multiple (LCM) of a set of positive integers is the smallest positive integer which ...
- [暑假集训--数论]poj2115 C Looooops
A Compiler Mystery: We are given a C-language style for loop of type for (variable = A; variable != ...
- [暑假集训--数论]poj1365 Prime Land
Everybody in the Prime Land is using a prime base number system. In this system, each positive integ ...
- [暑假集训--数论]poj2034 Anti-prime Sequences
Given a sequence of consecutive integers n,n+1,n+2,...,m, an anti-prime sequence is a rearrangement ...
- [暑假集训--数论]poj1595 Prime Cuts
A prime number is a counting number (1, 2, 3, ...) that is evenly divisible only by 1 and itself. In ...
- [暑假集训--数论]poj2262 Goldbach's Conjecture
In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in whic ...
- [暑假集训--数论]poj2909 Goldbach's Conjecture
For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 ...
- [暑假集训--数论]poj3518 Prime Gap
The sequence of n − 1 consecutive composite numbers (positive integers that are not prime and not eq ...
随机推荐
- CUDA中记录执行时间-GPU端
事件eventcudaEvent_t start,stop;cudaEventCreate(&start);cudaEventCreate(&stop);cudaEventRecord ...
- C# 多态与new关键字
1. 你通常怎样用多态? 假设我有一个类,里面有一个 PrintStatus 方法,用于打印实例的当前状态,我希望该类的派生类都带有一个 PrintStatus 方法,并且这些方法都用于打印其实例的当 ...
- .NET利用RFC连接SAP,查询、读取SAP数据
为黄朴整理!!!!!!!!!!!!!!!!! 在NuGet 添加 sapnco 一个简单的SAPCommand,方法 GetDataTableFromRFCTable 复制于 https://www. ...
- 1005: [HNOI2008]明明的烦恼
Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 6539 Solved: 2558[Submit][Status][Discuss] Descripti ...
- C/C++程序基础 (七)继承和多态
多态 编译时多态:重载 运行时多态:虚函数.根据运行时的类别获取正确的虚指针,从而定位正确的虚函数. 虚函数 虚函数指针:指向虚函数表.多重继承则含有多个基类的虚函数指针. 虚函数表:函数指针集合.普 ...
- Centos 6版本Device eth0 does not seem to be present,delaying initialization.故障处理
1.1 故障现象 2019年06月14日晚上,公司项目组说有台业务服务器连接不上,比较着急,我通过vpn拨入的方式远程登录到管理控制台查看发现网卡没有获取到IP地址,我尝试重启来重新启动,重启的时候 ...
- [BZOJ3312][USACO]不找零(状压DP)
Description 约翰带着 N 头奶牛在超市买东西,现在他们正在排队付钱,排在第 i 个位置的奶牛需要支付 Ci元.今天说好所有东西都是约翰请客的,但直到付账的时候,约翰才意识到自己没带钱,身上 ...
- jichu
第 题 请编写函数 fun,其功能时:计算并输出当 x<0.97 时下列多项式的值,直到| sn-s(n-)|<0.000001 为止. Sn=+.5x+)/!x()+…+)()…..() ...
- 51NOD 1128正整数分组V2 二分答案
这道题是典型的二分答案法.但是首先难道这道题的时候我进行了一系列的思考,甚至联想到了之前多校中类似于树状划分的问题...原因是大家都包括N各节点K个输入.. 实际上最开始联想到了应当使用二分法“枚举” ...
- spark测试几个hadoop的典型例子
1.求每年的最高温度数据格式如下: 0067011990999991950051507004888888889999999N9+00001+999999999999999999999900670119 ...