There is a 3*n grid which is to be filled with 3 different colors.How many ways can we do it if no column or row have all the cells of same color. N<=20000 and mod 1e9+7
# | User | Rating |
---|---|---|
1 | tourist | 3947 |
2 | jiangly | 3734 |
3 | Radewoosh | 3646 |
4 | jqdai0815 | 3620 |
4 | Benq | 3620 |
6 | orzdevinwang | 3612 |
7 | ecnerwala | 3581 |
8 | Geothermal | 3569 |
8 | cnnfls_csy | 3569 |
10 | ksun48 | 3479 |
# | User | Contrib. |
---|---|---|
1 | awoo | 162 |
2 | maomao90 | 160 |
3 | nor | 156 |
4 | cry | 155 |
4 | adamant | 155 |
4 | atcoder_official | 155 |
4 | -is-this-fft- | 155 |
8 | maroonrk | 153 |
9 | SecondThread | 147 |
10 | Petr | 146 |
There is a 3*n grid which is to be filled with 3 different colors.How many ways can we do it if no column or row have all the cells of same color. N<=20000 and mod 1e9+7
Name |
---|
Maybe, you can calculate number of all ways to place three colors, and then calculate number of ways if you fix one row to be one color and then color other two and subtract from answer. You can to all this with dp.
Lets assume we have the answer for 3*(n-1) grid. Now its obvious that for 3*n grid we just have to take care for the nth column to be legal.
And the number of ways a column can be legal is 24 ( 3 cells in a column, number of ways to fill is 27 and number of illegal ways would be 3 i.e RRR, GGG, BBB ).
Lets assume we call answer for 3*(n-1) to be dp[n-1] then we can write it as:
dp[n] = dp[n-1]*24 + something
This plus something is because some of the illegal ways for coloring 3*(n-1) can also be made legal.
Now its obvious that these illegal ways for (n-1) would be only illegal in terms of rows and not columns.
Lets see how many illegal ways of rows exists!
Either one of the rows can be illegal or 2 rows can be illegal. Note that all 3 rows cannot be simultaneously be illegal.
And hence number of one row illegal = 3C1*3
Similarly for 2 rows = 3C2*3
And considering the coloring options for these 2 conditions, we get:
dp[n] = dp[n-1]*24 + Σ 3Ci*3*2i for all i ∈ {1,2}
P.S — I'm obviously unsure of the solution and would like to test it. So please provide a link to the problem.
I no more have access to the problem as it was asked in a coding round by a company and is no more public.
Ohh alright!
But this solution looks promising!
No. Your solution is not right.
Calculate the answer for $$$n = 2$$$
Thats a diplomatic answerXD.
Please provide a reason.
Find the answer for $$$n = 2$$$. Moreover, the addition part in dp is wrong.
Yes you are right, the plus something part is definetely wrong. I will try to rectify it!
Edit: Wtf! This is not even right. Why is this upvoted?
$$$C1_i = C1_{i - 1} \cdot 8$$$
$$$C2_i = C2_{i - 1} \cdot 7$$$
$$$dp_i = dp_{i - 1} \cdot 24 + C1_i + C2_i$$$
dpi — number of ways to color 3 x i grid with three colors such that no row and column have the same color.
C1i — number of ways to color 3 x i grid with three colors such that one of the rows has the same color in 3 x i — 1 grid.
C2i — number of ways to color 3 x i grid with three colors such that two of the rows have the same color in 3 x i — 1 grid.
Base case:
$$$C1_1 = 3!, C2_1 = 24 - 3! = 18, dp_1 = 0$$$.
$$$EDIT: $$$ Its wrong the ideia, sorry.
As i_love_cuiaoxiang mentioned, there is 24 choices of column to respect the column restriction.
Lets do a dp of coloring choices that respect the column restriction and not respect the row restriction.
The $$$dp[i][j]$$$ have the number of choices that don't respect the row restriction if we just consider the first $$$i$$$ columns and put $$$j$$$ in the $$$i$$$ column ($$$j$$$ can be the options that we can put in the column (24), can be "RRG", "BBR" or any other combination except "RRR", "GGG" and "BBB").
How the transition works? $$$dp[i][j] = \sum\limits_{k} dp[i-1][k]$$$ if $$$k$$$ have a common value with j in any position("RGB" have a common value with "RRB" but don't have with "GBR", note that position matters).
The answer for the problem will be $$$24^{n} - \sum\limits_{i} dp[n][k]$$$. The complexity will be $$$O(n*24^2)$$$.
why so?
EDIT: Ohh ok! You dont take into consideration that it was already legal but you were still trying to make it legal by placing only the ones which dont intersect with it
Lets se the sequence: "RRG", "BRB", "BGG", my dp will add this case to dp[3]["BGG"], because "RRG" match "BRB" and "BRB" match "BGG", and is a case that don't disrespect any restriction.
I think my solution would work if we already precompute answers for n=1 and n=2!
I don't think so, It's impossible for the answer be greater or equal than $$$24^{n}$$$(the maximum possible answer if we don't consider the row restriction), but your solution will give a answer greater or equal $$$24^{n}$$$, so you will get WA.
I suggest this solution:
Let:
-> $$$answer = sum - sum1$$$
$$$sum$$$ & $$$sum1$$$ can be calculated by dp bitmask with Inclusion — Exclusion.
$$$sum$$$ is always $$$24^n$$$. can you elaborate on calculating $$$sum1$$$?
Let $$$f[0/1][0/1][0/1] =$$$ number of ways to create table that row 1, 2, 3 is same or not.
$$$sum1 = f[1][0][0] + f[0][1][0] + f[0][0][1] - f[1][1][0] - f[1][0][1] - f[0][1][1] + f[1][1][1]. (Inclusion - Exclusion)$$$
To calculate $$$f$$$, we can use $$$dp[i][mask] =$$$ number of ways to create table 3 * i with the $$$i-th$$$ $$$column's$$$ $$$state = mask$$$ and no column have all the cells of same color.
Let $$$k =$$$ number of qualified states between 2 adjacent columns for each state of $$$f$$$. In the worst case, $$$k = 9^2$$$.
The complexity could be $$$O(2^3 * 9^2 * n)$$$