count
to 0, which will hold the total number of distributions.a
given to the first child, starting from max(0, n - 2 * limit)
to min(limit, n)
.a
, calculate the range for the second child b
:
b
is max(0, n - a - limit)
.b
is min(limit, n - a)
.b
can be chosen within the range and add it to count
.count
as the total number of distributions.