結果

問題 No.1743 Permutation Code
ユーザー gew1fw
提出日時 2025-06-12 15:41:47
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,384 bytes
コンパイル時間 224 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 76,544 KB
最終ジャッジ日時 2025-06-12 15:42:00
合計ジャッジ時間 10,595 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 7 TLE * 1 -- * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from sys import stdin

def compute_n(length):
    n = 0
    total_bits = 0
    while True:
        n += 1
        bits = n.bit_length()
        total_bits += bits
        if total_bits == length:
            return n
        if total_bits > length:
            return -1

def backtrack(c, binaries, n, used, path, result, pos):
    if pos == len(c):
        result.extend(path)
        return True
    max_len = 0
    for num in binaries:
        l = len(num)
        if l > max_len and l <= len(c) - pos:
            max_len = l
    for l in range(max_len, 0, -1):
        if pos + l > len(c):
            continue
        current = c[pos:pos+l]
        if current not in binaries:
            continue
        num = int(current, 2)
        if num < 1 or num > n:
            continue
        if not used[num]:
            used[num] = True
            if backtrack(c, binaries, n, used, path + [num], result, pos + l):
                return True
            used[num] = False
    return False

def solve():
    c = stdin.read().strip()
    length = len(c)
    n = compute_n(length)
    if n == -1:
        print("Invalid input")
        return
    binaries = {bin(i)[2:]: i for i in range(1, n+1)}
    used = [False] * (n + 2)
    result = []
    backtrack(c, binaries, n, used, [], result, 0)
    print(' '.join(map(str, result)))

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