結果

問題 No.1407 Kindness
ユーザー norioc
提出日時 2025-05-21 11:06:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 193 ms / 2,000 ms
コード長 1,001 bytes
コンパイル時間 375 ms
コンパイル使用メモリ 82,168 KB
実行使用メモリ 101,980 KB
最終ジャッジ日時 2025-05-21 11:06:49
合計ジャッジ時間 5,814 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

def ndlist(shape: list[int], *, val=0) -> list:
    assert len(shape) > 0 and all(s > 0 for s in shape)

    def rec(p):
        if p == len(shape)-1:
            return [val] * shape[p]

        return [rec(p+1) for _ in range(shape[p])]

    return rec(0)


def digit_dp(s: str) -> int:
    ds = [int(c) for c in s]
    nd = len(ds)

    dp = ndlist([nd+1, 2, 2])
    # dp[i][j][k]
    # i : i 桁目までみた
    # j : n 未満か(j=0 完全一致 j=1 より小さい)
    # k : leading zero
    dp[0][0][1] = 1

    for i in range(nd):
        for j in range(2):
            to = ds[i] if j == 0 else 9

            dp[i+1][1][1] = 1
            for k in range(2):  # leading zero
                for x in range(1, to+1):
                    nj = j | (x < to)
                    nk = 0

                    dp[i+1][nj][nk] += dp[i][j][k] * x
                    dp[i+1][nj][nk] %= MOD

    return (dp[nd][0][0] + dp[nd][1][0]) % MOD


MOD = 10**9 + 7
N = input()

ans = digit_dp(N)
print(ans)
0