A remote relay station keeps exactly three backup drones — Drone A, Drone B, and Drone C — on standby. A technician has n identical power cells and must install every single cell into one of the three drones' battery bays; each cell goes into exactly one drone, and all n cells must be used. Every drone's bay can safely hold anywhere from 0 up to cap power cells (inclusive) — installing more than cap cells into one bay risks an overheat and is not allowed.
Because the three drones are individually identified, two installation plans are considered different whenever any single drone ends up holding a different number of cells, even if the other two drones' counts are simply swapped between plans. Determine how many distinct installation plans let the technician use all n cells without ever exceeding a drone's cap.
A single line containing two space-separated integers n and cap.
Print a single integer — the number of distinct ways to distribute all n power cells among the three drones so that every drone receives between 0 and cap cells, inclusive.
Example 1
Input
5 2
Expected
3
Explanation
With n=5 cells and cap=2, each drone can hold at most 2 cells, so the three counts must sum to 5 while none exceeds 2. The only possible split (up to which drone gets which count) is two drones at the cap of 2 and one drone at 1. Since the drones are distinguishable, this pattern can be arranged in 3 ways depending on which drone gets only 1 cell, giving 3 total plans.
Example 2
Input
3 3
Expected
10
Explanation
With cap=3 and only n=3 cells to place, no drone can possibly exceed the cap no matter how the cells are split (the most any single drone could receive is all 3, which is still within the cap). So every ordered way of writing 3 as a1+a2+a3 with each ai>=0 is valid; there are C(3+2,2) = 10 such ordered triples, so the answer is 10.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →