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 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
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
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 $$$< 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
Took long time to understand. But I got it.
Deleted.