結果

問題 No.1560 majority x majority
ユーザー tenten
提出日時 2023-02-02 15:37:09
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 4,258 bytes
コンパイル時間 2,942 ms
コンパイル使用メモリ 95,864 KB
実行使用メモリ 69,380 KB
最終ジャッジ日時 2024-07-02 04:18:54
合計ジャッジ時間 11,585 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 22 WA * 4
権限があれば一括ダウンロードができます

ソースコード

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