Connection Status:
Competition Arena > SellingFruits
SRM 706 · 2016-12-07 · by Errichto · Search, Simple Math
Class Name: SellingFruits
Return Type: int
Method Name: maxDays
Arg Types: (int, int, int, int)
Problem Statement

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 ints x, f, d, and p. Compute and return the largest possible number of days Limak can live on his own.

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

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

2)
1
97
98
1
Returns: 97
3)
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.

4)
17
1
2000000000
4
Returns: 95238095

Submissions are judged against all 100 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class SellingFruits with a public method int maxDays(int x, int f, int d, int p) · 100 test cases · 2 s / 256 MB per case

Submitting as anonymous