poj 2142 The Balance】的更多相关文章

POJ.2142 The Balance (拓展欧几里得) 题意分析 现有2种质量为a克与b克的砝码,求最少 分别用多少个(同时总质量也最小)砝码,使得能称出c克的物品. 设两种砝码分别有x个与y个,那么有ax+by=c.可用拓展欧几里得求解. 若x与y均为正数,说明在天平的同一侧,否则在不同册. 需要注意的是,求出的x与y仅为一组特解,此时需要求|x| + |y| 的最小值.根据: 可得 显然这是不好求的,但我们不妨设a>b,根据斜率关系,不难作图. 可以看出,最小值在t2附近取得,枚举附近值…
The Balance http://poj.org/problem?id=2142 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 3…
嗯... 题目链接:http://poj.org/problem?id=2142 AC代码: #include<cstdio> #include<iostream> using namespace std; inline int _abs(int x){ ) return -x; return x; } inline void exgcd(int a, int b, int &g, int &x, int &y){ ; y = ;} else { exgcd…
题意:有两种类型的砝码,每种的砝码质量a和b给你,现在要求称出质量为c的物品,要求a的数量x和b的数量y最小,以及x+y的值最小. 用扩展欧几里德求ax+by=c,求出ax+by=1的一组通解,求出当x取最小合法正整数解时y的取值,当y小于0时,说明应该放在a的另一边,变为正值.同理当y取最小时,可得到另一组解,比较两组解,取最小即可. #include<stdio.h> int ex_gcd(int a,int b,int &x,int &y){ if(!b){ x=,y=;…
这题实际解不定方程:ax+by=c只不过题目要求我们解出的x和y 满足|x|+|y|最小,当|x|+|y|相同时,满足|ax|+|by|最小.首先用扩展欧几里德,很容易得出x和y的解.一开始不妨令a>b,若a<=b,则交换a和b.设d=gcd(a,b),最终的则x=x0+b/d*t,y=y0-a/d*tz=|x|+|y|=|x0+b/d*t|+|y0-a/d*t|实际上就是求z=|a1*t+c1|+|c2-a2*t|在t取何值时最小.(a2>a1)首先由不定方程ax+by=c,a>…
题目: 求ax+by=c的一组解,使得abs(x)+abs(y)尽量小,满足前面前提下abs(ax)+abs(by)尽量小 题解: exgcd之后,分别求出让x尽量小和y尽量小的解,取min即可 #include<cstdio> #include<algorithm> #include<cstring> using namespace std; int a,b,c,x,y,u1,u2,v1,v2,g; int exGcd(int a,int b,int &x,i…
d.用2种砝码,质量分别为a和b,称出质量为d的物品.求所用的砝码总数量最小(x+y最小),并且总质量最小(ax+by最小). s.扩展欧几里得求解不定方程. 设ax+by=d. 题意说不定方程一定有解.对于不定整数方程pa+qb=c,若 c mod Gcd(p, q)=0,则该方程存在整数解,否则不存在整数解. 也就是说,d mod gcd(a,b)=0. a,b,d同时除以gcd(a,b)得到a'x+b'y=d'; 用扩展欧几里德求出 a'x+b'y=gcd(a',b')=1的解(x,y),…
题意: 给定 a b n找到满足ax+by=n 的x,y 令|x|+|y|最小(等时令a|x|+b|y|最小) 分析: 算法一定是扩展欧几里得. 最小的时候一定是 x 是最小正值 或者 y 是最小正值 (简单的证明应该是分x,y 符号一正一负,和x,y符号都为正来考虑) 扩欧解的方程为 ax+by = gcd(a, b) 先简化问题,等价为扩欧求的是 a'x+b'y = 1 则原方程等价为 a'x+b'y = n' (a, b, n 全部除以gcd(a, b) ) 先解x为最小正值的时候 x =…
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 an…
The Balance Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4781   Accepted: 2092 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 usi…