TopCoder problem "SeatingPlan" used in SRM 320 (Division I Level Three)



Problem Statement

    

Right before the day of the final exam, a teacher is busy coming up with a seating assignment for the venue. The seats in the exam venue are arranged as m rows, each containing n seats, i.e., an m by n grid. The total number of students is exactly the same as the total number of seats, so all the seats will be occupied.

The teacher's task is complicated by the fact that there are k students who are suspected cheaters. In order to prevent cheating, the teacher will not allow any two suspected cheaters to sit in adjacent seats. Two seats are adjacent if and only if they are in adjacent columns of the same row, or adjacent rows of the same column.

The teacher's method for assigning the seats is simple. She finds a random seating assignment. Then, she checks if there are any suspected cheaters assigned to adjacent seats. If so, she repeats the process by finding another random assignment, and otherwise, she is done.

Your job is to determine the expected number of random seating assignments the teacher must generate before she finishes her task. Return this expected number as a String formatted "p/q" (quotes for clarity only), where p and q are relatively prime positive integers with no leading zeros. If there is no valid seating assignment, return "Impossible!" (quotes for clarity only).

 

Definition

    
Class:SeatingPlan
Method:expectedTrial
Parameters:int, int, int
Returns:String
Method signature:String expectedTrial(int m, int n, int k)
(be sure your method is public)
    
 

Constraints

-m will be between 1 and 80, inclusive.
-n will be between 1 and 80, inclusive.
-m*n will be less than or equal to 80.
-k will be between 0 and 20, inclusive, and less than or equal to m*n.
 

Examples

0)
    
1
4
3
Returns: "Impossible!"
1)
    
2
3
0
Returns: "1/1"
Since there is no cheater, the teacher can finish the task with the first trial.
2)
    
2
3
2
Returns: "15/8"
Let S represent a suspected cheater, and N a non-cheater.

All possible acceptable seating plans:
SNS   SNN   SNN   NSN   NSN   NNS   NNS   NNN
NNN   NSN   NNS   SNN   NNS   SNN   NSN   SNS
And all possible unacceptable seating plans:
SSN   NSS   NNN   NNN   SNN   NSN   NNS
NNN   NNN   SSN   NSS   SNN   NSN   NNS
3)
    
3
3
2
Returns: "3/2"
4)
    
8
7
18
Returns: "70775996591300/172086661"

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10000&pm=6400

Writer:

lyc1977

Testers:

PabloGilberto , brett1479 , Olexiy , Mike Mirzayanov

Problem categories:

Dynamic Programming, Math