首页 > 代码库 > 2013nanjignB
2013nanjignB
B - Poor Warehouse Keeper
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64uAppoint description:
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?
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.
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 13 89 31
Sample Output
0511
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #include<vector> 7 #define M(a,b) memset(a,b,sizeof(a)) 8 #include<map> 9 #include<stdlib.h>10 #define eps 1e-511 using namespace std;12 13 double x,y;14 15 int main()16 {17 while(scanf("%lf%lf",&x,&y)==2)18 {19 double lim = (y+1)/x-eps;20 double pri = 1;21 int num = 1;22 int tim = 0;23 double all = 1;24 if(x>y) {puts("-1"); continue;}25 while(1)26 {27 if(all-y>0||fabs(all-y)<eps)28 break;29 //cout<<lim<<endl;30 int tmp = (int)((lim - pri)/(1.0/num));31 pri+=tmp*(1.0/num);32 //cout<<pri<<‘ ‘<<tmp<<endl;33 tim+=tmp;34 all+= tmp;35 if(tmp==0)36 {37 int tm = (int)(1.0/(lim-pri)-num)+1;38 if(tm>(y+1-eps-all)/pri)39 tm = (int)((y+1-eps-all)/pri);40 num+=tm;41 tim+=tm;42 all+=pri*tm;43 }44 // cout<<tim<<endl;45 //cout<<all<<endl;46 }47 printf("%d\n",tim);48 }49 return 0;50 }
2013nanjignB
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。