結果
| 問題 | No.220 世界のなんとか2 |
| コンテスト | |
| ユーザー |
norioc
|
| 提出日時 | 2026-01-07 23:10:10 |
| 言語 | PyPy3 (7.3.17) |
| 結果 |
AC
|
| 実行時間 | 48 ms / 1,000 ms |
| コード長 | 1,311 bytes |
| 記録 | |
| コンパイル時間 | 364 ms |
| コンパイル使用メモリ | 82,328 KB |
| 実行使用メモリ | 64,644 KB |
| 最終ジャッジ日時 | 2026-01-07 23:10:13 |
| 合計ジャッジ時間 | 2,433 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 19 |
ソースコード
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):
lz, m3, c3, lt = k # (leading zero, mod, 3を含むか, 未満か)
if lz:
for d in range(1, 10):
nm3 = d % 3
nc3 = d == 3
yield (False, nm3, nc3, True), 1
yield (True, 0, 0, False), 0 # leading zero 維持
else:
for d in range(10):
if not lt and d > x: break
nm3 = (10*m3 + d) % 3
nc3 = c3 | (d == 3)
nlt = lt | (d < x)
yield (False, nm3, nc3, nlt), v
def op(a, b):
return a + b
def digit_dp() -> int:
digits = [int(c) for c in str(10**P)]
init = {(True, 0, 0, False): 1}
init[False, 1, False, False] = 1 # 先頭桁
dp = accum_dp(digits[1:], f, op, 0, init)
res = 0
for (lz, m3, c3, _), v in dp.items():
if lz: continue
if m3 == 0 or c3:
res += v
return res
P = int(input())
ans = digit_dp()
print(ans)
norioc