Bounded Compositions¶
Bounded compositions are what remains after plain stars and bars stops being exact.
The base problem is still:
but now each variable has extra constraints such as:
x_i \ge L_ix_i \le U_i- some slots must be positive
- different classes of slots have different caps
This topic is the bridge between:
- Counting Basics, where lower bounds are removed by a simple shift
and
- Inclusion-Exclusion, where upper bounds are repaired by alternating-sign corrections
It also connects naturally to small-sum DP when the total S is small enough.
At A Glance¶
- Use when: identical units are distributed into ordered bins, but some bins have upper bounds or forced minimums
- Core worldview: reduce lower bounds first, then decide whether upper bounds are best handled by stars-and-bars plus PIE or by direct DP
- Main tools: shifting, stars and bars, inclusion-exclusion on violated caps, small-sum DP
- Typical complexity: formula/PIE in
O(2^n)over slot types or constraints; DP often inO(nS)orO(nSK) - Main trap: applying plain stars and bars after upper bounds already broke the model
Problem Model And Notation¶
We count ordered n-tuples of integers:
such that:
The basic variants are:
- nonnegative compositions:
x_i \ge 0 - positive compositions:
x_i \ge 1 - bounded compositions:
0 \le x_i \le U - mixed bounds:
L_i \le x_i \le U_i
The key modeling fact is:
- the bins are ordered / distinguishable
- the units are identical
So this is still a stars-and-bars family, but with extra restrictions.
From Brute Force To The Right Idea¶
Brute Force: Enumerate Every Tuple¶
A naive solution is:
- try every
n-tuple summing toS - test which ones obey the slot bounds
This is hopeless even for moderate parameters.
But the problem already has a clean algebraic shape:
The real question is only:
- what do the lower bounds and upper bounds do to the base stars-and-bars model?
The First Shift: Remove Lower Bounds First¶
If:
set:
Then:
This step is mandatory before anything else.
Lower bounds are usually easy.
The Second Shift: Upper Bounds Are The Real Difficulty¶
If after shifting you still have:
then plain stars and bars is no longer valid, because it counts solutions that overshoot some cap.
So now you must choose one of two directions:
- if the caps are small or the total
Sis small, use DP - if the number of constrained slots is small and intersections are easy, use inclusion-exclusion
The Third Shift: Count Violations Rather Than Valid Objects¶
The clean PIE viewpoint is:
- count all nonnegative solutions first
- then subtract those where at least one slot exceeds its upper bound
This is exactly the kind of overlap correction PIE is made for.
Core Invariants And Why They Work¶
1. Shifting Lower Bounds¶
Suppose:
Define:
Then:
and:
This is a bijection:
- every valid
x-tuple corresponds to exactly one nonnegativey-tuple - and vice versa
So the count is preserved.
This is why lower bounds are considered "easy" in this family.
2. Plain Stars And Bars For The Unbounded Version¶
For nonnegative solutions:
the count is:
For positive solutions:
the count becomes:
after shifting by 1.
This is the baseline count that upper bounds will later correct.
3. Inclusion-Exclusion For Upper Bounds¶
Suppose after shifting we want:
for all i.
Let A_i be the bad set where:
To count valid solutions, we compute:
Now if a subset T of variables is forced to violate the cap, then for each i \in T, write:
This reduces the total sum by:
So one |T| = t intersection contributes:
when the top is nonnegative, and 0 otherwise.
That gives the standard bounded-composition formula:
This is the cleanest proof that upper-bounded stars and bars is really just stars and bars plus PIE.
4. Why Small-Sum DP Is Often Safer¶
If:
- slot bounds vary by position
- there are several slot classes
Sis only around100or1000
then a direct DP is often clearer than writing one complicated PIE formula.
The basic DP is:
Transitions:
for every allowed value v of the next slot.
This is often the right contest choice because:
- the state meaning is explicit
- mixed bounds are easy to encode
- the risk of sign mistakes disappears
Variant Chooser¶
Use Plain Stars And Bars When¶
- there are no upper bounds
- and after shifting, every slot is simply nonnegative
This is the [Counting Basics](../counting-basics/README.md) layer.
Use Bounded-Composition PIE When¶
- many slots share the same upper bound
- or the intersection after violating some caps still has a clean closed form
- and the number of bounded slots is small enough to tolerate subset-size or subset iteration logic
Canonical smells:
- "each bin can contain at most
Kitems" - "no run can exceed
K-1" - all constrained slots look structurally similar
When every bounded slot has the same cap, the PIE often compresses from "iterate all subsets" into "sum over the number of violated slots t".
Use DP When¶
- the total sum
Sis small - bounds differ by slot
- slots come in several structural classes
- the counting model is already decomposed into a manageable number of gaps/groups
This is often the better route in olympiad-style practice problems.
Use Counting-Then-DP Hybrid When¶
- first count a skeleton combinatorially
- then count how extra mass is distributed into bounded gaps
This is exactly the shape of VOSFENCE:
- count the red/blue skeleton
- then count white-gap placements under positivity and cap constraints
Move To Full Inclusion-Exclusion Or Another Topic When¶
- upper bounds are irregular and the bad-set intersections are the real structure
- or the overlap logic is now the main difficulty
Then you are really in Inclusion-Exclusion, not just bounded compositions.
Worked Examples¶
Example 1: Positive To Nonnegative¶
Count:
Shift:
Then:
So the answer is:
This is the basic lower-bound shift.
Example 2: Uniform Upper Bound¶
Count:
Start with all nonnegative solutions:
Now subtract bad solutions where some x_i \ge 4.
For one chosen violating variable, shift by 4, leaving:
There are 3 choices of violating variable, so subtract:
For two simultaneous violations, the remaining sum would be negative, so those terms vanish.
Final answer:
This is the cleanest bounded-stars-and-bars PIE example.
Example 3: Why DP Can Be Cleaner Than PIE¶
Suppose there are:
- some forced-positive gaps
- some optional gaps
- and different gaps have different caps
You could still try to force a formula, but the DP:
often matches the statement more directly.
That is not a failure of combinatorics. It is good modeling discipline.
Example 4: VOSFENCE¶
VOSFENCE is a strong example because it splits one messy problem into two layers:
- count the red/blue skeleton by transition structure
- count bounded white-gap placements
The bounded-composition layer appears after:
- some gaps are forced positive
- every gap has cap
K-1 - the total white count is fixed
The final solution uses small DP because the totals are small and the gap classes are explicit.
Algorithms And Pseudocode¶
Shift Lower Bounds¶
for each slot i:
S -= lower_bound[i]
if S < 0:
answer = 0
else:
continue with nonnegative model
DP For Bounded Compositions¶
dp[0][0] = 1
for i in 0..n-1:
for s in 0..S:
for v in allowed_values_of_slot_i:
if s + v <= S:
dp[i+1][s+v] += dp[i][s]
With prefix sums, many such transitions can be optimized, but the conceptual baseline is this slot-by-slot DP.
PIE For Uniform Upper Bounds¶
answer = 0
for t in 0..n:
remaining = S - t * (U + 1)
if remaining < 0:
break
term = C(n, t) * C(remaining + n - 1, n - 1)
if t is even: answer += term
else: answer -= term
This is the standard bounded-stars-and-bars formula in executable form.
Implementation Notes¶
1. Lower Bounds First, Always¶
Do not mix lower-bound handling and upper-bound handling in the same step unless you are very sure of the algebra.
First shift:
- remove the minimums
Then solve the remaining nonnegative problem.
2. Plain Stars And Bars Dies The Moment Upper Bounds Matter¶
Once caps are present, the simple formula is no longer exact.
That is the most common wrong turn in this topic.
3. Negative Remaining Sum Means Zero Ways¶
After shifts or PIE corrections, if the remaining sum becomes negative, that branch contributes 0.
This sounds trivial, but it is one of the most frequent algebra-to-code mistakes.
4. DP Is Not "Less Mathematical"¶
If the slots fall into several classes and S is small, DP is often the cleanest implementation of the composition model.
Using DP here is still doing combinatorics correctly.
5. Ordered Bins Are Part Of The Model¶
These are compositions, not partitions.
So:
(2, 1, 0)and(1, 2, 0)are different unless the statement explicitly says otherwise
6. Tiny Brute Force Is Great Here Too¶
For small n, S, and U, brute-force enumeration is a very good sanity check for:
- the lower-bound shift
- the cap interpretation
- DP versus PIE agreement
7. Unequal Caps Usually Destroy The Clean Closed Formula¶
The compact alternating-sum formula is cleanest when every bounded slot behaves the same way.
If caps differ heavily by slot, the uniform t-based formula usually disappears, and the better choices are often:
- full subset PIE
- or direct DP
Practice Archetypes¶
The most common bounded-composition-shaped tasks are:
- bounded stars and bars
- distribute identical mass into gaps with caps
- count after some gaps are forced positive
- choose a skeleton first, then fill bounded gaps
- small-total counting where DP is the clean bounded-composition engine
Strong contest smells include:
- an equation like
x_1 + \cdots + x_n = S - some
x_imust be positive - some
x_icannot exceed a cap - the statement is really about gaps, runs, or separators with bounded sizes
References And Repo Anchors¶
Research sweep refreshed on 2026-04-24.
Primary / course:
- Washington CSE 312: Counting, stars and bars, inclusion-exclusion
- UCSD compositions and partitions slides
Reference:
Practice:
Repo anchors:
- ladder: Bounded Compositions ladder
- notebook refresher: Combinatorics Cheatsheet
- adjacent topic: Counting Basics
- adjacent topic: Inclusion-Exclusion