結果

問題 No.1505 Zero-Product Ranges
コンテスト
ユーザー norioc
提出日時 2026-01-18 15:39:59
言語 PyPy3
(7.3.17)
結果
AC  
実行時間 294 ms / 2,000 ms
コード長 932 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 391 ms
コンパイル使用メモリ 82,380 KB
実行使用メモリ 104,828 KB
最終ジャッジ日時 2026-01-18 15:40:12
合計ジャッジ時間 12,302 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 49
権限があれば一括ダウンロードができます

ソースコード

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 op(a, b):
    return a + b


ans = 0


def f(k, v, x):
    global ans
    t, b = k  # (取った, 0を含むか)

    if t == 0:
        yield k, v
        # 取る
        nb = b | (x == 0)
        yield (1, nb), v
    elif t == 1:
        nb = b | (x == 0)
        yield (1, nb), v

        if b:
            ans += v
    else:
        assert False


N = int(input())
A = list(map(int, input().split()))

init = {(0, False): 1}
dp = accum_dp(A, f, op, 0, init)
for (t, b), v in dp.items():
    if t == 1 and b:
        ans += v

print(ans)
0