結果

問題 No.1633 Sorting Integers (Multiple of 2^K)
ユーザー lam6er
提出日時 2025-04-09 21:02:54
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,831 bytes
コンパイル時間 322 ms
コンパイル使用メモリ 82,284 KB
実行使用メモリ 154,164 KB
最終ジャッジ日時 2025-04-09 21:04:54
合計ジャッジ時間 5,035 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20 TLE * 1 -- * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

import math

def solve():
    N = int(input())
    c = list(map(int, input().split()))
    
    # Create a dictionary to check the counts of each digit.
    input_counts = {str(d+1): c[d] for d in range(9)}
    
    # Check if all digits are odd (leading to exponent 0)
    all_odd = True
    for d in range(1, 10):
        if (d % 2 == 0) and c[d-1] > 0:
            all_odd = False
            break
    if all_odd:
        print(0)
        return
    
    # Check exact power of 2
    log2 = math.log10(2)
    lower_e = max(0, math.ceil((N-1) * log2))
    upper_e = math.floor(N * log2)
    max_e = -1
    for e in range(lower_e, upper_e + 1):
        num = 1 << e
        s = str(num)
        if len(s) != N:
            continue
        temp_counts = {}
        for ch in s:
            temp_counts[ch] = temp_counts.get(ch, 0) + 1
        valid = True
        for d in range(1, 10):
            key = str(d)
            if temp_counts.get(key, 0) != input_counts.get(key, 0):
                valid = False
                break
        if valid:
            max_e = e
            break  # Found the maximum possible e
    
    if max_e != -1:
        print(max_e)
        return
    
    # Fallback to permutations (only feasible for very small N)
    from itertools import permutations
    digits = []
    for d in range(9):
        digits.extend([str(d+1)] * c[d])
    max_k = 0
    seen = set()
    for perm in permutations(digits):
        num_str = ''.join(perm)
        if num_str in seen:
            continue
        seen.add(num_str)
        if perm[-1] in {'1', '3', '5', '7', '9'}:
            continue
        num = int(num_str)
        current_k = 0
        while num % 2 == 0:
            current_k += 1
            num //= 2
        if current_k > max_k:
            max_k = current_k
    print(max_k)

solve()
0