結果
| 問題 | No.2071 Shift and OR | 
| コンテスト | |
| ユーザー |  tenten | 
| 提出日時 | 2022-09-20 08:43:17 | 
| 言語 | Java (openjdk 23) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 114 ms / 2,000 ms | 
| コード長 | 2,155 bytes | 
| コンパイル時間 | 2,221 ms | 
| コンパイル使用メモリ | 77,672 KB | 
| 実行使用メモリ | 39,704 KB | 
| 最終ジャッジ日時 | 2024-12-22 03:21:43 | 
| 合計ジャッジ時間 | 6,901 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 37 | 
ソースコード
import java.io.*;
import java.util.*;
public class Main {
    static boolean[][] dp;
    static boolean[][] visited;
    static int[] values;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        if (n >= 16) {
            System.out.println((1 << 16) - 1);
            return;
        }
        values = new int[n];
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt();
        }
        dp = new boolean[n][1 << 16];
        visited = new boolean[n][1 << 16];
        for (int i = (1 << 16) -1; i > 0; i--) {
            if (dfw(n - 1, i)) {
                System.out.println(i);
                return;
            }
        }
    }
    
    static boolean dfw(int idx, int mask) {
        if (mask == 0) {
            return true;
        }
        if (idx < 0) {
            return false;
        }
        if (visited[idx][mask]) {
            return dp[idx][mask];
        }
        visited[idx][mask] = true;
        int value = values[idx];
        for (int i = 0; i < 16; i++) {
            dp[idx][mask] = dfw(idx - 1, mask ^ (mask & value));
            if (dp[idx][mask]) {
                return true;
            }
            value = next(value);
        }
        return dp[idx][mask];
    }
    
    static int next(int x) {
        return (x >> 1) + (1 << 15) * (x % 2);
        
    }
}
    
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 String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
            
            
            
        