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


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 PERL6 VB.NET

hide comments
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
2016-06-02 07:48:19 vijay kumar paliwal
Here comes my third fifty!! :)
2016-05-25 12:05:12
Question is worth trying !!
2016-01-22 07:03:39
I'm not getting the question. Can some1 explain test cases?
2015-11-18 22:06:15 :.Mohib.:
top-down :D ;)
2015-11-09 08:12:49
After juggling with TLE's for 4 hrs, Attained the enlightenment.

Last edit: 2015-11-09 10:22:28
2015-10-02 12:34:09 rahul nagurtha
Can someone tell me why am i getting wrong answer instead of TLE ?
2015-09-24 14:09:33 Abhilash
Topdown gets AC with few optimizations in code.
2015-08-31 16:22:18 anando_du
Top DOwn ... single submission . AC . DP state is important here ....
2015-08-30 15:46:20 Abhinandan Agarwal
A good problem indeed! Top down -TLE,
Bottom Up - AC
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.