Poor Warehouse Keeper HDU - 4803(思维)

题目

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;
}
发布了52 篇原创文章 · 获赞 2 · 访问量 851

猜你喜欢

转载自blog.csdn.net/qq_44714572/article/details/103948844