ASSIGN - Assignments


Problem

Your task will be to calculate number of different assignments of n different topics to n students such that everybody gets exactly one topic he likes.

Input

First line of input contains number of test cases c (1<=c<=80). Each test case begins with number of students n (1<=n<=20). Each of the next n lines contains n integers describing preferences of one student. 1 at the ith position means that this student likes ith topic, 0 means that he definitely doesn't want to take it.

Output

For each test case output number of different assignments (it will fit in a signed 64-bit integer).

Example

Input:
3
3
1 1 1
1 1 1
1 1 1
11
1 0 0 1 0 0 0 0 0 1 1 
1 1 1 1 1 0 1 0 1 0 0 
1 0 0 1 0 0 1 1 0 1 0 
1 0 1 1 1 0 1 1 0 1 1 
0 1 1 1 0 1 0 0 1 1 1 
1 1 1 0 0 1 0 0 0 0 0 
0 0 0 0 1 0 1 0 0 0 1 
1 0 1 1 0 0 0 0 0 0 1 
0 0 1 0 1 1 0 0 0 1 1 
1 1 1 0 0 0 1 0 1 0 1 
1 0 0 0 1 1 1 1 0 0 0 
11
0 1 1 1 0 1 0 0 0 1 0 
0 0 1 1 1 1 1 1 1 1 1 
1 1 0 1 0 0 0 0 0 1 0 
0 1 0 1 0 1 0 1 0 1 1 
1 0 0 1 0 0 0 0 1 0 1 
0 0 1 0 1 1 0 0 0 0 1 
1 0 1 0 1 1 1 0 1 1 0 
1 0 1 1 0 1 1 0 0 1 0 
0 0 1 1 0 1 1 1 1 1 1 
0 1 0 0 0 0 0 0 0 1 1 
0 1 1 0 0 0 0 0 1 0 1 

Output:
6
7588
7426


hide comments
saurav786: 2016-11-07 21:31:07

Topdown+bitmasking :-D

poomrokc: 2016-10-15 08:20:36

My O(2^n*n) Gets tle Xp for each case lol

dev: 2016-10-10 16:49:52

Very Nice DP + BitMasking !
O(2^n * n*n ) will give Tle.

hamjosh1: 2016-09-22 15:12:53

learned new things :'D

srinath: 2016-09-21 11:52:19

Using map gives TLE. Use vector instead.

darwinthamil: 2016-07-27 15:32:52

@vikikkdi : seri

vikikkdi: 2016-06-25 06:16:14

can anyone explain me the test cases??

vaibhavi760: 2016-06-23 06:12:28

took 1 day to solve and finally success nice question to understand bitmasking

Last edit: 2016-06-25 18:01:55
vijay kumar paliwal: 2016-06-02 07:48:19

Here comes my third fifty!! :)

ajay_5097: 2016-05-25 12:05:12

Question is worth trying !!


Added by:gawry
Date:2005-10-08
Time limit:2.997s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: NODEJS PERL 6 VB.net