Permutation Combination question (3*n board) - math

I have a question:
Find the total number of ways a 3×n board can be painted using 3 colors while making sure no cells of the same row or the same column have entirely the same color. The answer must be computed modulo 10^9+7.
Also somebody answered it here.
https://math.stackexchange.com/questions/3215805/coloring-a-3-times-n-board-using-3-colors
but i am not able to understand, can you explain in easy language?

To understand the general principle, let's look at a situation with only two columns. Name the colors red, green and blue.
For a column to fulfill the conditions: Each cell can have one of 3 colors, there are 3 cells, totaling 3^3=27 possible colorings. Among these, 3 are monochrome (one red, one green, one blue). Subtracting the disallowed colorings: 27-3=24 possibilities for a column. Only considering the columns, there are 24^n colorings for the board.
Now, let's look at the disallowed rows. For the monochrome row, there are 3 colors and 3 possible rows to choose from. For each of these 9 possibilities, each column can be colored in 8 possible ways. This means 9x8^n rows need to be removed. The image below shows each of the possible boards where no column is monochrome. The 9x8^2=576 rows to be removed are shown with a black line:
If we now subtract the number of blacked out rows from the total, we are subtracting too many: some boards have two bad rows and some even three.
So, the count of the boards with two bad rows has to be added again. Their total is 3x3x2^n + 3x6x3^n. The first term counts the case with two equal colored bad rows: 3 positions for the non-bad row, 3 colors for the bad rows, 2 remaining colors for each cell of the non-bad row. The second term counts the case with two differently colored bad rows: 3 positions for the non-bad row, 6 possible colorings for the bad rows, 3 possible colors for each cell of the non-bad row.
There are 24 boards with three bad rows which got subtracted three times in the second step and added again three times in the third step. They now have to be subtracted from the total.
So, as explained in the linked post, the general formula is:
24^n − 9×8^n + 18×3^n+9×2^n − 24
which gives the values 0, 174, 9750, 296490, 7672350, 188757354, 4567637550, 109924439610, 2640599939550, 63393718361034, ... for n starting at 1.
Or modulo 10^9+7:
0, 174, 9750, 296490, 7672350, 188757354, 567637522, 924438847, 599921070, 717917283, ...

Related

Finding 2 variables that differs the most and combining them into one variable

I am predicting bankruptcy for 17 successful companies. For each of the companies (1 - 17):
I have calculated the values of 6 bankruptcy predicting models (A, L, T, S, Z, CH);
And marked: "1" - bankruptcy is likely (red), "0" - bankruptcy is unpredictable (gray), "-1" - bankruptcy is unlikely (green).
All the cells in the first block have to be green, because the companies are successful. However, I get different values and colors. And my task is to find out, that 2 models (of these 6) can be applied together to get the biggest amount of green cells for each company.
My solution:
Let's say, we compare the pair of A and L models:
First row: If we have two green cells, as the result we get green cell. Ok.
Second row: green and red. We cannot get green here.
Third row: green.
Fourth row: red.
If I need to repeat it for all pairs of models and in this way find that two of them makes the most green cells?

Assign integral value to list of relative values

I have an assortment of syrups, each of which has a value - the amount of sugar per volume. As people blend these syrups, I track which ones are used, and created a table to get a Relative Weight of each blend. I understand > Data > Sort > Options > Custom Sort Order.
However, I really don't wish to sort each table, and am looking for a way to parse a column of this list as entered, and return a column with results in an Integral Relative Value of each row, as compared to the weights of syrups in the other rows of the table.
Unique Name weight Not Unique Relative Value
blueberry .250 2
raspberry .333 3
orange .425 4
tangerine .333 3
blackberry .225 1
I am attempting to find the "Relative Sort", a nested function which can assign an integral value of the Unique Name which compares the weights of the syrups. A "Lookup" only works if there is an absolute equality, right?
What if someone doesn't use "blackberry syrup", then "blueberry" is the lightest, and should be labeled as 1.
Is this too complicated for LibreOffice Calc?
It's a recursive greater than/less than/equal to comparison?
IF the problem is calculating the right hand column below from entries that may be sorted ascending by value as on the left:
then an answer is, in C2 and copied down to suit (provided C1 is blank or 0):
=IF(B1<>B2,C1+1,C1)
Without sorting the RANK function might be simpler and adequate (though in the example returning 5 rather than 4).

Need a formula to sort Excel column by

Please have a look at this spreadsheet
I need a formula that would put the green rows at the top or near the top at least and the red rows at or near the bottom the column is sorted. It doesn't have to work for any numbers, just the ones in this spreadsheet
I got close with by sorting by column D which has (columnA / columnB * 100) * columnA but there are green outlier rows in the middle that should be more towards the top area
You say nothing about the line colored tan (near-red), so I treat that as a red line.
Put a new formula in E2,
=IF(A2<5,100,0)+IF(C2>0.68,0,10)-D2/10000
then copy it down column Sort the table on column E, increasing. You then get this:
This works because the green rows are distinguished in their column C, which is 0.667 or less for green and 0.700 for all other rows. The red rows are distinguished in their column A, which is 2 or less for red (and for the tan) and 7 or more for all other rows. So my formula makes breakpoints at 5 for column A and 0.68 for column C and assigns appropriate numbers, adding them for a final score. I use column D as a tie-breaker, as in your original data. You can remove the -D2/10000 if you don't care about tie-breakers in each color section.
I did all this in Microsoft Excel (in Office 365), as in your question title.

Filtering grouped data in R

I was wondering if anyone can help with grouping the data below as I'm trying to use the subset function to filter out volumes below a certain threshold but given that the data represents groups of objects, this creates the problem of removing certain items that should be kept.
In Column F ( and I) you can see Blue, Red, and Yellow Objects. Each represent three separate colored probes on one DNA strand. Odd numbered or non-numbered Blue ,Red, and Yellow are paired with a homologous strand represented by an even numbered Blue, Red, and Yellow. Ie data in rows 2,3,and 4 are one "group" and pair with the "group" shown in rows 5,6,and 7. This then repeats, so 8,9,10 are a new group and that group pairs with the one in 11,12,13.
What I would like to do is subset the groups so that only those below a certain Distance to Midpoint (column M) are kept. The Midpoint here is the midpoint of the line that connects the blue of one group with the blue of its partner, so the subset should only apply to the Blue distance to midpoint, and that is where I'm having a problem. For instance if I ask to keep blue distances to midpoint that are less than 3, then the objects in row 3 and 4 should be kept because they are part of the group with the blue distance below 3. Right now though when I filter with the subset function I lose Red Selection and Yellow Selection. I'm confident there is a straighforward solution to this in R, but I'd also be open to some type of filtering in excel if anyone has suggestions via that route instead.
EDIT
I managed to work something out in Excel last night after posting the question. Solution isn't pretty but it works well enough. I just added a new column next to "distance to midpoint" that gives all the objects in one group the same distance so that when I filter the data I won't lose any objects that I shouldn't. If it helps anyone in the future, the formula I used in excel was:
=SQRT ( ((INDEX($B$2:$B$945,1+QUOTIENT(ROWS(B$2:B2)-1,3)*3))- (INDEX($O$2:$O$945,1+QUOTIENT(ROWS(O$2:O2)-1,3)*3)) ) ^2 +( (INDEX($C$2:$C$945,1+QUOTIENT(ROWS(C$2:C2)-1,3)*3))-(INDEX($P$2:$P$945,1+QUOTIENT(ROWS(P$2:P2)-1,3)*3)) ) ^2 +( (INDEX($D$2:$D$945,1+QUOTIENT(ROWS(D$2:D2)-1,3)*3))-(INDEX($Q$2:$Q$945,1+QUOTIENT(ROWS(Q$2:Q2)-1,3)*3)) ) ^2)
Would be easier with a reproducible example, but here's a (hacky) plyr solution:
filterframe<-function(df,threshold){
df$grouper<-rep(seq(from=1,to=6),nrow(df)/6)
dataout<-df%>%group_by(grouper)%>%summarise(keep=.[[1]]$distance_to_midpoint<threshold)
dataout[dataout$keep,]
}
filterframe(mydata)
A base R solution provided below. The idea is that once your data are in R, you (edit) keep! rows iff they meet 2 criteria. First, the Surpass column has to contain the word "blue" in it, which is done with the grepl function. Second, the distance must below a certain threshold (set arbitrarily by thresh.
fakeData=data.frame(Surpass=c('blue', 'red', 'green', 'blue'),
distance=c(1,2,5,3), num=c(90,10,9,4))
#thresh is your distance threshold
thresh = 2
fakeDataNoBlue = fakeData[which(grepl('blue', fakeData$Surpass)
& fakeData$distance < thresh),]
There's probably also a quick dplyr solution using filter, but I haven't fully explored the functionality there. Also, I may be a bit confused on if you also want to keep the other colors. If so, that's the same as saying you want to remove the blue ones exceeding a certain distance threshold, which you would just do a -which command, and turn the < operator into a > operator.

Maths Question: number of different permutations

This is more of a maths question than programming but I figure a lot of people here are pretty good at maths! :)
My question is: Given a 9 x 9 grid (81 cells) that must contain the numbers 1 to 9 each exactly 9 times, how many different grids can be produced. The order of the numbers doesn't matter, for example the first row could contain nine 1's etc. This is related to Sudoku and we know the number of valid Sudoku grids is 6.67×10^21, so since my problem isn't constrained like Sudoku by having to have each of the 9 numbers in each row, column and box then the answer should be greater than 6.67×10^21.
My first thought was that the answer is 81! however on further reflection this assumes that the 81 numbers possible for each cell are different, distinct number. They are not, there are 81 possible numbers for each cell but only 9 possible different numbers.
My next thought was then that each of the cells in the first row can be any number between 1 and 9. If by chance the first row happened to be all the same number, say all 1s, then each cell in the second row could only have 8 possibilites, 2-9. If this continued down until the last row then number of different permutations could be calculated by 9^2 * 8^2 * 7^2 ..... * 1^2. However this doesn't work if each row doesn't contain 9 of the same number.
It's been quite a while since I studied this stuff and I can't think of a way to work it out, I'd appreciate any help anyone can offer.
Imagine taking 81 blank slips of paper and writing a number from 1 to 9 on each slip (nine of each number). Shuffle the deck, and start placing the slips on the 9x9 grid.
You'd be able to create 81! different patterns if you considered each slip to be unique.
But instead you want to consider all the 1's to be equivalent.
For any particular configuration, how many times will that configuration be repeated
due to the 1's all being equivalent? The answer is 9!, the number of ways you can permute the nine slips with 1 written on them.
So that cuts the total number of permutations down to 81!/9!. (You divide by the number of indistinguishable permutations. Instead of 9! indistinguishable permutations, imagine there were just 2 indistinguishable permutations. You would divide the count by 2, right? So the rule is, you divide by the number of indistinguishable permutations.)
Ah, but you also want the 2's to be equivalent, and the 3's, and so forth.
By the same reasoning, that cuts down the number of permutations to
81!/(9!)^9
By Stirling's approximation, that is roughly 5.8 * 10^70.
First, let's start with 81 numbers, 1 through 81. The number of permutations for that is 81P81, or 81!. Simple enough.
However, we have nine 1s, which can be arranged in 9! indistinguishable permutations. Same with 2, 3, etc.
So what we have is the total number of board permutations divided by all the indistinguishable permutations of all numbers, or 81! / (9! ** 9).
>>> reduce(operator.mul, range(1,82))/(reduce(operator.mul, range(1, 10))**9)
53130688706387569792052442448845648519471103327391407016237760000000000L

Resources