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
把握的原则是,x加1对单价没什么影响,y加1才会提高单价,所以策略是先让y加,不能再加的时候让x加
ac代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define esp 0.000001
using namespace std;
int main()
{
int x,y;
while(scanf("%d%d",&x,&y)!=EOF)
{
double i,j=1;
double now=1;
int step=0;
int Max;
for(i=1;i<=x;i++,step++)
{
j=now*i;
if(j>=y&&j<y+1) break;
for(;j<y+1;)
{
Max=(y+1)*i/x-j-esp;
now=j/i;
if((j+1)/i*x>=y+1) break;
j+=Max;
step+=Max;
if(j>=y&&j<y+1) goto k;
}
}
k:
if(x>y) printf("-1\n");
else
printf("%d\n",step);
}
return 0;
}