結果

問題 No.1560 majority x majority
ユーザー tentententen
提出日時 2023-02-02 15:37:09
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 4,258 bytes
コンパイル時間 2,616 ms
コンパイル使用メモリ 94,400 KB
実行使用メモリ 81,916 KB
最終ジャッジ日時 2023-09-14 21:32:18
合計ジャッジ時間 11,909 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 684 ms
74,196 KB
testcase_01 AC 556 ms
69,364 KB
testcase_02 AC 43 ms
49,340 KB
testcase_03 AC 138 ms
55,296 KB
testcase_04 WA -
testcase_05 AC 1,564 ms
81,916 KB
testcase_06 AC 75 ms
51,192 KB
testcase_07 AC 123 ms
53,564 KB
testcase_08 AC 147 ms
54,820 KB
testcase_09 AC 99 ms
52,304 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 107 ms
52,016 KB
testcase_14 AC 119 ms
53,496 KB
testcase_15 AC 172 ms
55,428 KB
testcase_16 AC 100 ms
52,816 KB
testcase_17 AC 327 ms
58,524 KB
testcase_18 AC 241 ms
57,804 KB
testcase_19 AC 60 ms
50,496 KB
testcase_20 AC 82 ms
52,056 KB
testcase_21 AC 99 ms
51,636 KB
testcase_22 AC 273 ms
58,256 KB
testcase_23 AC 79 ms
51,872 KB
testcase_24 AC 80 ms
50,896 KB
testcase_25 AC 282 ms
59,032 KB
testcase_26 AC 44 ms
49,312 KB
testcase_27 AC 44 ms
49,352 KB
testcase_28 AC 46 ms
49,304 KB
testcase_29 AC 47 ms
49,692 KB
testcase_30 AC 54 ms
50,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    static int m;
    static Judge[] judges;
    static HashMap<Judge, HashMap<Integer, Integer>> dp = new HashMap<>();
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        m = sc.nextInt();
        Judge.size = n;
        judges = new Judge[m];
        for (int i = 0; i < m; i++) {
            judges[i] = new Judge();
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (sc.nextInt() == 1) {
                    judges[j].add(i);
                }
            }
        }
        for (int i = 0; i < m; i++) {
            judges[i].makePop();
        }
        Judge all = new Judge();
        for (int i = 0; i < n; i++) {
            all.add(i);
        }
        System.out.println(dfw(all, (1 << m) - 1));
    }
    
    static int dfw(Judge x, int mask) {
        if (mask == 0) {
            return 1;
        }
        if (!dp.containsKey(x)) {
            dp.put(x, new HashMap<>());
        }
        if (!dp.get(x).containsKey(mask)) {
            int ans = 0;
            for (int i = 0; i < m; i++) {
                if ((mask & (1 << i)) == 0) {
                    continue;
                }
                Judge next = judges[i].and(x);
                if (x.pop <= next.pop * 2) {
                    ans += dfw(next, mask ^ (1 << i));
                }
            }
            dp.get(x).put(mask, ans);
        }
        return dp.get(x).get(mask);
    }
    
    static class Judge {
        static int size;
        int length;
        long[] data;
        int pop;
        
        public Judge() {
            length = (size + 60 - 1) / 60;
            data = new long[length];
        }
        
        public Judge(int length, long[] data) {
            this.length = length;
            this.data = data;
        }
        
        public void add(int x) {
            data[x / 60] |= (1L << (x % 60));
        }
        
        public void makePop() {
            for (long x : data) {
                long y = x;
                while (y > 0) {
                    if (y % 2 == 1) {
                        pop++;
                    }
                    y >>= 1;
                }
            }
        }
        
        public Judge and(Judge x) {
            long[] next = new long[length];
            for (int i = 0; i < length; i++) {
                next[i] = (data[i] & x.data[i]);
            }
            Judge ans = new Judge(length, next);
            ans.makePop();
            return ans;
        }
        
        public int hashCode() {
            return pop;
        }
        
        public boolean equals(Object o) {
            Judge x = (Judge)o;
            for (int i = 0; i < length; i++) {
                if (data[i] != x.data[i]) {
                    return false;
                }
            }
            return true;
        }
    }
}

class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0