結果

問題 No.1634 Sorting Integers (Multiple of K) Hard
ユーザー gew1fw
提出日時 2025-06-12 20:50:29
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 1,002 bytes
コンパイル時間 142 ms
コンパイル使用メモリ 81,540 KB
実行使用メモリ 643,520 KB
最終ジャッジ日時 2025-06-12 20:55:12
合計ジャッジ時間 7,112 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 4 MLE * 1 -- * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import defaultdict

def main():
    N, K = map(int, sys.stdin.readline().split())
    c = list(map(int, sys.stdin.readline().split()))
    pow10 = [1] * N
    for i in range(1, N):
        pow10[i] = (pow10[i-1] * 10) % K

    initial_counts = tuple(c)
    dp = defaultdict(int)
    dp[(initial_counts, 0)] = 1

    for pos in range(N):
        new_dp = defaultdict(int)
        for (counts, mod), ways in dp.items():
            for d in range(9):
                if counts[d] == 0:
                    continue
                new_counts = list(counts)
                new_counts[d] -= 1
                new_counts = tuple(new_counts)
                contribution = (d + 1) * pow10[pos]
                new_mod = (mod + contribution) % K
                new_dp[(new_counts, new_mod)] += ways
        dp = new_dp

    total = 0
    for (counts, mod), ways in dp.items():
        if mod == 0:
            total += ways
    print(total)

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