Stars and Bars

Distribute n identical items (stars) into k distinct boxes by placing k−1 dividers (bars). Drag a bar to change the distribution; the count C(n+k−1, k−1) stays fixed for given n, k.

Non-negative: C(n+k−1, k−1) Positive: C(n−1, k−1) Upper bound: inclusion–exclusion
Star (item)
Bar (drag me)
Forced / pre-placed
Over-cap (bad)
n + k − 1
choose k − 1
total arrangements
current sum
valid? (this mode)

What is happening

Drag any orange bar left or right. The boxes between bars resize; the sum always equals n.

Complexity

OperationTimeNotes
C(n+k−1, k−1) multiplicative loopO(min(k, n))exact, divide-as-you-go
C(m, r) mod p, precomputed factorialsO(1)after O(N) precompute
Positive count C(n−1, k−1)O(min(k, n))needs n ≥ k
Upper-bound count (uniform cap, PIE)O(k)≤ k+1 signed terms
Upper-bound count (varied caps, subset PIE)O(2ᵏ)prune forced > n
Brute-force enumerate all solutionsO(C(n+k−1, k−1))testing only

Operation Log