結果

問題 No.1355 AND OR GAME
ユーザー LyricalMaestro
提出日時 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
権限があれば一括ダウンロードができます

ソースコード

diff #

## https://yukicoder.me/problems/no/1355

def calc_bit(X, max_k):
    array = [0] * max_k
    for i in range(max_k):
        array[i] = X % 2
        X //= 2
    return array

def main():
    N, X, Y = map(int, input().split())
    A = list(map(int, input().split()))

    max_value = max(X, Y, max(A))
    k = 0
    while (1 << k) < max_value:
        k += 1
    max_k = k + 1

    x_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_bit
    answer_p = []
    same_flg = False
    for i in reversed(range(N)):
        a_bit = a_bits[i]
        
        # 一致している?
        is_ok = True
        for k in range(max_k):
            if current_bit[k] != -1 and current_bit[k] != a_bit[k]:
                is_ok = False
                break
        if is_ok:
            answer_p.append(3)
            same_flg = True
            break

        # 包含している?(X and A[i] = currentとなりえる?)
        is_ok2 = True
        for k in range(max_k):
            if current_bit[k] != -1:
                if current_bit[k] == 1 and a_bit[k] == 0:
                    is_ok2 = False
                    break
                    

        # 包含されている?(X or A[i] = currentとなりえる?)
        is_ok3 = True
        for k in range(max_k):
            if current_bit[k] != -1:
                if current_bit[k] == 0 and a_bit[k] == 1:
                    is_ok3 = False
                    break
        
        if (not is_ok2) and (not is_ok3):
            print(-1)
            return
        elif is_ok2:
            # X and A[i] = currentは成り立つ
            for k in range(max_k):
                if current_bit[k] == -1:
                    current_bit[k] = -1
                else:
                    if a_bit[k] == 1:
                        current_bit[k] = current_bit[k]
                    else:
                        current_bit[k] = -1
            answer_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] = -1
                else:
                    if a_bit[k] == 1:
                        current_bit[k] = -1
                    else:
                        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 = True
        for k in range(max_k):
            if current_bit[k] != -1 and current_bit[k] != x_bit[k]:
                is_ok = False
                break
        if not is_ok:
            print(-1)
            return
        else:
            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()
0