TopcoderArchiveVisit Topcoder
SRM7 Dec 2016
SRM 706

SellingFruits

Problem statement, definition, constraints, and public examples.

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.

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

  1. 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. 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.
  3. 1
    97
    98
    1
    Returns: 97
  4. 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.
  5. 17
    1
    2000000000
    4
    Returns: 95238095
  6. 1
    1996245611
    1999990159
    123
    Returns: 1996275808
  7. 15000000
    100
    2000000000
    1
    Returns: 133
  8. 1
    1000000000
    2000000000
    1000000000
    Returns: 1000000000
Back to all problems