結果

問題 No.3432 popcount & sum (Hard)
コンテスト
ユーザー norioc
提出日時 2026-01-12 20:22:16
言語 PyPy3
(7.3.17)
結果
AC  
実行時間 162 ms / 2,000 ms
コード長 1,487 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 423 ms
コンパイル使用メモリ 82,932 KB
実行使用メモリ 78,532 KB
最終ジャッジ日時 2026-01-12 20:22:20
合計ジャッジ時間 3,066 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

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):
    diff, cmp, ilt, jlt = k
    cnt, tot = v  # (個数, 総和)

    e, x = ix
    tot2 = tot + cnt * pow(2, e, MOD)
    for id in range(2):
        if not ilt and id > x: continue
        for jd in range(2):
            if not jlt and jd > x: continue
            if cmp == 0 and id > jd: continue

            n_ilt = ilt | (id < x)
            n_jlt = jlt | (jd < x)
            n_cmp = cmp | (id < jd)
            n_diff = diff + (jd - id)
            n_tot = tot2 if id == jd == 1 else tot
            yield (n_diff, n_cmp, n_ilt, n_jlt), (cnt, n_tot)


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


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

    init = {(0, 0, False, 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)
    ans = 0
    for (diff, _, _, _), v in dp.items():
        if diff == 0:
            ans += v[1]
            ans %= MOD

    return ans


MOD = 998244353
N = int(input())

ans = digit_dp()
print(ans)
0