Poor Warehouse Keeper HDU - 4803(思维)

本文介绍了一个涉及仓库记录调整的算法问题,目标是最小化按钮点击次数以达到预期的屏幕显示数值。通过逆向工程和精确跳跃策略,文章详细阐述了解决方案的实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

Jenny is a warehouse keeper. He writes down the entry records everyday. The record is shown on a screen, as follow:

There are only two buttons on the screen. Pressing the button in the first line once increases the number on the first line by 1. The cost per unit remains untouched. For the screen above, after the button in the first line is pressed, the screen will be:

The exact total price is 7.5, but on the screen, only the integral part 7 is shown.
Pressing the button in the second line once increases the number on the second line by 1. The number in the first line remains untouched. For the screen above, after the button in the second line is pressed, the screen will be:

Remember the exact total price is 8.5, but on the screen, only the integral part 8 is shown.
A new record will be like the following:

At that moment, the total price is exact 1.0.
Jenny expects a final screen in form of:

Where x and y are previously given.
What’s the minimal number of pressing of buttons Jenny needs to achieve his goal?
Input
There are several (about 50, 000) test cases, please process till EOF.
Each test case contains one line with two integers x(1 <= x <= 10) and y(1 <= y <= 10 9) separated by a single space - the expected number shown on the screen in the end.
Output
For each test case, print the minimal number of pressing of the buttons, or “-1”(without quotes) if there’s no way to achieve his goal.
Sample Input
1 1
3 8
9 31
Sample Output
0
5
11

Hint
For the second test case, one way to achieve is:
(1, 1) -> (1, 2) -> (2, 4) -> (2, 5) -> (3, 7.5) -> (3, 8.5)

题意

一个屏幕两个按钮一上一下对应x,y。
上面按钮 y` = (1+x)/x*y x = x+1; 公式1
下面按钮 y`` = y+1 x 不变 公式2
问你最小次数调到题给x,y 。

解释

操作1的次数是固定的,次数为x-1,问题是如何少使用操作2到达终点。
策略是每次都通过终点和新的起点倒推,算出新的跳跃落点位置,然后通过操作2走到起跳点。
从终点倒推,由于显示只有整数,所以取(y+1-eps),根据公式1,公式2,递推得y=(x+1)/x y。然后写出多条,左右两边分别相乘,约去,得Yk= Xk/x1y1。带入(x,y+1-eps)整理得到y1 =(y+1-eps)*x1/x;然后比较与X1距离,差值代表操作二次数,为了控制精度,选择每次可以选择在精准跳跃落点跳,故向下取整,然后操作1起跳。重复过程x-1次落于终点左右分别小于2区间内,但不知道是落在左侧或右侧,由于已经不能操作1,使用操作2,如果不够就走过去,够按照不够的代码,步长为0,对结果无影响。

#include <cstdio>
#include <cmath>
double const eps = 1e-5;
int main(){
	double x, y;
	while(~scanf("%lf %lf", &x, &y)){
		double y1 = 1;
		double x1;
		int ans = 0;
		int dis; 
		if(y <x){
			printf("-1\n");
			continue;
		}
		else{
			for(x1 = 1; x1 < x; x1++){
				double now = (y+1-eps)*x1/x;
				dis = floor(now-y1);
				ans += dis;
				y1 += dis;
				ans++;
				y1 +=y1/x1;
				
			}
			ans += floor(y+1-eps-y1);
			printf("%d\n", ans);
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值