Sphere Online Judge

SPOJ Problem Set (classical)

423. Assignments

Problem code: ASSIGN

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


Added by:Pawel Gawrychowski
Date:2005-10-08
Time limit:20s
Source limit:50000B
Memory limit:256MB
Cluster: Pyramid (Intel Pentium III 733 MHz)
Languages:All except: NODEJS PERL 6

hide comments
2013-02-04 12:17:56 foram


Last edit: 2013-05-20 06:25:11
2013-01-13 09:11:11 thefourtheye
Can anyone please explain the sample input and output, atleast the first one?
2012-06-18 16:33:42 guliashvili
O(n*2^n) + very good constant factor optimizations. time : 11.63
2012-04-17 21:07:51 Nafis Ahmed
O(n*2^n) gets ACed on 19.5s and sometimes the same soln gets TLE.
2011-06-14 10:17:55 alexandre
OMG it works!!! AC!!!
2011-06-14 10:14:39 George Skhirtladze
Yes of course!
2010-07-24 00:14:50 Tomasz Żurkowski
my O(n^2 * 2^n) is accepted :)
2010-05-01 21:14:14 Martin Betlista Šuška
O(n*2^n) is not accepted
2010-04-30 14:57:25 Mudit Jain
if a student likes no subject? that means if all preferences are 0's then?
SPOJ © 2013 Sphere Research Labs. All Rights Reserved.