結果

問題 No.775 tatyamと素数大富豪(hard)
ユーザー gew1fw
提出日時 2025-06-12 15:44:11
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,257 bytes
コンパイル時間 295 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 74,776 KB
最終ジャッジ日時 2025-06-12 15:44:13
合計ジャッジ時間 1,878 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4 WA * 1
other AC * 8 WA * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from itertools import permutations
from functools import cmp_to_key

def main():
    N, K = map(int, sys.stdin.readline().split())
    a = list(map(str, sys.stdin.readline().split()))
    
    # Custom comparator to sort the array
    def compare(x, y):
        if x + y > y + x:
            return -1
        else:
            return 1
    
    a_sorted = sorted(a, key=cmp_to_key(compare))
    
    # Function to determine the minimal M where M! >= K
    def find_m(k):
        m = 1
        fact = 1
        while fact < k:
            m += 1
            fact *= m
        return m
    
    M = find_m(K)
    if M > N:
        M = N
    
    # Split the sorted array into fixed and last M elements
    fixed = a_sorted[:N - M]
    last_m = a_sorted[N - M:]
    
    # Generate all unique permutations of last_m
    perms = set(permutations(last_m))
    
    # Collect all possible concatenated strings
    numbers = set()
    for perm in perms:
        s = ''.join(fixed + list(perm))
        numbers.add(s)
    
    # Convert to a sorted list in descending order
    sorted_numbers = sorted(numbers, reverse=True)
    
    # Output the top K numbers
    for num in sorted_numbers[:K]:
        print(num)

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