HDU - 4803 Poor Warehouse Keeper (贪心模拟)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lpeaceminusone/article/details/83105456

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 <= 109) 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等比例增加。

第二个按一下,单价变化,x不变, y加一。

#include<set>
#include<map>
#include<stack>
#include<queue>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define ll long long
#define inf 0x3f3f3f
using namespace std;
int main(){
    int n,m;
    while(~scanf("%d%d",&n,&m)){
     if(n>m){
        printf("-1\n");continue;
     }
     double x=((m+0.99999)*1.0)/(n*1.0);
     double tmp=1;
     int ans=(n-1);
     for(int i=1;i<=n;i++){
        double a=i*x;
        int c=int(a-tmp);
        ans+=c;
        tmp+=c;
        tmp=tmp*(i+1)/i;
     }
        printf("%d\n",ans);
    }
}

猜你喜欢

转载自blog.csdn.net/lpeaceminusone/article/details/83105456
今日推荐