HDU 4803Poor Warehouse Keeper(数学题)

来源:互联网 发布:知乎辩护人一样的电影 编辑:程序博客网 时间:2024/05/22 08:27

Poor Warehouse Keeper

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4558    Accepted Submission(s): 1173


Problem Description
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 13 89 31
 

Sample Output
0511
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)
 

Source
2013ACM/ICPC亚洲区南京站现场赛——题目重现

把上面那个按钮 称为按钮1;

下面那个按钮  称为按钮2;


题意:两种操作,如果按下按钮1,会使数量+1,单价不变,总价=(数量+1)*单价

           如果按下按钮2,会使总价加1,数量不变,单价=(总价+1)/数量;

现在问至少多少步可以使得   按钮1数字为x, 按钮2数字为y;

想法:已知只有按按钮2才造成单价上升,而最后总单价可以求出
设总单价为m,a1 ,a2 ,a3.......为在数量为1,2,,3.......时增加的单价系数
m=(1+a1/1+a2/2+a3/3+。。。)
现在我们要求出系数a的和
y+1-1e-5为总价=x*m=(1*x+a1/1*x+a2/2*x+a3/3*x......)
y+1-1e-5-x=a1*x/1+a2*x/2+a3*x/3+.......
让y=y+1-1e-5-x;
有1>1/2>1/3>1/4
a1约=(y*1/x);
a2约=(y-a1*x/1)*2/x;
y<1结束
代码一:(通俗版)
#include <cstdio>#include <cmath>#define eps 1e-5using namespace std;double a[15],x,y;int ans,n;int main(){    while (scanf("%lf%lf",&x,&y)!=EOF)    {        ans=(int)(x-1);        y-=x-1+eps;        if (y<0)        {            printf("-1\n");            continue;        }        for (int i=1; i<=x; i++) a[i]=x/i;        for (int i=1; i<=x; i++)        {            n=(int)(y/a[i]);            ans+=n;            y-=n*a[i];            if (y<1) break;        }        printf("%d\n",ans);    }    return 0;}

代码二:(精简版)
#include <iostream>#include <cstdio>#include <cmath>#include <cstdio>typedef long long ll;const double eps=1e-5;using namespace std;int main(){    double x,y;    while(scanf("%lf%lf",&x,&y)!=EOF)    {      if(x>y)      {        printf("-1\n");        continue;      }      double s=(y+1-eps)/x;      int cnt=(int)x-1;      double tmp=1;      for(int i=1;i<=(int)x;i++)      {        int t=(int)(s*i-tmp);        tmp+=t;        cnt+=t;        tmp=tmp/i*(i+1);      }     printf("%d\n",cnt);    }    return 0;}



原创粉丝点击