結果

問題 No.685 Logical Operations
コンテスト
ユーザー norioc
提出日時 2026-01-07 13:04:43
言語 PyPy3
(7.3.17)
結果
AC  
実行時間 47 ms / 2,000 ms
コード長 1,962 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 290 ms
コンパイル使用メモリ 82,204 KB
実行使用メモリ 62,624 KB
最終ジャッジ日時 2026-01-07 13:04:46
合計ジャッジ時間 2,949 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, 未満か)
    ds = [(0, 0), (0, 1), (1, 0), (1, 1)]

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

            if xd < yd:
                yield (state+1, n_xlt, n_ylt), v
            else:
                yield (state, n_xlt, n_ylt), v
    elif state == 1:  # x < y かつ (x and y) < (x xor y)
        for xd, yd in ds:
            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 xd == yd == 1:
                yield (state+1, n_xlt, n_ylt), v
            else:
                yield (state, n_xlt, n_ylt), v
    elif state == 2:
        for xd, yd in ds:
            if not xlt and xd > x: continue
            if not ylt and yd > x: continue
            n_xlt = xlt | (xd < x)
            n_ylt = ylt | (yd < x)
            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