@@ -11,8 +11,12 @@ public static void main(String[] args) throws IOException {
11
11
final StringBuilder stringBuilder = new StringBuilder ();
12
12
for (int t = Integer .parseInt (br .readLine ()); t > 0 ; t --) {
13
13
final int n = Integer .parseInt (br .readLine ());
14
- final int sets [] = Arrays .stream (br .readLine ().split (" " )).mapToInt (Integer ::parseInt ).toArray ();
15
- stringBuilder .append (new Solver (sets ).solve ()).append ('\n' );
14
+ final int wardrobe [][] = new int [n ][];
15
+ for (int i = 0 ; i < n ; i ++) {
16
+ wardrobe [i ] = Arrays .stream (br .readLine ().split (" " )).mapToInt (Integer ::parseInt ).toArray ();
17
+ }
18
+ final Solver solver = new Solver (wardrobe );
19
+ stringBuilder .append (solver .solve ()).append ('\n' );
16
20
}
17
21
System .out .println (stringBuilder );
18
22
}
@@ -21,31 +25,50 @@ public static void main(String[] args) throws IOException {
21
25
class Solver {
22
26
23
27
public static final int MOD = 1000000007 ;
24
- private int [][] shirtSets ;
28
+ private final int length ;
29
+ private int [][] people ;
30
+ private final int [] count ;
31
+ private final long dp [][];
25
32
26
- public Solver (final int [] people ) {
27
- final int shirtSets [][] = new int [100 ][people .length ];
28
- final int count [] = new int [people .length ];
29
- for (int i = 0 ; i < people .length ; i ++) {
30
- shirtSets [people [i ]][count [people [i ]]++] = i ;
33
+ public Solver (final int [][] wardrobe ) {
34
+ final int people [][] = new int [101 ][wardrobe .length ];
35
+ count = new int [101 ];
36
+ for (int i = 0 ; i < wardrobe .length ; i ++) {
37
+ for (int index = 0 ; index < wardrobe [i ].length ; index ++) {
38
+ int shirt = wardrobe [i ][index ];
39
+ people [shirt ][count [shirt ]++] = i ;
40
+ }
41
+ }
42
+ this .length = wardrobe .length ;
43
+ this .people = people ;
44
+ dp = new long [1 << length ][101 ];
45
+ for (final long [] aDp : dp ) {
46
+ Arrays .fill (aDp , -1 );
31
47
}
32
- this .shirtSets = shirtSets ;
33
48
}
34
49
35
50
public long solve () {
36
51
return possiblities (0 , 0 );
37
52
}
38
53
39
54
private long possiblities (final int people , final int assigned ) {
55
+ if (assigned == this .people .length ) {
56
+ return people == (1 << length ) - 1 ? 1 : 0 ;
57
+ }
58
+ if (dp [people ][assigned ] != -1 ) {
59
+ return dp [people ][assigned ];
60
+ }
40
61
long answer = 0 ;
41
- for (int i = 0 ; i < shirtSets [assigned ]. length ; i ++) {
42
- int person = shirtSets [assigned ][i ];
62
+ for (int i = 0 ; i < count [assigned ]; i ++) {
63
+ int person = this . people [assigned ][i ];
43
64
if ((people & (1 << person )) == 0 ) {
44
65
int changedPeople = people ;
45
66
changedPeople = changedPeople | (1 << person );
46
67
answer = (answer + possiblities (changedPeople , assigned + 1 )) % MOD ;
47
68
}
48
69
}
70
+ answer = (answer + possiblities (people , assigned + 1 )) % MOD ;
71
+ dp [people ][assigned ] = answer ;
49
72
return answer ;
50
73
}
51
74
}
0 commit comments