結果
問題 |
No.1245 ANDORゲーム(calc)
|
ユーザー |
![]() |
提出日時 | 2020-10-02 22:51:15 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 620 ms / 2,000 ms |
コード長 | 1,480 bytes |
コンパイル時間 | 2,147 ms |
コンパイル使用メモリ | 76,908 KB |
実行使用メモリ | 64,992 KB |
最終ジャッジ日時 | 2024-07-17 22:51:26 |
合計ジャッジ時間 | 15,188 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 25 |
ソースコード
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] sa = br.readLine().split(" "); int n = Integer.parseInt(sa[0]); int q = Integer.parseInt(sa[1]); sa = br.readLine().split(" "); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(sa[i]); } char[] s = br.readLine().toCharArray(); sa = br.readLine().split(" "); int[] t = new int[q]; for (int i = 0; i < q; i++) { t[i] = Integer.parseInt(sa[i]); } br.close(); long[] dp0 = new long[31]; long[] dp1 = new long[31]; int now0 = 0; int now1 = Integer.MAX_VALUE; for (int i = 0; i < n; i++) { int next0 = 0; int next1 = 0; if (s[i] == '0') { next0 = now0 & a[i]; next1 = now1 & a[i]; } else { next0 = now0 | a[i]; next1 = now1 | a[i]; } for (int j = 0; j < 31; j++) { int c = 1 << j; if ((now0 & c) != (next0 & c)) { dp0[j] += c; } if ((now1 & c) != (next1 & c)) { dp1[j] += c; } } now0 = next0; now1 = next1; } PrintWriter pw = new PrintWriter(System.out); for (int i = 0; i < q; i++) { long ans = 0; for (int j = 0; j < 31; j++) { if ((t[i] >> j & 1) == 0) { ans += dp0[j]; } else { ans += dp1[j]; } } pw.println(ans); } pw.flush(); } }