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?
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?
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.
1 1 3 8 9 31
0
5
11
For the second test case, one way to achieve is: (1, 1) -> (1, 2) -> (2, 4) -> (2, 5) -> (3, 7.5) -> (3, 8.5) 想法很简单,加满下面再加上面再加满下面,但是不好操作。
一开始想法是求出 (y+1)/x 这么个上界,二分使其小于这个上界。。还是不行,
。。比较简单又正确的做法是,求出最大的满足的上界 (y+1-eps)/x 利用当前的i<=x,可以求得最理想状态相差的y,加上即可,即使达到y+很小一部分就能达成,但是对于 (y+1-eps)里面 两者的差值还是小于1,向下取整直接为0了
#include <bits/stdc++.h> using namespace std; const double eps=1e-5; int main() { double x,y; while(~scanf("%lf%lf",&x,&y)) { if(x>y) { printf("-1\n"); continue; } double s=(y+1.0-eps)/x; double tmp=1.0; int ans=0; ans=x-1; for(int i=1;i<=(int)x;i++) { int t=(int)(s*i-tmp); ans+=t; tmp+=t; tmp=tmp/i*(i+1); } printf("%d\n",ans ); } }