Problem Statement
Little Limak wants to show his parents that he's responsible and independent. He decided to move out and live on his own, at least for some time. While living alone, he has to eat every day. Living alone comes with some other expenses as well. More precisely, Limak will eat 1 fruit and spend x dollars each day he lives on his own.
Currently Limak has f fruits and d dollars. Limak can buy more pieces of fruit in the local store. The store sells 1 fruit for p dollars, and Limak can purchase arbitrarily many pieces of fruit there.
You are given the
Definition
- Class:
- SellingFruits
- Method:
- maxDays
- Parameters:
- int, int, int, int
- Returns:
- int
- Method signature:
- int maxDays(int x, int f, int d, int p)
- (be sure your method is public)
Notes
- Note the unusual Time Limit.
- For the provided constraints, it can be proved that the answer will fit into a 32-bit signed integer.
Constraints
- x, f, d and p will each be between 1 and 2,000,000,000, inclusive.
Examples
3
5
100
10
Returns: 11
Limak needs one fruit and 3 dollars for each day. He starts with 5 fruits and 100 dollars. The store sells additional fruit at 10 dollars a piece. Limak should buy 6 additional pieces of fruit from the store. This will leave him with 5+6 = 11 pieces of fruit and 100-6*10 = 40 dollars. That will allow Limak to live on his own for 11 days. After 11 days he will be left with no fruit and 7 dollars. Limak is unable to live on his own for longer. If he buys one more piece of fruit (to have a total of 12), he will only be left with 34 dollars, which is not enough for 12 days.
2
17
20
1
Returns: 10
In 10 days Limak will eat 10 fruits and will spend 20 dollars. He is left with 7 fruits but he has no money.
1
97
98
1
Returns: 97
16
4
8
2
Returns: 0
In this example Limak needs 1 fruit and 16 dollars for a day. He doesn't have enough money to live one day on his own, so the answer is 0.
17
1
2000000000
4
Returns: 95238095
1
1996245611
1999990159
123
Returns: 1996275808
15000000
100
2000000000
1
Returns: 133
1
1000000000
2000000000
1000000000
Returns: 1000000000
1
1
1
1
Returns: 1
1
1
1
2000000000
Returns: 1
1
1
2000000000
1
Returns: 1000000000
1
1
2000000000
2000000000
Returns: 1
1
2000000000
1
1
Returns: 1
1
2000000000
1
2000000000
Returns: 1
1
2000000000
2000000000
1
Returns: 2000000000
1
2000000000
2000000000
2000000000
Returns: 2000000000
2000000000
1
1
1
Returns: 0
2000000000
1
1
2000000000
Returns: 0
2000000000
1
2000000000
1
Returns: 1
2000000000
1
2000000000
2000000000
Returns: 1
2000000000
2000000000
1
1
Returns: 0
2000000000
2000000000
1
2000000000
Returns: 0
2000000000
2000000000
2000000000
1
Returns: 1
2000000000
2000000000
2000000000
2000000000
Returns: 1
23155
100
1123532000
1
Returns: 48520
23155
100
1923532000
1000000
Returns: 1977
23155
100
1923532000
200000000
Returns: 109
142857142
1
2000000000
1
Returns: 13
333333333
1
2000000000
12
Returns: 5
500000000
1
2000000000
123456789
Returns: 3
2000000000
1
1999999999
1
Returns: 0
2000000000
1
1999999999
2000000000
Returns: 0
2
424238336
100714
4844
Returns: 50357
1
3
521595369
10
Returns: 47417763
2
1801979803
9
1059961394
Returns: 4
1653377374
10
1734575199
38664371
Returns: 1
2
137806863
66050
6
Returns: 33025
2
4
33448
6
Returns: 4184
31852
356426809
9
103612
Returns: 0
2
7
10
80155
Returns: 5
1
62460
8
603570493
Returns: 8
10815
894429690
221558441
2
Returns: 20486
2
6
8
7
Returns: 4
7
24972
52553
3
Returns: 7507
10
9
1373226341
7
Returns: 80778023
2
2
1622597489
76178
Returns: 21301
39125
5
73264
8
Returns: 1
2
1244316438
213975408
2
Returns: 106987704
1
102237
2
10
Returns: 2
1432114614
102569
58873
10
Returns: 0
2
1
32844
601385645
Returns: 1
1
3
5
740759356
Returns: 3
97429
1529195747
101663
116374
Returns: 1
1
121521
6859
7817
Returns: 6859
1473442063
117253
1431419380
34776
Returns: 0
7684931
87173
10
1501252997
Returns: 0
1
1
82227
31115
Returns: 3
7
588219757
34248
97901
Returns: 4892
7
66948
2
88346
Returns: 0
1
1335939812
1065311706
8
Returns: 1065311706
2
780821397
62325
5817
Returns: 31162
3
34395
1197352299
34431
Returns: 69164
2
7
8
6
Returns: 4
2
2
10
3
Returns: 3
3
1
7
7
Returns: 1
2
8
10
3
Returns: 5
8
6
10
3
Returns: 1
1
4
8
7
Returns: 4
3
2
10
5
Returns: 2
3
1
4
7
Returns: 1
4
1
3
7
Returns: 0
1
5
8
7
Returns: 5
4
8
5
6
Returns: 1
2
8
5
5
Returns: 2
9
7
9
9
Returns: 1
1
5
10
3
Returns: 6
1
3
7
7
Returns: 3
80
15116246
1313059405
8325
Returns: 15128591
80
15116246
1313059404
8325
Returns: 15128590
80
15116246
1313059406
8325
Returns: 15128591
79
15116246
1313059405
8325
Returns: 15130391
81
15116246
1313059405
8325
Returns: 15126791
80
15116245
1313059405
8325
Returns: 15128590
80
15116247
1313059405
8325
Returns: 15128591
1
1000000000
2000000000
1
Returns: 1500000000
200000000
500
1215752193
200000000
Returns: 6
1000000000
1000000000
1999999999
11111
Returns: 1
2
2000000000
200
10
Returns: 100
2000000000
2000000000
23
2000000000
Returns: 0
1999999999
1999999999
2000000000
1
Returns: 1
100
1000000000
1000000000
1000000000
Returns: 10000000
1
1000000000
1000000000
1
Returns: 1000000000
2000000000
2000000000
1000000000
20
Returns: 0
20000000
20000000
2000000000
1
Returns: 100
100
5
100
10
Returns: 1
100000
10000000
1000
1000
Returns: 0
1999999997
1
2000000000
1999999996
Returns: 1
2000000000
2000000000
1000000000
1021
Returns: 0
3
2000000000
2000000000
2000000000
Returns: 666666666
50000
60000
10000000
1
Returns: 200