poj 2142
You are asked to help her by calculating how many weights are required.
Input
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
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<)n=-n;
return n;
}
ll gcd(ll a,ll b){
if(b==)return a;
else{
return gcd(b,a%b);
}
}
ll exgcd(ll a,ll b,ll &x,ll &y){
if(b==){
x=;y=;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==&&b==&&c==)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的更多相关文章
- POJ.2142 The Balance (拓展欧几里得)
POJ.2142 The Balance (拓展欧几里得) 题意分析 现有2种质量为a克与b克的砝码,求最少 分别用多少个(同时总质量也最小)砝码,使得能称出c克的物品. 设两种砝码分别有x个与y个, ...
- poj 2142 The Balance
The Balance http://poj.org/problem?id=2142 Time Limit: 5000MS Memory Limit: 65536K Descripti ...
- POJ 2142 The Balance(exgcd)
嗯... 题目链接:http://poj.org/problem?id=2142 AC代码: #include<cstdio> #include<iostream> using ...
- poj 2142 拓展欧几里得
#include <cstdio> #include <algorithm> #include <cstring> #include <iostream> ...
- POJ 2142 The Balance【扩展欧几里德】
题意:有两种类型的砝码,每种的砝码质量a和b给你,现在要求称出质量为c的物品,要求a的数量x和b的数量y最小,以及x+y的值最小. 用扩展欧几里德求ax+by=c,求出ax+by=1的一组通解,求出当 ...
- poj 2142 扩展欧几里得解ax+by=c
原题实际上就是求方程a*x+b*y=d的一个特解,要求这个特解满足|x|+|y|最小 套模式+一点YY就行了 总结一下这类问题的解法: 对于方程ax+by=c 设tm=gcd(a,b) 先用扩展欧几里 ...
- POJ 2142 The Balance (解不定方程,找最小值)
这题实际解不定方程:ax+by=c只不过题目要求我们解出的x和y 满足|x|+|y|最小,当|x|+|y|相同时,满足|ax|+|by|最小.首先用扩展欧几里德,很容易得出x和y的解.一开始不妨令a& ...
- POJ 2142:The Balance_扩展欧几里得(多组解)
先做出两个函数的图像,然后求|x|+|y|的最小值.|x|+|y|=|x0+b/d *t |+|y0-a/d *t| 这个关于t的函数的最小值应该在t零点附近(在斜率大的那条折线的零点附近,可以观察出 ...
- E - The Balance POJ - 2142 (欧几里德)
题意:有两种砝码m1, m2和一个物体G,m1的个数x1, m2的个数为x2, 问令x1+x2最小,并且将天平保持平衡 !输出 x1 和 x2 题解:这是欧几里德拓展的一个应用,欧几里德求不定方程 ...
- 扩展欧几里得(E - The Balance POJ - 2142 )
题目链接:https://cn.vjudge.net/contest/276376#problem/E 题目大意:给你n,m,k,n,m代表当前由于无限个质量为n,m的砝码.然后当前有一个秤,你可以通 ...
随机推荐
- 关于用友 U8-UAP二开的一些事
这是关于一个刚刚接触用友U8的二次开发的一些小心得. 首先就是用友二开的论坛,http://u8dev.yonyou.com/ 当然这个论坛做得不怎么样,提出了好几个问题,都没有回复的. 以下是关于二 ...
- js 学习笔记---基本概念
早已接触javascript多年之后,竟然还有这些概念混淆不清,毫不知情,说出来真实无地自容 ! 1.使用严格模式,"use strict",虽然不适用,但是要知道,以免别人使用时 ...
- 汇编:MSR/MRS/BIC指令
1.MRS指令MRS指令的格式为:MRS{条件} 通用寄存器,程序状态寄存器(CPSR或SPSR)MRS指令用于将程序状态寄存器的内容传送到通用寄存器中.该指令一般用在以下两种情冴: Ⅰ.当需要改 ...
- CAD取Excel表格(com接口)
1 2 3 4 5 6 7 8 9 10 11 12 MxDrawResbuf ret = (MxDrawResbuf)axMxDrawX1.Call("ExApp_GetExcel&quo ...
- 【转载】Linux 通过mount -o loop 配置本地.iso镜像为yum源(yum仓库)
原文地址:https://www.jb51.net/os/RedHat/2682_all.html 如果拷贝到本地,可以使用mount mount fileName mountPoint -o loo ...
- chrome浏览器处理本地Ajax跨域
chrome浏览器下 项目开发过程中,用到了Ajax异步请求.若将项目放在本地服务器中,通过localhost访问文件,不会报错.若直接通过file://访问文件就会报错. 报错信息: XMLHttp ...
- Django - Ajax初识
当需要在弹出的对话框中,做判断操作时,需要用到ajax 1.host.html <!DOCTYPE html><html lang="en"><hea ...
- php第二十一节课
AJAX <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...
- enote笔记语言(3)
what&why(why not)&how&when&where&which:紫色,象征着神秘而又潜蕴着强大的力量,故取紫色. key&key-memo ...
- input chrome下输入之后背景变为黄色的解决办法
之所以Input输入之后背景原因色变为可恶的黄色,是因为在chrome 下input加上了input:-webkit-autofill这个属性,里面写的就是这个问题出现的原因 代码就是:input:- ...