we are given N red balls and M blue balls, we have to find no. of arrangements of (n+m) balls such that no more than k consecutive balls are of the same color.
1<n,m,k<=1000
we are given N red balls and M blue balls, we have to find no. of arrangements of (n+m) balls such that no more than k consecutive balls are of the same color.
1<n,m,k<=1000
| # | User | Rating |
|---|---|---|
| 1 | Benq | 3792 |
| 2 | VivaciousAubergine | 3647 |
| 3 | Kevin114514 | 3603 |
| 4 | jiangly | 3583 |
| 5 | turmax | 3559 |
| 6 | tourist | 3541 |
| 7 | strapple | 3515 |
| 8 | ksun48 | 3461 |
| 9 | dXqwq | 3436 |
| 10 | Otomachi_Una | 3413 |
| # | User | Contrib. |
|---|---|---|
| 1 | Qingyu | 157 |
| 2 | adamant | 153 |
| 3 | Um_nik | 146 |
| 3 | Proof_by_QED | 146 |
| 5 | Dominater069 | 145 |
| 6 | errorgorn | 141 |
| 7 | cry | 139 |
| 8 | YuukiS | 135 |
| 9 | TheScrasse | 134 |
| 10 | chromate00 | 133 |
| Name |
|---|



Auto comment: topic has been updated by sahilshelangia (previous revision, new revision, compare).
$$$dp[i][j][c]$$$ is the number of ways to distribute $$$i$$$ red balls and $$$j$$$ blue balls with the last ball of color $$$c$$$ ($$$0$$$ = red, $$$1$$$ = blue)
to calculate $$$dp[i][j][0]$$$
- the total configurations are $$$dp[i - 1][j][0] + dp[i - 1][j][1]$$$ (configurations with $$$i - 1$$$ red balls and $$$j$$$ blue balls)
- you have to subtract the configurations with $$$k - 1$$$ red balls at the end (because you can't add another red ball). Moreover, you know that the ball before the $$$k - 1$$$ red balls must be blue. So you have to subtract $$$dp[i - k][j][1]$$$
$$$dp[i][j][1]$$$ can be calculated similarly
so
$$$dp[1][0][0] = dp[0][1][1] = 1$$$
$$$dp[i][j][0] = dp[i - 1][j][0] + dp[i - 1][j][1] - dp[i - k][j][1]$$$
$$$dp[i][j][1] = dp[i][j - 1][0] + dp[i][j - 1][1] - dp[i][j - k][0]$$$
(if some index is $$$ \lt 0$$$, assume that the value of dp is $$$0$$$)
hope this is correct
dp[i][j][0]=dp[i−1][j][0]+dp[i−1][j][1]-dp[i−k][j][1]
dp[i][j][1]=dp[i][j−1][0]+dp[i][j−1][1]-dp[i][j−k][0]
the last term in both the cases should be subtracted?
Of course, fixed
I am getting twice the actual answer. Here is the code. Your solution seems wrong.
Probably my base cases are wrong. Check with $$$dp[1][0][0] = 1, dp[0][1][1] = 1$$$
yes, it would give you a wrong answer,
In do transition we should consider
dp[i][j][0]=dp[i−1][j][0]+dp[i−1][j][1]-dp[i−k][j][1]
The last term should be dp[i-k-1][j][1].
as we have to find no. of ways in which no more than k elements of the same color. But we are allowing k consecutive color.
I think the recurrence relations should be
because we should subtract cases which have more than k consecutive red/blue balls.
Also, I think initialization is wrong here. using dp[0][0][0]=dp[0][0][1]=1, we get dp[0][1][1]=2 for k>1, which is obviously wrong.
I think it should be
where dp is computed from 1..n and 1..m
Yup, thank you
Deleted.