total identical apples. Every apple must go to someone, but no neighbor will take more than cap apples. Someone may get none at all.
How many ways are there to share the apples? Two ways differ if some particular neighbor gets a different number of apples, so (1, 0, 2) and (2, 0, 1) count separately.total=4, cap=2 → 6
The valid splits are (0, 2, 2), (2, 0, 2), (2, 2, 0), (1, 1, 2), (1, 2, 1) and (2, 1, 1): six ways.
total=7, cap=2 → 0
Even 2 each adds up to only 6, so 7 cannot be handed out.
total=2, cap=5 → 6
total=4, cap=2
6
Run checks the first 3 tests in your browser. Submit checks all 74 tests on the server, hidden ones included.