結果

問題 No.1743 Permutation Code
ユーザー lam6er
提出日時 2025-03-31 17:57:03
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,049 bytes
コンパイル時間 162 ms
コンパイル使用メモリ 82,780 KB
実行使用メモリ 77,540 KB
最終ジャッジ日時 2025-03-31 17:58:30
合計ジャッジ時間 11,560 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 7 TLE * 1 -- * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import defaultdict

sys.setrecursionlimit(1 << 25)

def main():
    C = input().strip()
    L = len(C)
    
    # Find N using binary search
    left = 1
    right = 1 << 20  # A sufficiently large upper bound
    N = -1
    while left <= right:
        mid = (left + right) // 2
        sum_bit = 0
        k = 1
        while True:
            a = 1 << (k - 1)
            if a > mid:
                break
            b = min((1 << k) - 1, mid)
            count = b - a + 1
            sum_bit += count * k
            if sum_bit > L:
                break
            k += 1
        if sum_bit == L:
            N = mid
            break
        elif sum_bit < L:
            left = mid + 1
        else:
            right = mid - 1
    
    # Precompute binary representations and group by bit length
    precompute = defaultdict(dict)
    for m in range(1, N + 1):
        s = bin(m)[2:]
        b = len(s)
        precompute[b][s] = m
    
    # Prepare used array and result list
    used = [False] * (N + 1)
    result = []
    
    def backtrack(pos):
        if pos == len(C):
            return True
        # Check possible bit lengths in descending order
        possible_bs = []
        for b in list(precompute.keys()):
            if pos + b > len(C):
                continue
            if C[pos] == '0':
                continue
            s_part = C[pos:pos + b]
            if s_part in precompute[b]:
                possible_bs.append(b)
        possible_bs.sort(reverse=True)
        
        for b in possible_bs:
            s_part = C[pos:pos + b]
            num = precompute[b].get(s_part)
            if num is not None and not used[num]:
                used[num] = True
                result.append(num)
                if backtrack(pos + b):
                    return True
                # Backtrack
                used[num] = False
                result.pop()
        return False
    
    backtrack(0)
    print(' '.join(map(str, result)))

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