In the Equation Hall of Prime City, the Mathematician seeks the number of non-negative integer solutions to x1 + x2 + x3 = n where each xi has an upper bound. The inclusion-exclusion principle extends Stars and Bars to handle these constraints. "First count all solutions without bounds using Stars and Bars," the Mathematician says. "Then subtract solutions where any variable exceeds its bound. Add back solutions where two variables exceed their bounds, and so on." Given n and three upper bounds b1, b2, b3, count the number of non-negative integer solutions to x1 + x2 + x3 = n where 0 <= xi <= bi. Output the count. Constraints: 0 <= n <= 10^6, 0 <= b1, b2, b3 <= 10^6 Input: 5 2 2 2 Output: 3 Input: 6 3 3 3 Output: 7
Constraints:
0 <= n, b1, b2, b3 <= 10^6
Tags:
