The Balance
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 5452   Accepted: 2380

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 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
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
思路:扩展欧几里德。
首先,如果没有取最小那个条件限制,这就是一道裸的扩展欧几里德。那么考虑那个限制条件。我们用欧几里德求出x,y后,我们先求
x的最小正解,然后代入方程求的y,如果y是正的,假设x要增大,那么y就要减小,当y还是>0由于这时两种砝码在同侧,所以我们可以
考虑下贪心,也就是优先取质量重的取,那么要么就是x取最小正解时要么取y是最小正解时(在x,y都为正的前提下)。那么如果当y<0
原来两个取正肯定要比一个取正一个取负的总和小。同理先取y为最小正,根据(ax+by=c)中x,y的等价性我们可以同样分析y。
那么综合这两种情况我们可以得出这样的结论(x+y)最小要么在x在最下正时取要么在y为最小正时取。
 1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<math.h>
5 #include<stdlib.h>
6 #include<string.h>
7 using namespace std;
8 typedef long long LL;
9 const long long N=1e6+3;
10 long long MM[1000005];
11 long long quick(long long n,long m);
12 pair<LL,LL>P(long long n,long long m);
13 long long gcd(long long n,long long m);
14 int main(void)
15 {
16 long long i,j,k,p,q;
17 while(scanf("%lld %lld %lld",&k,&p,&q),p!=0&k!=0&q!=0)
18 {
19 long long zz=gcd(k,p);
20 k/=zz;
21 p/=zz;
22 q/=zz;
23 pair<LL,LL>SS=P(k,p);
24 long long x=SS.first;
25 x*=q;
26 x=(x%p+p)%p;
27 long long y=fabs(1.0*(q-x*k)/p);
28 long long z=SS.second;
29 z*=q;
30 z=(z%k+k)%k;
31 long long xx=fabs(1.0*(q-z*p)/k);
32 if(xx+z<y+x)
33 printf("%lld %lld\n",xx,z);
34 else if(xx+z==y+x)
35 {
36 long long cc=x*k+y*p;
37 long long zz=z*p+xx*k;
38 if(xx<zz)
39 printf("%lld %lld\n",xx,z);
40 else printf("%lld %lld\n",x,y);
41 }
42 else printf("%lld %lld\n",x,y);
43 }
44 return 0;
45 }
46
47 long long quick(long long n,long m)
48 {
49 long long k=1;
50 while(m)
51 {
52 if(m&1)
53 {
54 k=(k%N*n%N)%N;
55 }
56 n=n*n%N;
57 m/=2;
58 }
59 return k;
60 }
61 long long gcd(long long n,long long m)
62 {
63 if(m==0)
64 return n;
65 if(n%m==0)
66 {
67 return m;
68 }
69 else return gcd(m,n%m);
70 }
71 pair<LL,LL>P(long long n,long long m)
72 {
73 if(m==0)
74 {
75 pair<LL,LL>K=make_pair(1,0);
76 return K;
77 }
78 else
79 {
80 pair<LL,LL>L=P(m,n%m);
81 pair<LL,LL>S;
82 S.first=L.second;
83 S.second=L.first-(n/m)*L.second;
84 return S;
85 }
86 }
												

The Balance(poj2142)的更多相关文章

  1. poj2142 The Balance

    poj2142 The Balance exgcd 应分为2种情况分类讨论 显然我们可以列出方程 ax-by=±d 当方程右侧为-d时,可得 by-ax=d 于是我们就得到了2个方程: ax-by=d ...

  2. [暑假集训--数论]poj2142 The Balance

    Ms. Iyo Kiffa-Australis has a balance and only two kinds of weights to measure a dose of medicine. F ...

  3. POJ2142:The Balance (欧几里得+不等式)

    Ms. Iyo Kiffa-Australis has a balance and only two kinds of weights to measure a dose of medicine. F ...

  4. POJ2142 The Balance (扩展欧几里德)

    本文为博主原创文章,欢迎转载,请注明出处 www.cnblogs.com/yangyaojia The Balance 题目大意  你有一个天平(天平左右两边都可以放砝码)与重量为a,b(1<= ...

  5. POJ-2142 The Balance 扩展欧几里德(+绝对值和最小化)

    题目链接:https://cn.vjudge.net/problem/POJ-2142 题意 自己看题吧,懒得解释 思路 第一部分就是扩展欧几里德 接下来是根据 $ x=x_0+kb', y=y_0- ...

  6. POJ2142——The Balance

    刚学习的扩展欧几里得算法,刷个水题 求解  线性不定方程 和  模线性方程 求方程 ax+by=c 或 ax≡c (mod b) 的整数解 1.ax+by=gcd(a,b)的一个整数解: <sp ...

  7. poj2142 The Balance 扩展欧几里德的应用 稍微还是有点难度的

    题目意思一开始没理解,原来是 给你重为a,b,的砝码 求测出 重量为d的砝码,a,b砝码可以无限量使用 开始时我列出来三个方程 : a*x+b*y=d; a*x-b*y=d; b*y-ax=d; 傻眼 ...

  8. POJ2142:The Balance——题解

    http://poj.org/problem?id=2142 题目大意:有一天平和两种数量无限的砝码(重为a和b),天平左右都可以放砝码,称质量为c的物品,要求:放置的砝码数量尽量少:当砝码数量相同时 ...

  9. Sample a balance dataset from imbalance dataset and save it(从不平衡数据中抽取平衡数据,并保存)

    有时我们在实际分类数据挖掘中经常会遇到,类别样本很不均衡,直接使用这种不均衡数据会影响一些模型的分类效果,如logistic regression,SVM等,一种解决办法就是对数据进行均衡采样,这里就 ...

随机推荐

  1. JAVA写入TXT

    用java生成txt文件有两种方式: 1)是通过字符流(或字节流): 2)是直接调用PrintWriter类. 具体实现过程如下: 1)字符流(字节流) 代码如下: import java.io.Fi ...

  2. 质量体系建设之路---可视化的MockServer

    一. 背景 福禄网络作为一家数字权益商品及服务提供商,覆盖了我们衣食住行的各种生活场景的权益内容,对接了如支付宝.京东.银行APP各种渠道,如何能够快速的响应渠道需求,提供稳定的接口服务,这就要求我们 ...

  3. ctfshow WEB入门 信息收集 1-20

    web1 题目:开发注释未及时删除 查看页面源代码即可 web2 题目:js把鼠标右键和f12屏蔽了 方法一: 禁用JavaScript 方法二: url前面加上view-source: web3 题 ...

  4. 【leetcode】633. Sum of Square Numbers(two-sum 变形)

    Given a non-negative integer c, decide whether there're two integers a and b such that a2 + b2 = c. ...

  5. C语言中的使用内存的三段

    1.正文段即代码和赋值数据段 一般存放二进制代码和常量 2.数据堆段 动态分配的存储区在数据堆段 3.数据栈段 临时使用的变量在数据栈段 典例 若一个进程实体由PCB.共享正文段.数据堆段和数据栈段组 ...

  6. Vue API 3模板语法 ,指令

    条件# v-if# v-if 指令用于条件性地渲染一块内容.这块内容只会在指令的表达式返回 truthy 值的时候被渲染. v-show# v-show 指令也是用于根据条件展示一块内容.v-show ...

  7. 关系型数据库和非关系型数据库区别、oracle与mysql的区别

    一.关系型数据库 关系型数据库,是指采用了关系模型来组织数据的数据库.    关系模型是在1970年由IBM的研究员E.F.Codd博士首先提出的,在之后的几十年中,关系模型的概念得到了充分的发展并逐 ...

  8. 1888-jerry99的数列--factorial

    1 #define _CRT_SECURE_NO_WARNINGS 1//jerry99的数列 2 #include<bits/stdc++.h> 3 int prime[40000] = ...

  9. 【Spark】【复习】Spark入门考前概念相关题复习

    Spark考前概念相关题复习 AUthor:萌狼蓝天 哔哩哔哩:萌狼蓝天 博客园:我的文章 - 萌狼蓝天 博客:萌狼工作室 - 萌狼蓝天 (mllt.cc) 选择题 Hadoop 1.HADOOP的三 ...

  10. 联盛德 HLK-W806 (七): 兼容开发板 LuatOS Air103

    目录 联盛德 HLK-W806 (一): Ubuntu20.04下的开发环境配置, 编译和烧录说明 联盛德 HLK-W806 (二): Win10下的开发环境配置, 编译和烧录说明 联盛德 HLK-W ...