結果
| 問題 | 
                            No.1634 Sorting Integers (Multiple of K) Hard
                             | 
                    
| コンテスト | |
| ユーザー | 
                             lam6er
                         | 
                    
| 提出日時 | 2025-04-15 22:56:54 | 
| 言語 | PyPy3  (7.3.15)  | 
                    
| 結果 | 
                             
                                TLE
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 2,683 bytes | 
| コンパイル時間 | 317 ms | 
| コンパイル使用メモリ | 81,548 KB | 
| 実行使用メモリ | 279,272 KB | 
| 最終ジャッジ日時 | 2025-04-15 22:59:19 | 
| 合計ジャッジ時間 | 16,028 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge2 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 13 TLE * 15 | 
ソースコード
import sys
from sys import stdin
from collections import defaultdict
def main():
    N, K = map(int, stdin.readline().split())
    original_counts = list(map(int, stdin.readline().split()))
    original_counts = original_counts[:9]  # Ensure only 9 elements
    
    # Compute weights for each position
    positions = list(range(N))
    weights = []
    for j in positions:
        exponent = N - 1 - j
        w = pow(10, exponent, K) if K != 0 else 0
        weights.append(w)
    
    # Split into group A and group B
    m = N // 2
    group_a_positions = positions[:m]
    group_b_positions = positions[m:]
    
    # Process group A
    group_a_dict = defaultdict(lambda: defaultdict(int))
    
    def backtrack_a(pos_idx, used, current_sum):
        if pos_idx == len(group_a_positions):
            remaining = tuple(original_counts[i] - used[i] for i in range(9))
            group_a_dict[remaining][current_sum % K] += 1
            return
        current_pos = group_a_positions[pos_idx]
        w = weights[current_pos]
        for digit in range(1, 10):
            idx = digit - 1
            if original_counts[idx] == 0:
                continue
            if used[idx] >= original_counts[idx]:
                continue
            new_used = list(used)
            new_used[idx] += 1
            new_sum = current_sum + digit * w
            backtrack_a(pos_idx + 1, tuple(new_used), new_sum)
    
    backtrack_a(0, tuple([0]*9), 0)
    
    # Process group B
    group_b_dict = defaultdict(lambda: defaultdict(int))
    
    def backtrack_b(pos_idx, used, current_sum):
        if pos_idx == len(group_b_positions):
            required = tuple(used)
            group_b_dict[required][current_sum % K] += 1
            return
        current_pos = group_b_positions[pos_idx]
        w = weights[current_pos]
        for digit in range(1, 10):
            idx = digit - 1
            if original_counts[idx] == 0:
                continue
            if used[idx] >= original_counts[idx]:
                continue
            new_used = list(used)
            new_used[idx] += 1
            new_sum = current_sum + digit * w
            backtrack_b(pos_idx + 1, tuple(new_used), new_sum)
    
    backtrack_b(0, tuple([0]*9), 0)
    
    # Combine the results
    total = 0
    for remaining in group_a_dict:
        if remaining not in group_b_dict:
            continue
        a_sums = group_a_dict[remaining]
        b_sums = group_b_dict[remaining]
        for s_a in a_sums:
            target = (-s_a) % K
            if target in b_sums:
                total += a_sums[s_a] * b_sums[target]
    
    print(total)
if __name__ == '__main__':
    main()
            
            
            
        
            
lam6er