CT943

Solve x1+x2+...+xk = n With xi <= b (Inclusion-Exclusion)

HardAcceptance: 0.0%

In the Advanced Combinatorics class of Prime City, the Professor presents a generalization of bounded Stars and Bars: k variables, each with its own upper bound, summing to n. The inclusion-exclusion principle handles the general case. "For k variables with bounds bi, iterate over all 2^k subsets of variables that exceed their bounds," the Professor says. "For each subset S, subtract (or add back) the count where each variable in S exceeds its bound. The sign alternates with the size of S." Given k, n, and k upper bounds b1, b2, ..., bk, count the number of non-negative integer solutions to x1 + x2 + ... + xk = n where 0 <= xi <= bi. Output the count. Constraints: 1 <= k <= 20, 0 <= n <= 10^6, 0 <= bi <= 10^6 Input: 3 5 2 2 2 Output: 3 Input: 2 10 3 3 Output: 0

Constraints:

1 <= k <= 20, 0 <= n <= 10^6, 0 <= bi <= 10^6

Tags:

stars-and-bars inclusion-exclusion combinatorics math
Loading...
Test Cases:No test cases
No test cases available.