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 | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
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.