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 | 3748 |

2 | Benq | 3540 |

3 | Petr | 3470 |

4 | Radewoosh | 3355 |

5 | ecnerwala | 3347 |

6 | maroonrk | 3345 |

7 | jiangly | 3324 |

8 | scott_wu | 3313 |

9 | ainta | 3298 |

10 | boboniu | 3289 |

# | User | Contrib. |
---|---|---|

1 | 1-gon | 200 |

2 | Errichto | 197 |

3 | rng_58 | 194 |

4 | SecondThread | 186 |

5 | awoo | 185 |

6 | Um_nik | 182 |

7 | vovuh | 179 |

8 | Ashishgup | 175 |

9 | -is-this-fft- | 173 |

9 | antontrygubO_o | 173 |

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

↑

↓

Codeforces (c) Copyright 2010-2021 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Feb/26/2021 04:24:01 (h2).

Desktop version, switch to mobile version.

Supported by

User lists

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 n

^{th}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 +

somethingThis

plus somethingis 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 =

^{3}C_{1}*3Similarly for 2 rows =

^{3}C_{2}*3And considering the coloring options for these 2 conditions, we get:

dp[n] = dp[n-1]*24 + Σ

^{3}C_{i}*3*2^{i}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 somethingpart 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$$$

dp

_{i}— number of ways to color 3 x i grid with three colors such that no row and column have the same color.C1

_{i}— 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.C2

_{i}— 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$$$.

Could you explain why multiply by 8 and 7?

Ah, it's wrong. C1, C2 are not right $$$n > 2$$$. They are too difficult to calculate.

dp[n]=dp[n-1]*(24)+(3C2)*3*2*(v1) +(24-(3C2)*3*2)*(v2).

((3C2)*3*2) invalid ways till (n-1) when two rows have same color.

((24-(3C2)*3*2)) invalid ways till (n-1) when no rows have same color.

v1 and v2 is the ways to fill nth col for those conditions. But not clear how to find v1 and v2.

$$$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

notrespect 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)$$$.

Oh yes!

This makes sense.

answer would be equal to the number of coloring that are legal for columns

minusthe number of coloring that are illegal for rows and legal for columns!I also understand the incorrectness of

dp[n] = dp[n-1]*24, it being that lets suppose one of the legal ways of coloring for n = 1 would be RRB but then in the very next column we are taking all 24 choices i.e we also take RRB or RGB and many more which are illegal!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 itLets 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.

And also for :

RGR

RBG

BRR

Now for dp[4] you wont count all states that are valid

Basically current state depends on more than one previous state.

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.

could u pls explain ur approach.How u got this .

StackExchange Math

I suggest this solution:

Let:

-> $$$answer = sum - sum1$$$

$$$sum$$$ & $$$sum1$$$ can be calculated by

dp bitmaskwithInclusion — 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)$$$

I don't see how this is valid.

Can you validate your approach with these?

Testcaseshttps://google.com Always works :)

Thank You. Nobody knows that.

ikr