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
- Only first column has same values.
- Only second column has same values.
- Only third column has same values.
- Only 2 first columns have same values (value from the first column is not necessarily equal to the other one).
- Only 2 last columns have same values.
- Only first and last columns have same values.
- 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.