POJ2142:The Balance (欧几里得+不等式)
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,求用最少的砝码或者最轻的砝码称出来,输出a和b的数量X和Y。
思路:|X|+|Y|=|X0+t*b/gcd|+|Y0-t*a/gcd|,在|X|取最小或者|Y|取最小时满足题意。
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
void ex_gcd(int a,int b,int &d,int &x,int &y)
{
if(b==){x=; y=; d=a; return ;}
ex_gcd(b,a%b,d,y,x); y-=a/b*x;
}
int main()
{
int a,b,c,x,y,gcd;
while(~scanf("%d%d%d",&a,&b,&c)){
if(a==&&b==&&c==) return ;
ex_gcd(a,b,gcd,x,y);
if(c%gcd!=) printf("no solution\n");
else{
int x1=((c/gcd*x)%(b/gcd)+(b/gcd))%(b/gcd);
int y1=(c-a*x1)/b;
int y2=((c/gcd*y)%(a/gcd)+(a/gcd))%(a/gcd);
int x2=(c-b*y2)/a;
x1=abs(x1); y1=abs(y1); x2=abs(x2); y2=abs(y2);
if(x1+y1<x2+y2||x1*a+y1*b<x2*a+y2*b) printf("%d %d\n",x1,y1);
else printf("%d %d\n",x2,y2);
}
}
return ;
}
POJ2142:The Balance (欧几里得+不等式)的更多相关文章
- POJ.2142 The Balance (拓展欧几里得)
POJ.2142 The Balance (拓展欧几里得) 题意分析 现有2种质量为a克与b克的砝码,求最少 分别用多少个(同时总质量也最小)砝码,使得能称出c克的物品. 设两种砝码分别有x个与y个, ...
- 扩展欧几里得(E - The Balance POJ - 2142 )
题目链接:https://cn.vjudge.net/contest/276376#problem/E 题目大意:给你n,m,k,n,m代表当前由于无限个质量为n,m的砝码.然后当前有一个秤,你可以通 ...
- SGU 141.Jumping Joe 数论,拓展欧几里得,二元不等式 难度:3
141. Jumping Joe time limit per test: 0.25 sec. memory limit per test: 4096 KB Joe is a frog who lik ...
- POJ - 2142 The Balance(扩展欧几里得求解不定方程)
d.用2种砝码,质量分别为a和b,称出质量为d的物品.求所用的砝码总数量最小(x+y最小),并且总质量最小(ax+by最小). s.扩展欧几里得求解不定方程. 设ax+by=d. 题意说不定方程一定有 ...
- POJ 2142 - The Balance [ 扩展欧几里得 ]
题意: 给定 a b n找到满足ax+by=n 的x,y 令|x|+|y|最小(等时令a|x|+b|y|最小) 分析: 算法一定是扩展欧几里得. 最小的时候一定是 x 是最小正值 或者 y 是最小正值 ...
- SGU 106 The equation 扩展欧几里得好题
扩展欧几里得的应用……见算法竞赛入门经典p.179 注意两点:1.解不等式的时候除负数变号 2.各种特殊情况的判断( a=0 && b=0 && c=0 ) ( a=0 ...
- NOIP2012拓展欧几里得
拉板题,,,不说话 我之前是不是说过数据结构很烦,,,我想收回,,,今天开始的数论还要恶心,一早上听得头都晕了 先来一发欧几里得拓展裸 #include <cstdio> void gcd ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C.Ray Tracing (模拟或扩展欧几里得)
http://codeforces.com/contest/724/problem/C 题目大意: 在一个n*m的盒子里,从(0,0)射出一条每秒位移为(1,1)的射线,遵从反射定律,给出k个点,求射 ...
- poj 1061 青蛙的约会 拓展欧几里得模板
// poj 1061 青蛙的约会 拓展欧几里得模板 // 注意进行exgcd时,保证a,b是正数,最后的答案如果是负数,要加上一个膜 #include <cstdio> #include ...
随机推荐
- itext A4纸张横向创建PDF
import java.awt.Color;import java.io.FileOutputStream;import java.io.IOException; import com.lowagie ...
- 程序包com.sun.image.codec.jpeg不存在解决方法
https://blog.csdn.net/u011627980/article/details/50436842
- AC日记——最大子段和 洛谷 P1115
题目描述 给出一段序列,选出其中连续且非空的一段使得这段和最大. 输入输出格式 输入格式: 输入文件maxsum1.in的第一行是一个正整数N,表示了序列的长度. 第2行包含N个绝对值不大于10000 ...
- SQL入门随笔(上机实验报告)
<数据定义部分> 一.定义模式和删除模式 a.为用户定义一个模式学生—课程模式 S-T CREATE SCHEMA "S-T" AUTHORIZATION USE ...
- Codeforces 518 D Ilya and Escalator
Discription Ilya got tired of sports programming, left university and got a job in the subway. He wa ...
- SolidEdge 如何由装配图快速生成爆炸视图
建立图纸精灵,组态中选择explode(没有下一步可选了) 点击完成即可绘制爆炸图
- linux的主分区与逻辑分区的关系
主分区和扩展分区的差别在于主分区位于硬盘的最開始.MBR 扇区的位置.这个位置的数据在计算机启动时.会自己主动被 BIOS 读取而且运行,也就是说这个位置的分区表会自己主动被 BIOS 读取到内 ...
- hdu 4858 项目管理(vector模拟)
# include <stdio.h> # include <algorithm> # include <string.h> # include <vecto ...
- MUI-折叠面板效果accordion
在做开发的过程中我们经经常使用到折叠面板. 那我们来看下折叠面板到底是怎么使用. 废话不多说. 代码粘下来: <!DOCTYPE html> <html> <head&g ...
- struct与 union的基本用法
结构体与联合体是C语言的常见数据类型,可对C的基本数据类型进行组合使之能表示复杂的数据结构,意义深远,是优异代码的必备工具.一. struct与 union的基本用法,在语法上union ...