How to calculate the different combinations of an NxM grid with 3-valid values

Viewed 442

I have 3 valid values: a,b,c I can insert them in a Nx3 grid in such a way that no row or column contains cells that are the same values. How can I calculate the number of valid patterns that can be created using N rows?

For example, if N = 4, the total number of valid pattern is 296490. enter image description here

This is my attempt:

def countPatterns(n):

    dp1, dp2 = 6,6
    mod = 10 ** 9 + 7
    
    for i in range(2, n+1):
        dp1, dp2 = (dp1 * 3 + dp2 * 2) % mod, (dp1 * 2 + dp2 * 2) % mod
    return (dp1 + dp2) % mod

When N = 4, the output should be 174 but I'm getting 54.

2 Answers

This can be solved using standard techniques from combinatorics.

import math


# Counts the number of grids where the first i rows and first j columns are
# monochrome. All of the monochrome rows and columns must have the same color if
# and only if both i and j are nonzero. Otherwise, the color choices are
# independent.
def count_monochrome(n, i, m, j):
    return 3 ** (1 if i and j else i + j) * 3 ** ((n - i) * (m - j))


# Uses inclusion-exclusion to count the number of grids with no monochrome row
# or column.
def count(n, m=3):
    # There are math.comb(n, i) * math.comb(m, j) intersections with i
    # monochrome rows and j monochrome columns.
    return sum(
        math.comb(n, i)
        * math.comb(m, j)
        * (-1) ** (i + j)
        * count_monochrome(n, i, m, j)
        for i in range(n + 1)
        for j in range(m + 1)
    ) % (10 ** 9 + 7)


print(count(4))

Short Answer

This can also be solved mathematically and so the formula is:

24n - (9 * 8n - 9 * 2n - 18 * 3n + 24)

def countPatterns(n):
    result = 24**n - (9 * 8**n - 9*2**n - 18*3**n + 24)
    mod = 10 ** 9 + 7
    return result % mod

print (countPatterns(4)) 

296490

Long Answer

1. Condition : no row contains same values

For a given row, there are 3 ways to set a colour in each cell. So, there are 33=27 possible combinations. However, out of these 27 combinations, 3 are GGG, BBB and RRR. So, they have to be excluded and the number of valid combinations in a row is 27 - 3 = 24.

Hence, the total number of combinations for a table with n rows is 24n

2. Condition : no column contains same values

To solve this case, we can count all "invalid combinations" and then subtract their number from the total number (which is 24n as stated in the previous section).

2.1 Invalid combinations

  1. Only first column has same values.
  2. Only second column has same values.
  3. Only third column has same values.
  4. Only 2 first columns have same values (value from the first column is not necessarily equal to the other one).
  5. Only 2 last columns have same values.
  6. Only first and last columns have same values.
  7. Values within each column are equal.

Obviously, number of combinations for 1 is equal to 2 and 3. The same can be noted about 4, 5 and 6.

If we sum up combinations from 1 to 7, then we'll get the total number.

However, it's hard to calculate (1)-(7) individually, but easier to calculate the number of combinations such that at least k columns have the same values. Once those are found, we will apply an inclusion-exclusion principle to find the number of combinations asked in the question.

2.1.1 First column has same values

For each row, there are 8 combinations such that the row starts with R:

RRG
RRB
RGR
RGG
RGB
RBR
RBG
RBB

That means that for a table with n, there are 8n combinations in which first column consists of 8s. Thereof, there are 3*8n combinations such that the first column starts from R, G or B.

Now, it's important to realise that this number represents 4 invalid cases - 1, 4, 6 and 7 (all sets of columns containing the first column).

2.1.2 Second column has same values

Basically, we can use the same logic as above to realise that it's also 3*8n.

This represents 2, 4, 5 and 7.

2.1.3 Third column has same values

Same as before - 3*8n.

This covers 3, 5, 6 and 7.

If we add up combinations from these 3 steps, we will get 9*8n. However, as noted in those steps, we will count 4, 5 and 6 twice. 7 will be counted thrice.

2.1.4 2 columns have same values

Let's count it for 2 first columns because the number for every pair of columns is still the same.

If a row starts with 2 identical colours, then there are 2 combinations for each colour. If not, then there are 3 combinations for each pair (there are 6 possible pairs). Hence, the total number of combinations is 3 * 2n + 6 * 3n.

So, the number of combinations for 4 and 7 is 3 * 2n + 6 * 3n.

The number of combinations for 5 and 7 is 3 * 2n + 6 * 3n.

The number of combinations for 6 and 7 is 3 * 2n + 6 * 3n.

2.1.5 Values within each column equal

This means that all rows must be equal. As there are only 24 ways to construct a row, there are 24 ways to have all values equal within columns. So, it's 24.

Total number of invalid cases

As said before, if we sum up 2.1.1, 2.1.2 and 2.1.3, then we will count 4, 5 and 6 twice, 7 - 3 times (from 2.1). Since the number of combinations for each pair of columns is the same, we can just subtract 3 * 2n + 6 * 2n 3 times (from 2.1.4), and then add the number of combinations for a three column case once (because it won't be there after the subtraction).

9 * 8n - 3 * (3 * 2n + 6 * 3n) + 24 = 9 * 8n - 9 * 2n - 18 * 3n + 24

3. Final Answer

Now, just subtract the number of invalid cases from 24n:

24n - (9 * 8n - 9 * 2n - 18 * 3n + 24)

Don't forget to apply a modulo operation by 109 + 7 at the end.

Related