結果

問題 No.189 SUPER HAPPY DAY
ユーザー terasaterasa
提出日時 2022-11-04 21:34:25
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 581 ms / 5,000 ms
コード長 3,657 bytes
コンパイル時間 119 ms
コンパイル使用メモリ 82,720 KB
実行使用メモリ 108,288 KB
最終ジャッジ日時 2024-07-18 19:17:27
合計ジャッジ時間 7,165 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 101 ms
78,976 KB
testcase_01 AC 110 ms
79,360 KB
testcase_02 AC 110 ms
78,720 KB
testcase_03 AC 131 ms
79,616 KB
testcase_04 AC 116 ms
79,872 KB
testcase_05 AC 118 ms
79,360 KB
testcase_06 AC 107 ms
79,488 KB
testcase_07 AC 111 ms
79,488 KB
testcase_08 AC 110 ms
79,488 KB
testcase_09 AC 108 ms
79,744 KB
testcase_10 AC 109 ms
79,104 KB
testcase_11 AC 111 ms
79,872 KB
testcase_12 AC 109 ms
79,232 KB
testcase_13 AC 330 ms
87,552 KB
testcase_14 AC 388 ms
89,856 KB
testcase_15 AC 358 ms
90,880 KB
testcase_16 AC 381 ms
92,288 KB
testcase_17 AC 427 ms
95,488 KB
testcase_18 AC 358 ms
89,600 KB
testcase_19 AC 443 ms
93,440 KB
testcase_20 AC 222 ms
83,456 KB
testcase_21 AC 250 ms
83,840 KB
testcase_22 AC 130 ms
80,256 KB
testcase_23 AC 336 ms
97,152 KB
testcase_24 AC 344 ms
93,952 KB
testcase_25 AC 581 ms
108,288 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from typing import List, Tuple, Callable, TypeVar
import sys
import itertools
import heapq
import bisect
from collections import deque, defaultdict
from functools import lru_cache, cmp_to_key

input = sys.stdin.readline

# for AtCoder Easy test
if __file__ != 'prog.py':
    sys.setrecursionlimit(10 ** 6)


def readints(): return map(int, input().split())
def readlist(): return list(readints())
def readstr(): return input().rstrip()


T = TypeVar('T')


class Rerooting:
    # reference: https://null-mn.hatenablog.com/entry/2020/04/14/124151
    # 適当な頂点vを根とする部分木に対して計算される値dp_vが、vの子c1, c2, ... ckを用いて
    # 下記のように表すことができる
    # dp_v = g(merge(f(dp_c1,c1), f(dp_c2,c2), ..., f(dp_ck,ck)), v)
    def __init__(self, N: int, E: List[Tuple[int, int]],
                 f: Callable[[T, int, int, int], T],
                 g: Callable[[T, int], T],
                 merge: Callable[[T, T], T], e: T):
        self.N = N
        self.E = E
        self.f = f
        self.g = g
        self.merge = merge
        self.e = e

        self.dp = [[self.e for _ in range(len(self.E[v]))] for v in range(self.N)]
        self._calculate()

    def _dfs1(self, root):
        stack = [(root, -1)]
        ret = [self.e] * self.N
        while stack:
            v, p = stack.pop()
            if v < 0:
                v = ~v
                acc = self.e
                for i, (c, d) in enumerate(self.E[v]):
                    if d == p:
                        continue
                    self.dp[v][i] = ret[d]
                    acc = self.merge(acc, self.f(ret[d], v, d, c))
                ret[v] = self.g(acc, v)
                continue

            stack.append((~v, p))
            for i, (c, d) in enumerate(self.E[v]):
                if d == p:
                    continue
                stack.append((d, v))

    def _dfs2(self, root):
        stack = [(root, -1, self.e)]
        while stack:
            v, p, from_par = stack.pop()
            for i, (c, d) in enumerate(self.E[v]):
                if d == p:
                    self.dp[v][i] = from_par
                    break
            ch = len(self.E[v])
            Sr = [self.e] * (ch + 1)
            for i in range(ch, 0, -1):
                c, d = self.E[v][i - 1]
                Sr[i - 1] = self.merge(Sr[i], self.f(self.dp[v][i - 1], v, d, c))
            Sl = self.e
            for i, (c, d) in enumerate(self.E[v]):
                if d != p:
                    val = self.merge(Sl, Sr[i + 1])
                    stack.append((d, v, self.g(val, v)))
                Sl = self.merge(Sl, self.f(self.dp[v][i], v, d, c))

    def _calculate(self, root=0):
        self._dfs1(root)
        self._dfs2(root)

    def solve(self, v):
        ans = self.e
        for i, (c, d) in enumerate(self.E[v]):
            ans = self.merge(ans, self.f(self.dp[v][i], v, d, c))
        return self.g(ans, v)


M, D = input().split()
mod = 10 ** 9 + 9


def solve(n):
    dp = [[0, 0] for _ in range(L + 1)]
    dp[0][0] = 1
    for i in range(len(n)):
        ndp = [[0, 0] for _ in range(L + 1)]
        for j in range(L + 1):
            for k in range(10):
                if j - k < 0:
                    break
                ndp[j][1] += dp[j - k][1]

                if k < int(n[i]):
                    ndp[j][1] += dp[j - k][0]
                elif k == int(n[i]):
                    ndp[j][0] += dp[j - k][0]
        dp = ndp
    return dp


L = 2000
dpm = solve(M)
dpd = solve(D)
ans = 0
for i in range(1, L + 1):
    ans += sum(dpm[i]) * sum(dpd[i]) % mod
    ans %= mod
print(ans)
0