結果
| 問題 | No.873 バイナリ、ヤバいなり!w | 
| コンテスト | |
| ユーザー |  maspy | 
| 提出日時 | 2020-03-19 15:13:20 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 527 ms / 2,000 ms | 
| コード長 | 1,412 bytes | 
| コンパイル時間 | 238 ms | 
| コンパイル使用メモリ | 82,248 KB | 
| 実行使用メモリ | 76,416 KB | 
| 最終ジャッジ日時 | 2024-06-11 14:05:25 | 
| 合計ジャッジ時間 | 6,086 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 36 | 
ソースコード
#!/usr/bin/ python3.8
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N = int(read())
dp = [0] * (N + 1)
for n in range(1, N + 1):
    x = n
    for i in range(1, N + 1):
        m = n - i * i
        if m < 0:
            break
        y = dp[m] + i
        if x > y:
            x = y
    dp[n] = x
def find_next_num_odd(N):
    for i in range(1, N + 1, 2):
        m = N - i * i
        if m < 0:
            break
        if dp[N] == dp[m] + i:
            return i
    return None
def find_next_num_even(N, reverse=True):
    U = int((N + 1) ** .5)
    if U & 1:
        U += 1
    rng = range(2, U + 1, 2)
    if reverse:
        rng = reversed(rng)
    for i in rng:
        m = N - i * i
        if m < 0:
            continue
        if dp[N] == dp[m] + i:
            return i
nums = []
while N:
    i = find_next_num_odd(N)
    if not i:
        break
    nums.append(i)
    N -= i * i
rev = True
while N:
    i = find_next_num_even(N, rev)
    rev = not rev
    nums.append(i)
    N -= i * i
def to_word(N, next_ch):
    q, r = divmod(N, 2)
    w = '01' if next_ch == 0 else '10'
    if r == 0:
        return w * q
    else:
        return w * q + str(next_ch)
words = []
next_ch = 0
for n in nums:
    words.append(to_word(n, next_ch))
    if not (n & 1):
        next_ch ^= 1
answer = ''.join(words)
print(answer)
            
            
            
        