Mid/SeniorEnumerationMath

Number of Ways to Buy Pens and Pencils

You are given an integer total indicating the amount of money you have. You are also given two integers cost1 and cost2 indicating the price of a pen and pencil respectively. You can spend part or all of your money to buy multiple quantities (or none) of each kind of writing utensil.

Return the number of distinct ways you can buy some number of pens and pencils.

Example 1
Inputtotal = 20, cost1 = 10, cost2 = 5
Output9
If pens cost 10 and pencils cost 5, the possible choices are 5 ways with 0 pens, 3 ways with 1 pen, and 1 way with 2 pens, for a total of 9 ways.
Example 2
Inputtotal = 5, cost1 = 10, cost2 = 10
Output1
Both pens and pencils cost more than the total, so the only way is to buy 0 pens and 0 pencils.

Constraints

  • 1 <= total, cost1, cost2 <= 10^6

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate