結果
問題 | No.1560 majority x majority |
ユーザー | tenten |
提出日時 | 2021-06-28 13:22:20 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 633 ms / 2,000 ms |
コード長 | 2,372 bytes |
コンパイル時間 | 2,329 ms |
コンパイル使用メモリ | 78,668 KB |
実行使用メモリ | 79,612 KB |
最終ジャッジ日時 | 2024-06-25 12:09:20 |
合計ジャッジ時間 | 11,083 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 26 |
ソースコード
import java.io.*; import java.util.*; public class Main { static int n; static int m; static boolean[][] votes; static boolean[][] unables; static int[] dp; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); n = sc.nextInt(); m = sc.nextInt(); votes = new boolean[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { votes[i][j] = (sc.nextInt() == 1); } } unables = new boolean[1 << m][n]; for (int i = 0; i < (1 << m); i++) { for (int j = 0; j < m; j++) { if ((i & (1 << j)) != 0) { continue; } for (int k = 0; k < n; k++) { unables[i][k] |= !votes[k][j]; } } } dp = new int[1 << m]; Arrays.fill(dp, -1); dp[0] = 1; System.out.println(dfw((1 << m) - 1)); } static int dfw(int mask) { if (dp[mask] < 0) { dp[mask] = 0; for (int i = 0; i < m; i++) { if ((mask & (1 << i)) == 0) { continue; } int agree = 0; int against = 0; for (int j = 0; j < n; j++) { if (unables[mask][j]) { continue; } if (votes[j][i]) { agree++; } else { against++; } } if (agree >= against) { dp[mask] += dfw(mask ^ (1 << i)); } } } return dp[mask]; } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public String next() throws Exception { if (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }