POJ-2142 The Balance(扩展欧几里得)

本文详细解析了一道经典的平衡砝码问题,通过数学方法找出使用最少数量的两种不同重量的砝码来达到指定重量的最佳方案。
部署运行你感兴趣的模型镜像
The Balance
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 6098 Accepted: 2671

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

Source

 

分析
假设 a 砝码我们用了 x 个,b 砝码我们用了 y 个。那么天平平衡时,就应该满足
ax+by==c。x,y 为正时表示放在和 c 物品的另一边,为负时表示放在 c 物品的同一边。于
是题意就变成了求|x|+|y|的最小值了。x 和 y 是不定式 ax+by==c 的解。刚刚上面已经提到
了关于 x,y 的所以解的同式,即
x=x0+b/d*t
y=y0-a/d*t
穷举所有解,取|x|+|y|最小的?显然是行不通的,仔细分析:
|x|+|y|==|x0+b/d*t|+|y0-a/d*t|,我们规定 a>b(如果 a<b,我们便交换 a b),从这个式子
中,我们可以轻易的发现:|x0+b/d*t|是单调递增的,|y0-a/d*t|是单调递减的,而由于我们
规定了 a>b,那么减的斜率边要大于增的斜率,于是整个函数减少的要比增加的快,但是
由于绝对值的符号的作用,最终函数还是递增的。也就是说,函数是凹的,先减小,再增
大。那么什么时候最小呢?很显然是 y0-a/d*t==0 的时候,于是我们的最小值|x|+|y|也一定
是在 t=y0*d/a 附近了,只要在附近枚举几个值就能找到最优解了。

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <queue>
 6 #include <stack>
 7 #include <vector>
 8 #include <iostream>
 9 #include "algorithm"
10 using namespace std;
11 typedef long long LL;
12 int a,b,c,d;
13 int x,y;
14 int exgcd(int a,int b,int &x,int &y){
15     if (b==0)
16     {x=1;
17      y=0;
18      return a;
19     }
20     int d=exgcd(b,a%b,x,y);
21     int t=x;
22     x=y;
23     y=t-(a/b)*y;
24     return d;
25 }
26 int main(){
27     freopen ("balance.in","r",stdin);
28     freopen ("balance.out","w",stdout);
29     int i,j;
30     bool flag;
31     while (1)
32     {scanf("%d%d%d",&a,&b,&c);
33      if (!(a || b || c))
34       break;
35      flag=false;
36      if (a<b)
37      {swap(a,b);
38       flag=true;
39      }
40      d=exgcd(a,b,x,y);
41      x*=(c/d),y*=(c/d);
42      int t,ax,ay,zx,zy;
43      t=y/(a/d);
44      ax=abs(x+b/d*t);
45      ay=abs(y-a/d*t);
46      for (i=1;i<=10;i++)
47      {zx=abs(x+b/d*(t+i));
48       zy=abs(y-a/d*(t+i));
49       if ((zx+zy<ax+ay) || (zx+zy==ax+ay && zx*a+zy*b<ax*a+ay*b))
50       {ax=zx;
51        ay=zy;
52       }
53       zx=abs(x+b/d*(t-i));
54       zy=abs(y-a/d*(t-i));
55       if ((zx+zy<ax+ay) || (zx+zy==ax+ay && zx*a+zy*b<ax*a+ay*b))
56       {ax=zx;
57        ay=zy;
58       }
59      }
60      if (flag)
61       swap(ax,ay);
62      printf("%d %d\n",ax,ay);
63     }
64     return 0;
65 }

 

转载于:https://www.cnblogs.com/keximeiruguo/p/5971726.html

您可能感兴趣的与本文相关的镜像

PyTorch 2.6

PyTorch 2.6

PyTorch
Cuda

PyTorch 是一个开源的 Python 机器学习库,基于 Torch 库,底层由 C++ 实现,应用于人工智能领域,如计算机视觉和自然语言处理

内容概要:本文详细介绍了“秒杀商城”微服务架构的设计与实战全过程,涵盖系统从需求分析、服务拆分、技术选型到核心功能开发、分布式事务处理、容器化部署及监控链路追踪的完整流程。重点解决了高并发场景下的超卖问题,采用Redis预减库存、消息队列削峰、数据库乐观锁等手段保障数据一致性,并通过Nacos实现服务注册发现与配置管理,利用Seata处理跨服务分布式事务,结合RabbitMQ实现异步下单,提升系统吞吐能力。同时,项目支持Docker Compose快速部署和Kubernetes生产级编排,集成Sleuth+Zipkin链路追踪与Prometheus+Grafana监控体系,构建可观测性强的微服务系统。; 适合人群:具备Java基础和Spring Boot开发经验,熟悉微服务基本概念的中高级研发人员,尤其是希望深入理解高并发系统设计、分布式事务、服务治理等核心技术的开发者;适合工作2-5年、有志于转型微服务或提升架构能力的工程师; 使用场景及目标:①学习如何基于Spring Cloud Alibaba构建完整的微服务项目;②掌握秒杀场景下高并发、超卖控制、异步化、削峰填谷等关键技术方案;③实践分布式事务(Seata)、服务熔断降级、链路追踪、统一配置中心等企业级中间件的应用;④完成从本地开发到容器化部署的全流程落地; 阅读建议:建议按照文档提供的七个阶段循序渐进地动手实践,重点关注秒杀流程设计、服务间通信机制、分布式事务实现和系统性能优化部分,结合代码调试与监控工具深入理解各组件协作原理,真正掌握高并发微服务系统的构建能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值