結果

問題 No.822 Bitwise AND
ユーザー gew1fw
提出日時 2025-06-12 16:54:07
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,056 bytes
コンパイル時間 225 ms
コンパイル使用メモリ 82,816 KB
実行使用メモリ 58,240 KB
最終ジャッジ日時 2025-06-12 16:54:11
合計ジャッジ時間 1,656 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 12 WA * 5
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    N, K = map(int, sys.stdin.readline().split())

    if N == 0:
        if K >= 1:
            print("INF")
        else:
            print(1)
        return

    # Collect all the bits where N is 0
    bits = []
    for i in range(30, -1, -1):
        if (N >> i) & 1 == 0:
            bits.append(i)

    # Initialize DP
    dp = [0] * (K + 1)
    dp[0] = 1

    for i in bits:
        power = 1 << i
        tmp = [0] * (K + 1)
        for s in range(K + 1):
            if dp[s] == 0:
                continue
            # Option 1: assign to A
            s_new_a = s - power
            if s_new_a >= 0 and s_new_a <= K:
                tmp[s_new_a] += dp[s]
            # Option 2: assign to neither
            s_new_n = s
            if s_new_n <= K:
                tmp[s_new_n] += dp[s]
            # Option 3: assign to B
            s_new_b = s + power
            if s_new_b <= K:
                tmp[s_new_b] += dp[s]
        dp = tmp

    total = sum(dp)
    print(total)

if __name__ == '__main__':
    main()
0