結果

問題 No.685 Logical Operations
コンテスト
ユーザー norioc
提出日時 2026-01-07 19:57:01
言語 PyPy3
(7.3.17)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 1,592 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 385 ms
コンパイル使用メモリ 82,800 KB
実行使用メモリ 63,632 KB
最終ジャッジ日時 2026-01-07 19:57:04
合計ジャッジ時間 3,017 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from collections.abc import Iterable


def accum_dp(xs: Iterable, f, op, e, init: dict, *, is_reset=True):
    dp = init.copy()
    for x in xs:
        pp = {} if is_reset else dp.copy()
        dp, pp = pp, dp
        for fm_key, fm_val in pp.items():
            for to_key, to_val in f(fm_key, fm_val, x):
                dp[to_key] = op(dp.get(to_key, e), to_val)

    return dp


def f(k, v, x):
    state, xlt, ylt = k  # (state, xが未満か, yが未満か)

    for xd, yd in [(0, 0), (0, 1), (1, 0), (1, 1)]:
        if not xlt and xd > x: continue
        if not ylt and yd > x: continue
        n_xlt = xlt | (xd < x)
        n_ylt = ylt | (yd < x)

        if state == 0:  # 未確定
            if xd < yd:
                yield (state+1, n_xlt, n_ylt), v
            elif xd == yd == 0:
                yield (state, n_xlt, n_ylt), v
        elif state == 1:  # x < y かつ (x and y) < (x xor y)
            if xd == yd == 1:
                yield (state+1, n_xlt, n_ylt), v
            else:
                yield (state, n_xlt, n_ylt), v
        elif state == 2:  # 条件を全て満たした状態
            yield (state, n_xlt, n_ylt), v
        else:
            assert False


def op(a, b):
    return (a + b) % MOD


def digit_dp(n: int) -> int:
    digits = [int(c) for c in bin(n)[2:]]

    init = {(0, False, False): 1}
    dp = accum_dp(digits, f, op, 0, init)
    res = 0
    for (state, _, _), v in dp.items():
        if state == 2:
            res += v
            res %= MOD

    return res


MOD = 10**9 + 7
N = int(input())
ans = digit_dp(N)
print(ans)
0