結果

問題 No.3045 反復重み付き累積和
ユーザー gew1fw
提出日時 2025-06-12 15:06:05
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,205 bytes
コンパイル時間 187 ms
コンパイル使用メモリ 82,688 KB
実行使用メモリ 52,480 KB
最終ジャッジ日時 2025-06-12 15:07:07
合計ジャッジ時間 3,176 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 3
other WA * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    sys.setrecursionlimit(1 << 25)
    N, M = map(int, sys.stdin.readline().split())
    if M >= 9:
        print(0)
        return
    s = str(N)
    len_n = len(s)
    memo = {}

    def dp(pos, tight, max_so_far, leading_zero):
        if pos == len_n:
            return 1
        key = (pos, tight, max_so_far, leading_zero)
        if key in memo:
            return memo[key]
        limit = int(s[pos]) if tight else 9
        total = 0
        for d in range(0, limit + 1):
            new_tight = tight and (d == limit)
            if leading_zero:
                if d == 0:
                    new_leading_zero = True
                    new_max = max_so_far
                else:
                    new_leading_zero = False
                    new_max = d
            else:
                new_leading_zero = False
                new_max = max(max_so_far, d)
            if not leading_zero and new_max > M:
                continue
            total += dp(pos + 1, new_tight, new_max, new_leading_zero)
        memo[key] = total
        return total

    x = dp(0, True, -1, True)
    answer = (N + 1) - x
    print(answer)

if __name__ == "__main__":
    main()
0