#P6191. Counting Non-Fighting Arrangements

    ID: 19411 Type: Default 1000ms 256MiB

Counting Non-Fighting Arrangements

Counting Non-Fighting Arrangements

John is very ingenious. He discovered that in order to prevent any brawls, there must be at least \(K\) cows between any two bulls. In other words, if bulls are placed in a line, then between every pair of consecutive bulls there must be at least \(K\) cows. John considers all bulls to be identical and all cows to be identical. Two arrangements are considered different if there is at least one position where the type of cow differs.

You are given two integers \(N\) and \(K\). Here, \(N\) represents the total number of positions available. You may place bulls in any subset of these positions as long as the condition is satisfied. Note that if no bull is placed, the arrangement (consisting solely of cows) is also valid.

Task: Compute the total number of valid arrangements to avoid any brawls.

Mathematical Formulation:

For any arrangement with exactly \(r\) bulls (where \(r \ge 1\)), the condition requires that \[ r + K(r-1) \le N. \] The number of ways to choose the positions for the \(r\) bulls so that there are at least \(K\) cows between every two bulls is given by the binomial coefficient \[ \binom{N - K(r-1)}{r}, \] and we also consider the case \(r=0\) (which contributes 1 valid arrangement). Thus, the final answer is: \[ 1 + \sum_{r=1}^{\lfloor\frac{N+K}{K+1}\rfloor} \binom{N - K(r-1)}{r}. \]

inputFormat

The input consists of a single line containing two space-separated integers \(N\) and \(K\), where \(N\) is the total number of positions (with \(N \ge 1\)) and \(K\) is the minimum number of cows required between any two bulls (with \(0 \le K < N\)).

outputFormat

Output a single integer, which is the number of valid arrangements.

sample

3 0
8