https://codeforces.com/contest/1237/problem/F
key to come up with solution is that we need to realize the importance of iterating two kinds of dominos,both horizontal and vertical, or we won't satisfy the requirement of total answer
description
you are given a square grid (n*m) .let 's call a placement of dominos(多米诺骨牌)Balance if no row or column contains two different grids that belong to two different dominos.
now the grid is placed some dominos balancedly at the beginning. find the number of situations that you can put zero or some additional dominos in the grid without losing its beauty of balance.
breakthrough
this is a problem about rows and columns. in order to calculate the legal ways maybe we need some combinatorics or DP methods. and this problem happens to involve both
first let's try to view this problem in combinatorics perspective.
consider the number of two kinds of dominos we use. let (a) be the number of horizontal dominos and (b) be the vertical. we need:
- (a+2*b) legal rows , among with (b) pair of rows are independently adjacent.
- (b+2*a) legal columns , among with (a) pair of rows are independently adjacent.
let (R) be the number of ways of generation of rows of map that satisfy the limitation of rows. let (C) be the columns. then the number of legal map, where all limitation of both rows and columns is satisfied, is (R*C)
in any legal map, take rows for example, where we have already figured out some perticular positions to place horizontal and others for vertical dominos
( which means the placements between horizontal and vertical are independent )
which the number of ways we put all horizontal dominos is (A(a,a)) or (a!) ,so is the vertical. they collectively lead to our final answer (R*C*a!*b!).
note that the time we set all (a,b) in peace, both limitations which come from both rows and columns are satisfied because the map is legal and in which row correspond to column and vice versa.
now it's time to solve R and C.
just the same as to solve a easy low dimentional problem. consider a line or a particular row with some obstacles in it, now we have to place (a) dominos or (1*2) rectangles and (b) (1*1) squares in it, and we need to figure out the ways of legal distribution. and here is why DP is so amazing:
- denote (f[i][j]) be the number of ways to pick (j*2) consecutive grids to place retangles out of the first (i) grids, and just set (1*1) squares aside.
- (f[i][j]=f[i-1][j]) in any situation
- (f[i][j]+=f[i-2][j-1]) if we can put a rec tangle right now
- at last we consider all squares, just combinatrics: C(the number of remainGrids, the number of squares we need to consider). C stands for mathematic symbol of conbination
- at last we multiply the number of ways of rectangles and squares.
in this way we can deal with both R and C and finally the answer.