結果
問題 | No.1355 AND OR GAME |
ユーザー |
|
提出日時 | 2024-12-06 01:33:55 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 447 ms / 2,000 ms |
コード長 | 3,233 bytes |
コンパイル時間 | 343 ms |
コンパイル使用メモリ | 82,560 KB |
実行使用メモリ | 226,724 KB |
最終ジャッジ日時 | 2024-12-06 01:34:26 |
合計ジャッジ時間 | 26,652 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 95 |
ソースコード
## https://yukicoder.me/problems/no/1355def calc_bit(X, max_k):array = [0] * max_kfor i in range(max_k):array[i] = X % 2X //= 2return arraydef main():N, X, Y = map(int, input().split())A = list(map(int, input().split()))max_value = max(X, Y, max(A))k = 0while (1 << k) < max_value:k += 1max_k = k + 1x_bit = calc_bit(X, max_k)y_bit = calc_bit(Y, max_k)a_bits = []for a in A:a_bit = calc_bit(a, max_k)a_bits.append(a_bit)current_bit = y_bitanswer_p = []same_flg = Falsefor i in reversed(range(N)):a_bit = a_bits[i]# 一致している?is_ok = Truefor k in range(max_k):if current_bit[k] != -1 and current_bit[k] != a_bit[k]:is_ok = Falsebreakif is_ok:answer_p.append(3)same_flg = Truebreak# 包含している?(X and A[i] = currentとなりえる?)is_ok2 = Truefor k in range(max_k):if current_bit[k] != -1:if current_bit[k] == 1 and a_bit[k] == 0:is_ok2 = Falsebreak# 包含されている?(X or A[i] = currentとなりえる?)is_ok3 = Truefor k in range(max_k):if current_bit[k] != -1:if current_bit[k] == 0 and a_bit[k] == 1:is_ok3 = Falsebreakif (not is_ok2) and (not is_ok3):print(-1)returnelif is_ok2:# X and A[i] = currentは成り立つfor k in range(max_k):if current_bit[k] == -1:current_bit[k] = -1else:if a_bit[k] == 1:current_bit[k] = current_bit[k]else:current_bit[k] = -1answer_p.append(1)elif is_ok3:# X or A[i] = currentは成り立つfor k in range(max_k):if current_bit[k] == -1:current_bit[k] = -1else:if a_bit[k] == 1:current_bit[k] = -1else:current_bit[k] = current_bit[k]answer_p.append(2)if same_flg:for _ in range(len(answer_p), N):answer_p.append(3)answer_p.reverse()print(" ".join(map(str, answer_p)))else:# 一致している?is_ok = Truefor k in range(max_k):if current_bit[k] != -1 and current_bit[k] != x_bit[k]:is_ok = Falsebreakif not is_ok:print(-1)returnelse:answer_p.reverse()print(" ".join(map(str, answer_p)))# 剣山# x = X# for i in range(N):# x = x & A[i]# elif answer_p[i] == 2:# x = x | A[i]# else:# x = A[i]# print(Y, x)if __name__ == "__main__":main()