Блог пользователя deep_tricks

Автор deep_tricks, история, 3 года назад, По-английски

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

  • Проголосовать: нравится
  • +7
  • Проголосовать: не нравится

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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.

»
3 года назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

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.

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    I no more have access to the problem as it was asked in a coding round by a company and is no more public.

    • »
      »
      »
      3 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      Ohh alright!
      But this solution looks promising!

      • »
        »
        »
        »
        3 года назад, # ^ |
        Rev. 2   Проголосовать: нравится +8 Проголосовать: не нравится

        No. Your solution is not right.

        Calculate the answer for $$$n = 2$$$

        • »
          »
          »
          »
          »
          3 года назад, # ^ |
            Проголосовать: нравится 0 Проголосовать: не нравится

          Thats a diplomatic answerXD.
          Please provide a reason.

          • »
            »
            »
            »
            »
            »
            3 года назад, # ^ |
              Проголосовать: нравится +8 Проголосовать: не нравится

            Find the answer for $$$n = 2$$$. Moreover, the addition part in dp is wrong.

            • »
              »
              »
              »
              »
              »
              »
              3 года назад, # ^ |
                Проголосовать: нравится 0 Проголосовать: не нравится

              Yes you are right, the plus something part is definetely wrong. I will try to rectify it!

              • »
                »
                »
                »
                »
                »
                »
                »
                3 года назад, # ^ |
                Rev. 3   Проголосовать: нравится +8 Проголосовать: не нравится

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

»
3 года назад, # |
Rev. 3   Проголосовать: нравится -7 Проголосовать: не нравится

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

  • »
    »
    3 года назад, # ^ |
    Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

    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

    • »
      »
      »
      3 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      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.

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

I think my solution would work if we already precompute answers for n=1 and n=2!

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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.

»
3 года назад, # |
  Проголосовать: нравится +13 Проголосовать: не нравится
$$$ 24^n - 9 \cdot 8^n + 18 \cdot 3^n + 9 \cdot 2^n - 24 $$$
»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

I suggest this solution:

Let:

  • $$$sum =$$$ number of ways to create a table that no column have all the cells of same color.
  • $$$sum1 =$$$ number of ways to create a table that no column have all the cells of same color and have one or more rows have all the cells of same color.

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

$$$sum$$$ & $$$sum1$$$ can be calculated by dp bitmask with Inclusion — Exclusion.

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    $$$sum$$$ is always $$$24^n$$$. can you elaborate on calculating $$$sum1$$$?

    • »
      »
      »
      3 года назад, # ^ |
      Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

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