Statistics

Problem Statement for "SellingFruits"

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

  9. 1

    1

    1

    1

    Returns: 1

  10. 1

    1

    1

    2000000000

    Returns: 1

  11. 1

    1

    2000000000

    1

    Returns: 1000000000

  12. 1

    1

    2000000000

    2000000000

    Returns: 1

  13. 1

    2000000000

    1

    1

    Returns: 1

  14. 1

    2000000000

    1

    2000000000

    Returns: 1

  15. 1

    2000000000

    2000000000

    1

    Returns: 2000000000

  16. 1

    2000000000

    2000000000

    2000000000

    Returns: 2000000000

  17. 2000000000

    1

    1

    1

    Returns: 0

  18. 2000000000

    1

    1

    2000000000

    Returns: 0

  19. 2000000000

    1

    2000000000

    1

    Returns: 1

  20. 2000000000

    1

    2000000000

    2000000000

    Returns: 1

  21. 2000000000

    2000000000

    1

    1

    Returns: 0

  22. 2000000000

    2000000000

    1

    2000000000

    Returns: 0

  23. 2000000000

    2000000000

    2000000000

    1

    Returns: 1

  24. 2000000000

    2000000000

    2000000000

    2000000000

    Returns: 1

  25. 23155

    100

    1123532000

    1

    Returns: 48520

  26. 23155

    100

    1923532000

    1000000

    Returns: 1977

  27. 23155

    100

    1923532000

    200000000

    Returns: 109

  28. 142857142

    1

    2000000000

    1

    Returns: 13

  29. 333333333

    1

    2000000000

    12

    Returns: 5

  30. 500000000

    1

    2000000000

    123456789

    Returns: 3

  31. 2000000000

    1

    1999999999

    1

    Returns: 0

  32. 2000000000

    1

    1999999999

    2000000000

    Returns: 0

  33. 2

    424238336

    100714

    4844

    Returns: 50357

  34. 1

    3

    521595369

    10

    Returns: 47417763

  35. 2

    1801979803

    9

    1059961394

    Returns: 4

  36. 1653377374

    10

    1734575199

    38664371

    Returns: 1

  37. 2

    137806863

    66050

    6

    Returns: 33025

  38. 2

    4

    33448

    6

    Returns: 4184

  39. 31852

    356426809

    9

    103612

    Returns: 0

  40. 2

    7

    10

    80155

    Returns: 5

  41. 1

    62460

    8

    603570493

    Returns: 8

  42. 10815

    894429690

    221558441

    2

    Returns: 20486

  43. 2

    6

    8

    7

    Returns: 4

  44. 7

    24972

    52553

    3

    Returns: 7507

  45. 10

    9

    1373226341

    7

    Returns: 80778023

  46. 2

    2

    1622597489

    76178

    Returns: 21301

  47. 39125

    5

    73264

    8

    Returns: 1

  48. 2

    1244316438

    213975408

    2

    Returns: 106987704

  49. 1

    102237

    2

    10

    Returns: 2

  50. 1432114614

    102569

    58873

    10

    Returns: 0

  51. 2

    1

    32844

    601385645

    Returns: 1

  52. 1

    3

    5

    740759356

    Returns: 3

  53. 97429

    1529195747

    101663

    116374

    Returns: 1

  54. 1

    121521

    6859

    7817

    Returns: 6859

  55. 1473442063

    117253

    1431419380

    34776

    Returns: 0

  56. 7684931

    87173

    10

    1501252997

    Returns: 0

  57. 1

    1

    82227

    31115

    Returns: 3

  58. 7

    588219757

    34248

    97901

    Returns: 4892

  59. 7

    66948

    2

    88346

    Returns: 0

  60. 1

    1335939812

    1065311706

    8

    Returns: 1065311706

  61. 2

    780821397

    62325

    5817

    Returns: 31162

  62. 3

    34395

    1197352299

    34431

    Returns: 69164

  63. 2

    7

    8

    6

    Returns: 4

  64. 2

    2

    10

    3

    Returns: 3

  65. 3

    1

    7

    7

    Returns: 1

  66. 2

    8

    10

    3

    Returns: 5

  67. 8

    6

    10

    3

    Returns: 1

  68. 1

    4

    8

    7

    Returns: 4

  69. 3

    2

    10

    5

    Returns: 2

  70. 3

    1

    4

    7

    Returns: 1

  71. 4

    1

    3

    7

    Returns: 0

  72. 1

    5

    8

    7

    Returns: 5

  73. 4

    8

    5

    6

    Returns: 1

  74. 2

    8

    5

    5

    Returns: 2

  75. 9

    7

    9

    9

    Returns: 1

  76. 1

    5

    10

    3

    Returns: 6

  77. 1

    3

    7

    7

    Returns: 3

  78. 80

    15116246

    1313059405

    8325

    Returns: 15128591

  79. 80

    15116246

    1313059404

    8325

    Returns: 15128590

  80. 80

    15116246

    1313059406

    8325

    Returns: 15128591

  81. 79

    15116246

    1313059405

    8325

    Returns: 15130391

  82. 81

    15116246

    1313059405

    8325

    Returns: 15126791

  83. 80

    15116245

    1313059405

    8325

    Returns: 15128590

  84. 80

    15116247

    1313059405

    8325

    Returns: 15128591

  85. 1

    1000000000

    2000000000

    1

    Returns: 1500000000

  86. 200000000

    500

    1215752193

    200000000

    Returns: 6

  87. 1000000000

    1000000000

    1999999999

    11111

    Returns: 1

  88. 2

    2000000000

    200

    10

    Returns: 100

  89. 2000000000

    2000000000

    23

    2000000000

    Returns: 0

  90. 1999999999

    1999999999

    2000000000

    1

    Returns: 1

  91. 100

    1000000000

    1000000000

    1000000000

    Returns: 10000000

  92. 1

    1000000000

    1000000000

    1

    Returns: 1000000000

  93. 2000000000

    2000000000

    1000000000

    20

    Returns: 0

  94. 20000000

    20000000

    2000000000

    1

    Returns: 100

  95. 100

    5

    100

    10

    Returns: 1

  96. 100000

    10000000

    1000

    1000

    Returns: 0

  97. 1999999997

    1

    2000000000

    1999999996

    Returns: 1

  98. 2000000000

    2000000000

    1000000000

    1021

    Returns: 0

  99. 3

    2000000000

    2000000000

    2000000000

    Returns: 666666666

  100. 50000

    60000

    10000000

    1

    Returns: 200


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: