Poor Warehouse Keeper

Poor Warehouse Keeper

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

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 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)

Source

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

Recommend

liuyiding

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdlib>
 4 #include<cstdio>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<queue>
 8 #include<map>
 9 #include<set>
10 #include<string>
11 //#include<pair>
12
13 #define N 1005
14 #define M 1000005
15 #define mod 1000000007
16 #define inf 0x3f3f3f3f
17 //#define p 10000007
18 #define mod2 100000000
19 #define ll long long
20 #define LL long long
21 #define maxi(a,b) (a)>(b)? (a) : (b)
22 #define mini(a,b) (a)<(b)? (a) : (b)
23
24 using namespace std;
25
26 ll mul=3628800;
27 ll x,y;
28 ll ans;
29 ll tmi,tma;
30 ll dmi,dma;
31 ll dan;
32 ll num,tot;
33
34 void ini()
35 {
36     ans=0;
37     tmi=y*mul;
38     tma=(y+1)*mul;
39     dmi=tmi/x;
40     dma=tma/x;
41     num=1;
42     dan=mul;
43     tot=mul;
44 }
45
46
47 void solve()
48 {
49     ans=x-1;
50     ll k1,k2;
51     ll te;
52     if(dan>=dma){
53         ans=-1;return;
54     }
55     for(num=1;num<=x;num++){
56         te=mul/num;
57         if(dan>=dmi) break;
58         k1=(dmi-dan+te-1)/te;
59         k2=(dma-dan+te-1)/te;
60       //  printf(" num=%I64d ans=%I64d dan=%I64d dmi=%I64d dma=%I64d te=%I64d k1=%I64d k2=%I64d\n",
61 //               num,ans,dan,dmi,dma,te,k1,k2);
62         if(k1!=k2){
63             ans+=k1;
64             dan+=k1*mul/num;
65             return;
66         }
67         else{
68             dan+=(k1-1)*te;
69             ans+=(k1-1);
70         }
71     }
72 }
73
74 void out()
75 {
76     printf("%I64d\n",ans);
77 }
78
79 int main()
80 {
81     //freopen("data.in","r",stdin);
82     //freopen("data.out","w",stdout);
83     //scanf("%d",&T);
84    // for(int ccnt=1;ccnt<=T;ccnt++)
85    // while(T--)
86     while(scanf("%I64d%I64d",&x,&y)!=EOF)
87     {
88         //if(n==0 && k==0 ) break;
89         //printf("Case %d: ",ccnt);
90         ini();
91         solve();
92         out();
93     }
94
95     return 0;
96 }
时间: 2024-10-10 03:41:46

Poor Warehouse Keeper的相关文章

HDU 4803 Poor Warehouse Keeper(贪心)

http://acm.hdu.edu.cn/showproblem.php?pid=4803 贪心的策略是,每次尽量加价格,加到能满足条件的最大值,然后加一下数量,这样反复直到到达答案. 然后加到满足条件最大值一步不能模拟,可以推一下公式就能直接算出来了 代码: #include <stdio.h> #include <string.h> const double eps = 1e-9; double x, y; int main() { while (~scanf("%

BNUOJ 34025 -Poor Warehouse Keeper(贪心)

题目:BNUOJ 34025 -Poor Warehouse Keeper(贪心) 题目大意:有一个商品的信息表,上面是数量,下面是总价,然后旁边各有一个按钮.上面的数量按钮按一下数量就加1,然后价格对应的也要在加上一个当前的单价.下面的按钮按一下的话,就对应的总价加1.初始状态是 1 1,然后给出终点状态,问能否得到.可以的话输出最少要按几次按钮,否则输出-1:总价每次输出都是下取整. 解题思路:如果想要尽快的得到总点状态的值,那么就应该按总价的按钮,因为只有总价变大了,商品对因的单价就上升了

hdu 4803 Poor Warehouse Keeper(贪心+数学)

题目链接:hdu 4803 Poor Warehouse Keeper 题目大意:有以个屏幕可以显示两个值,一个是数量x,一个是总价y.有两种操作,一种是加一次总价,变成x,x+y:一种是加一个数量,这要的话总价也会相应加上一个的价钱,变成x+1,y+y/x.总价显示的为取整后的整数,小数部分忽略.给定一个目标x,y,初始状态为1,1,求最少需要多少次可以目标状态,不可以达到的话输出-1. 解题思路:如果是加一次总价的话,单价就在变大:如果是加一次数量的话,单价是不变的.总而言之,单价是只会往上

HDU 4803 Poor Warehouse Keeper

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4803 解题报告:有一个记录器,一共有两个按钮,还有两行屏幕显示,第一行的屏幕显示的是数目,第二行的屏幕显示的是总价,按第一行的按钮表示单价不变,数量加1,同时第二行的总价会根据当前的单价进行相应的增加,然后按一下第二行的按钮表示数量不变,总价加1,这样的话单价就变大了.现在给出这个屏幕上显示的初始的第一行的是1,第二行的是1,然后再给出结束的时候第一行是x,第二行是y,让你求要从初始的变成最后的至少

hdu4803 Poor Warehouse Keeper

给定两个数字显示板A, B和两个按钮,按下按钮使得其对应数字显示增加1. 另,B显示板只显示其实际值的整数部分.两显示器对应的实际值有一个比值p. 按下A板上的按钮, A板上显示的数增加1到a + 1, p不变,B板上的数字更新为(int)(b * (a + 1) / a). 按下B板上的按钮,A板上上显示的数值不变,B 板加1 到b + 1,p更新为(b + 1) / a. 给定目标状态(a1, b1),问至少经过多少次按下按钮可从初始状态(1, 1)到达该状态. 若不可达输出-1. 显然按下

【HDU4803】Poor Warehouse Keeper 数学+贪心

#include <stdio.h> int main() { puts("转载请注明出处[vmurder]谢谢"); puts("网址:blog.csdn.net/vmurder/article/details/43450731"); } 题意: 初始状态为:1个物品,总价为1. 目标状态为:x个物品,总价为y. 操作A:变为x+1,y+y/x.(y不取整) 操作B:变为x,  y+1 问最少多少步可以达成条件?(最后操作结束后对y取整) 如果不行输出

【贪心】hdu4803 Poor Warehouse Keeper

题意:一开始有1个物品,总价是1.你的一次操作可以要么使得物品数量+1,总价加上当前物品的单价.要么可以使得总价+1,物品数量不变.问你最少要几次操作从初始状态到达有x个物品,总价是y的状态.这里的y可以有小数点后的部分,会抹去. 如果x>y,显然无解. 因为不管怎样操作,物品的单价是单调不下降的.所以一个naive的贪心策略是先用第二种操作,将物品提升到最大的可能单价(<(y+1)/x),然后再用第一种操作操作到不能再操作为止,剩余的部分用第二种补齐.然而这是不对的. 我们发现,第一种操作,

ZOJ 2601 Warehouse Keeper(费用流)

ZOJ 2601 Warehouse Keeper The company where Jerry works owns a number of warehouses that can be used to store various goods. For each warehouse the types of goods that can be stored in this warehouse are known. To avoid problems with taxes, each ware

2015.11.8 ---2013.南京

GPA 算绩点 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 using namespace std; 6 7 int n; 8 string p[15] = {"A","A-","B+","B","B-", 9 "C+"