TopCoder problem "RooksParty" used in SRM 473 (Division I Level Three)



Problem Statement

    

The black and white chess rooks were bored, so they decided to invite their colorful friends to a party. However, as the evening progressed, many pairs of rooks of different colors started arguing and threatening each other.

To prevent a massacre, you now need to place all the rooks in such a way that no two rooks of different colors attack each other.

You are given the dimensions of the chessboard: ints rows and columns. You are also given a int[] counts, where counts[i] is the number of rooks of the i-th color you have.

Compute and return the value (X mod 1,000,000,009), where X is the number of valid arrangements of all the given rooks on the given chessboard. No square of the chessboard may contain more than one rook. Rooks of the same color are undistinguishable.

 

Definition

    
Class:RooksParty
Method:countArrangements
Parameters:int, int, int[]
Returns:int
Method signature:int countArrangements(int rows, int columns, int[] counts)
(be sure your method is public)
    
 

Notes

-Two rooks attack each other if they are either in the same row or in the same column, and all squares between them are empty.
 

Constraints

-rows will be between 1 and 30, inclusive.
-columns will be between 1 and 30, inclusive.
-counts will contain between 1 and 10 elements, inclusive.
-Each element of counts will be positive.
-The sum of all elements of counts will not exceed rows*columns.
 

Examples

0)
    
2
3
{1,1}
Returns: 12

Here are all 12 valid placements:

1..   1..   .1.   .1.   ..1   ..1
.2.   ..2   2..   ..2   2..   .2.

.2.   ..2   2..   ..2   2..   .2.
1..   1..   .1.   .1.   ..1   ..1
1)
    
5
2
{3}
Returns: 120
As all three rooks have the same color, we can put them on any three squares.
2)
    
5
2
{1,1,1}
Returns: 0
It is impossible to place these rooks correctly.
3)
    
8
8
{1,1,1,1,1,1,1,1}
Returns: 625702391
Here the answer is (8! * 8!) modulo 1,000,000,009.
4)
    
4
2
{3,1}
Returns: 8

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=10980

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=14155&pm=10980

Writer:

misof

Testers:

PabloGilberto , bmerry , ivan_metelsky

Problem categories:

Dynamic Programming