fork download
  1. import java.util.*;
  2. import java.io.*;
  3. import static java.lang.Math.max;
  4. import static java.lang.Math.min;
  5. import static java.lang.Math.abs;
  6. public class Main {
  7. public static int mod = (int) 1e9 + 7;
  8.  
  9. static class FastReader {
  10.  
  11. public FastReader() {
  12. }
  13.  
  14. String next() {
  15. while (st == null || !st.hasMoreTokens()) {
  16. try {
  17. st = new StringTokenizer(br.readLine());
  18. } catch (IOException e) {
  19. e.printStackTrace();
  20. }
  21. }
  22. return st.nextToken();
  23. }
  24.  
  25. int nextInt() {
  26. return Integer.parseInt(next());
  27. }
  28.  
  29. long nextLong() {
  30. return Long.parseLong(next());
  31. }
  32.  
  33. double nextDouble() {
  34. return Double.parseDouble(next());
  35. }
  36.  
  37. String nextLine() {
  38. String str = "";
  39. try {
  40. str = br.readLine().trim();
  41. } catch (Exception e) {
  42. e.printStackTrace();
  43. }
  44. return str;
  45. }
  46. }
  47.  
  48. static class FastWriter {
  49. private final BufferedWriter bw;
  50.  
  51. public FastWriter() {
  52. this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
  53. }
  54.  
  55. public void print(Object object) throws IOException {
  56. bw.append("" + object);
  57. }
  58.  
  59. public void println(Object object) throws IOException {
  60. print(object);
  61. bw.append("\n");
  62. }
  63.  
  64. public void println() throws IOException {
  65. bw.append("\n");
  66. }
  67.  
  68. public void close() throws IOException {
  69. bw.close();
  70. }
  71.  
  72. public void printLongArr(long[] arr) throws IOException {
  73. for (long ele : arr) {
  74. print(ele + " ");
  75. }
  76. println();
  77. }
  78.  
  79. public void printIntArr(int[] arr) throws IOException {
  80. for (int ele : arr) {
  81. print(ele + " ");
  82. }
  83. println();
  84. }
  85. }
  86. public static long helper(int[][] mat,int mask,int size, long[]dp){
  87. int student = Integer.bitCount(mask);
  88. if( student>=size){
  89. return 1;
  90. }
  91. if(dp[mask]!=-1){
  92. return dp[mask];
  93. }
  94. long ans = 0;
  95. for (int topic = 0; topic < size; topic++) {
  96. int currentMask = 1<<topic;
  97. if((mask&currentMask) == 0 && mat[student][topic]==1) {
  98. ans += helper(mat, mask | currentMask, size,dp);
  99. }
  100. }
  101. return dp[mask]=ans;
  102. }
  103. public static void main
  104. (String[] args) {
  105. try {
  106. FastReader fin = new FastReader();
  107. FastWriter fout = new FastWriter();
  108.  
  109. int t = fin.nextInt();
  110. while(t-- > 0) {
  111. int n = fin.nextInt();
  112. int[][] likabilityMatrix = new int[n][n];
  113. for (int i = 0; i < n; i++) {
  114. for (int j = 0; j < n; j++) {
  115.  
  116. likabilityMatrix[i][j] = fin.nextInt(); }
  117. }
  118. long[] dp = new long [1<<(n + 1)];
  119. Arrays.fill(dp,-1);
  120. fout.println(helper(likabilityMatrix,0,n,dp));
  121. }
  122. fout.close();
  123. } catch (Exception e) {
  124. e.printStackTrace();
  125. return;
  126. }
  127. }
  128. }
  129.  
Success #stdin #stdout 0.16s 57552KB
stdin
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 
stdout
6
7588
7426