
Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
We can enumerate the number of candies of one particular child, let it be <code>i</code> which means <code>0 <= i <= min(limit, n)</code>.
Suppose the 2nd child gets <code>j</code> candies. Then <code>0 <= j <= limit</code> and <code>i + j <= n</code>.
The 3rd child will hence get <code>n - i - j</code> candies and we should have <code>0 <= n - i - j <= limit</code>.
After some transformations, for each <code>i</code>, we have <code>max(0, n - i - limit) <= j <= min(limit, n - i)</code>, each <code>j</code> corresponding to a solution. So the number of solutions for some <code>i</code> is <code>max(min(limit, n - i) - max(0, n - i - limit) + 1, 0)</code>. Sum the expression for every <code>i</code> in <code>[0, min(n, limit)]</code>.