結果
| 問題 | No.1505 Zero-Product Ranges |
| コンテスト | |
| ユーザー |
norioc
|
| 提出日時 | 2026-01-18 15:57:39 |
| 言語 | PyPy3 (7.3.17) |
| 結果 |
AC
|
| 実行時間 | 308 ms / 2,000 ms |
| コード長 | 1,246 bytes |
| 記録 | |
| コンパイル時間 | 287 ms |
| コンパイル使用メモリ | 82,756 KB |
| 実行使用メモリ | 104,796 KB |
| 最終ジャッジ日時 | 2026-01-18 15:58:01 |
| 合計ジャッジ時間 | 11,264 ms |
|
ジャッジサーバーID (参考情報) |
judge6 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 49 |
ソースコード
from collections.abc import Iterable
from enum import IntEnum, auto
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
class E(IntEnum):
S = 0,
TAKE = auto()
G = auto()
def f(k, v, x):
s, b = k # (状態, 0を含むか)
match s:
case E.S: # まだ取ってない
yield k, v
# 取る
nb = b | (x == 0)
yield (E.TAKE, nb), v
case E.TAKE: # 取った
nb = b | (x == 0)
yield (E.TAKE, nb), v # とり続ける
# ここで終了
if b:
yield (E.G, b), v
case E.G: # 終了状態
yield k, v
case _:
assert False
N = int(input())
A = list(map(int, input().split()))
init = {(E.S, False): 1}
dp = accum_dp(A, f, op, 0, init)
ans = 0
for (t, b), v in dp.items():
if t in [E.TAKE, E.G] and b:
ans += v
print(ans)
norioc