結果

問題 No.775 tatyamと素数大富豪(hard)
ユーザー gew1fw
提出日時 2025-06-12 20:49:14
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,811 bytes
コンパイル時間 178 ms
コンパイル使用メモリ 81,860 KB
実行使用メモリ 379,676 KB
最終ジャッジ日時 2025-06-12 20:51:05
合計ジャッジ時間 4,949 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4 WA * 1
other AC * 2 WA * 2 TLE * 1 -- * 5
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import defaultdict

def main():
    n, k = map(int, sys.stdin.readline().split())
    a = list(map(str, sys.stdin.readline().split()))
    
    # Create a frequency dictionary for the cards
    count = defaultdict(int)
    for s in a:
        count[s] += 1
    
    # Remove cards that have zero count (though initially all have positive)
    count = {s: cnt for s, cnt in count.items() if cnt > 0}
    
    # Initialize the current list with all possible first steps
    current = []
    for s in count:
        new_count = count.copy()
        new_count[s] -= 1
        if new_count[s] == 0:
            del new_count[s]
        current.append((s, new_count))
    
    # Sort the current list in descending order based on the partial string
    current.sort(key=lambda x: x[0], reverse=True)
    # Keep only the top 1000 to manage computational load
    current = current[:1000]
    
    for _ in range(n - 1):
        next_current = []
        for partial_str, remaining in current:
            # Generate all possible next steps
            for s in remaining:
                new_count = remaining.copy()
                new_count[s] -= 1
                if new_count[s] == 0:
                    del new_count[s]
                new_partial = partial_str + s
                next_current.append((new_partial, new_count))
        
        # Sort the next_current list in descending order
        next_current.sort(key=lambda x: x[0], reverse=True)
        # Keep only the top 1000
        current = next_current[:1000]
    
    # After processing all steps, collect the top K results
    current.sort(key=lambda x: x[0], reverse=True)
    # Output the first K results, each on a new line
    for i in range(k):
        print(current[i][0])

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