結果

問題 No.1634 Sorting Integers (Multiple of K) Hard
ユーザー lam6er
提出日時 2025-04-15 22:53:52
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,683 bytes
コンパイル時間 311 ms
コンパイル使用メモリ 82,384 KB
実行使用メモリ 280,012 KB
最終ジャッジ日時 2025-04-15 22:56:45
合計ジャッジ時間 15,646 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 12 TLE * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

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()
0