結果

問題 No.1634 Sorting Integers (Multiple of K) Hard
ユーザー gew1fw
提出日時 2025-06-12 20:52:21
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 1,429 bytes
コンパイル時間 198 ms
コンパイル使用メモリ 82,052 KB
実行使用メモリ 602,212 KB
最終ジャッジ日時 2025-06-12 20:56:41
合計ジャッジ時間 6,761 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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()))
    counts = tuple(c)
    
    # Precompute the weights for each position p
    weight = []
    for p in range(N):
        exponent = N - 1 - p
        w = pow(10, exponent, K)
        weight.append(w)
    
    # Initialize DP
    current_dp = defaultdict(int)
    current_dp[(counts, 0)] = 1
    
    for p in range(N):
        next_dp = defaultdict(int)
        for (current_counts, mod), ways in current_dp.items():
            current_counts_list = list(current_counts)
            for d in range(1, 10):
                if current_counts_list[d - 1] == 0:
                    continue
                # Create new counts
                new_counts = list(current_counts_list)
                new_counts[d - 1] -= 1
                new_counts_tuple = tuple(new_counts)
                # Compute new mod
                contribution = d * weight[p]
                new_mod = (mod + contribution) % K
                # Update next_dp
                key = (new_counts_tuple, new_mod)
                next_dp[key] += ways
        current_dp = next_dp
    
    # The final state is all counts zero and mod 0
    final_counts = (0, 0, 0, 0, 0, 0, 0, 0, 0)
    answer = current_dp.get((final_counts, 0), 0)
    
    print(answer)

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