結果

問題 No.1631 Sorting Integers (Multiple of K) Easy
ユーザー gew1fw
提出日時 2025-06-12 13:31:17
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,079 bytes
コンパイル時間 350 ms
コンパイル使用メモリ 82,972 KB
実行使用メモリ 319,944 KB
最終ジャッジ日時 2025-06-12 13:36:39
合計ジャッジ時間 6,257 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 10 TLE * 1 -- * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

import math
from collections import defaultdict

def main():
    N, K = map(int, input().split())
    c = list(map(int, input().split()))
    original_counts = tuple(c)
    
    product = 1
    for count in original_counts:
        product *= math.factorial(count)
    
    dp = defaultdict(int)
    initial_counts = original_counts
    dp[(0, initial_counts)] = 1
    
    for _ in range(N):
        next_dp = defaultdict(int)
        for (r, counts), ways in dp.items():
            for i in range(9):
                if counts[i] == 0:
                    continue
                digit = i + 1
                new_r = (r * 10 + digit) % K
                new_counts = list(counts)
                new_counts[i] -= 1
                new_counts_tuple = tuple(new_counts)
                next_dp[(new_r, new_counts_tuple)] += ways * counts[i]
        dp = next_dp
    
    code_answer = 0
    for (r, counts), ways in dp.items():
        if r == 0:
            code_answer += ways
    
    answer = code_answer // product
    print(answer)

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