結果

問題 No.939 and or
ユーザー FromBooska
提出日時 2023-03-07 18:55:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 38 ms / 2,000 ms
コード長 1,221 bytes
コンパイル時間 162 ms
コンパイル使用メモリ 82,300 KB
実行使用メモリ 53,984 KB
最終ジャッジ日時 2024-09-18 02:13:21
合計ジャッジ時間 2,255 ms
ジャッジサーバーID
(参考情報)
judge4 / judge6
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

# AND、つまりAの桁が1ならば、XもYもその桁は1
# OR、つまりBの桁が0ならば、XもYもその桁は0
# ここまでに矛盾があればそんなX, Yは存在しないので0
# 残りのありうる組合せはANDの桁が0でORの桁が1、逆はありえない
# ANDの桁が0でORの桁が1の数がn個なら、組合せ数は2**(n-1)ではないか

A, B = map(int, input().split())
#print(bin(A), bin(B))
if A == B:
    # 実験したら同じ数のときにエラー出ていた
    ans = 1
    print(ans)
    exit()

test_from_A = True
A_len = len(bin(A))-2
for shift in range(A_len):
    if A>>shift & 1 == 1:
        if B>>shift & 1 != 1:
            test_from_A = False
test_from_B = True
B_len = len(bin(B))-2
for shift in range(B_len):
    if B>>shift & 1 != 1:
        if A>>shift & 1 == 1:
            test_from_B = False            
#print(test_from_A, test_from_B)
count = 0
max_len = max(A_len, B_len)
# A_lenで数え上げるとB_lenの方が長いときにミスる
for shift in range(max_len):
    if A>>shift & 1 != 1:
        if B>>shift & 1 == 1:
            count += 1
if test_from_A == True and test_from_B == True:
    ans = 2**(count-1)
    print(ans)
else:
    print(0)
0