这个答案将展示如何获得正确的封闭公式的通用方法,而不是给出“在这里,它有效”的公式。
要使用Stars and Bars formula 和Inclusion Exclusion 得到一个接近的公式,您需要找到方程的解数:
x1 + x2 = n
s.t.
(1) 0 <= x1 <= A
(2) 0 <= x2 <= B
表示:
W(0) = #solutions to the problem regardless of restrictions
W(1) = #solutions to the problem with (1) is NOT correct (x1 > A)
W(2) = #solutions to the problem with (2) is NOT correct (x2 > B)
W(1,2) = #solutions to the problem with both (1,2) NOT correct (x1 > A and X2 > B)
从包含排除原则,我们上面形式化的问题的答案是:
E = W(0) - (W(1) + W(2)) + W(1,2)
剩下的就是给W(...) 提供公式,为此我们将使用星形和条形。
使用无约束方程和条形星形定理2:
W(0) = Choose(N + 2 - 1, 2 - 1) = Choose(N + 1, 1) = N + 1
为了计算 W(1) 和 W(2),我们强制 x1/x2 为 A+1 或 B+1,然后照常进行,得到方程:
x1 + x2 = N - A - 1
x1 + x2 = N - B - 1
和解的数量是(再次使用定理 2):
W(1) = Choose(N - A - 1 + 2 - 1, 1) = Chose(N-A,1) = max{N-A,0}
W(2) = (similarly) = max{N-B,0}
对于 W(1,2),我们设置两者并继续得到:
W(1,2) = Choose(N - A - 1 - B -1 + 2 - 1) = Choose(N-A-B-1,1) = max{N-A-B-1,0}
将所有内容加起来得到最终公式:
E = W(0) - (W(1) + W(2)) + W(1,2) =
= N + 1 - max{N-A,0} - max{N-B,0} + max{N-A-B-1,0}
在你的例子中是:
E = 9 + 1 - 3 - 3 + 0 = 4