結果

問題 No.737 PopCount
コンテスト
ユーザー norioc
提出日時 2026-01-06 20:09:54
言語 PyPy3
(7.3.17)
結果
AC  
実行時間 62 ms / 1,000 ms
コード長 1,354 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 209 ms
コンパイル使用メモリ 82,168 KB
実行使用メモリ 71,812 KB
最終ジャッジ日時 2026-01-06 20:09:56
合計ジャッジ時間 2,311 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

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, ix):
    b, lt = k     # (1の個数, 未満か)
    cnt, tot = v  # (個数, 総和)

    e, x = ix
    ntot = tot + cnt * pow(2, e, MOD)
    if lt:
        yield k, v
        yield (b+1, lt), (cnt, ntot)  # 1 を立てる
    elif x == 0:
        yield k, v
    elif x == 1:
        yield (b, True), v  # 0 にする
        yield (b+1, False), (cnt, ntot)
    else:
        assert False


def op(a, b):
    a1, a2 = a
    b1, b2 = b
    return (a1+b1) % MOD, (a2+b2) % MOD


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

    init = {(0, False): (1, 0)}
    nd = len(digits)
    xs = [(e, d) for e, d in zip(reversed(range(nd)), digits)]
    dp = accum_dp(xs, f, op, (0, 0), init)
    res = 0
    for i in range(1, 64):
        a = dp.get((i, True), (0, 0))
        b = dp.get((i, False), (0, 0))
        res += (a[1] + b[1]) * i
        res %= MOD

    return res

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