結果

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

ソースコード

diff #

import sys
from functools import lru_cache

def main():
    N, K = map(int, sys.stdin.readline().split())
    c = list(map(int, sys.stdin.readline().split()))
    
    max_sum = 10**N - 1
    if K > max_sum:
        print(0)
        return
    
    exponents = [pow(10, (N-1 - p), K) for p in range(N)]
    counts = tuple(c)
    
    @lru_cache(maxsize=None)
    def dp(pos, cnts, rem):
        if pos == N:
            return 1 if rem == 0 else 0
        total = 0
        for d in range(9):
            if cnts[d] == 0:
                continue
            new_cnts = list(cnts)
            new_cnts[d] -= 1
            new_cnts = tuple(new_cnts)
            e = exponents[pos]
            contribution = (d + 1) * e
            new_rem = (rem + contribution) % K
            total += dp(pos + 1, new_cnts, new_rem)
        return total
    
    result = dp(0, counts, 0)
    print(result)

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