結果

問題 No.873 バイナリ、ヤバいなり!w
ユーザー maspymaspy
提出日時 2020-03-19 15:13:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 598 ms / 2,000 ms
コード長 1,412 bytes
コンパイル時間 295 ms
コンパイル使用メモリ 87,272 KB
実行使用メモリ 79,528 KB
最終ジャッジ日時 2023-09-02 07:13:08
合計ジャッジ時間 8,093 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 307 ms
78,004 KB
testcase_01 AC 193 ms
77,500 KB
testcase_02 AC 578 ms
79,528 KB
testcase_03 AC 481 ms
78,888 KB
testcase_04 AC 73 ms
71,680 KB
testcase_05 AC 74 ms
71,192 KB
testcase_06 AC 72 ms
71,932 KB
testcase_07 AC 72 ms
71,760 KB
testcase_08 AC 181 ms
77,184 KB
testcase_09 AC 72 ms
71,012 KB
testcase_10 AC 75 ms
71,272 KB
testcase_11 AC 72 ms
71,768 KB
testcase_12 AC 105 ms
77,128 KB
testcase_13 AC 70 ms
71,620 KB
testcase_14 AC 73 ms
71,756 KB
testcase_15 AC 70 ms
71,584 KB
testcase_16 AC 71 ms
71,184 KB
testcase_17 AC 72 ms
71,312 KB
testcase_18 AC 71 ms
71,204 KB
testcase_19 AC 71 ms
71,452 KB
testcase_20 AC 70 ms
71,756 KB
testcase_21 AC 69 ms
71,892 KB
testcase_22 AC 72 ms
71,532 KB
testcase_23 AC 72 ms
71,520 KB
testcase_24 AC 70 ms
71,712 KB
testcase_25 AC 72 ms
71,868 KB
testcase_26 AC 74 ms
71,872 KB
testcase_27 AC 73 ms
71,584 KB
testcase_28 AC 69 ms
71,600 KB
testcase_29 AC 68 ms
71,756 KB
testcase_30 AC 72 ms
71,824 KB
testcase_31 AC 69 ms
71,676 KB
testcase_32 AC 88 ms
76,904 KB
testcase_33 AC 79 ms
76,752 KB
testcase_34 AC 139 ms
77,324 KB
testcase_35 AC 598 ms
79,516 KB
testcase_36 AC 337 ms
78,092 KB
testcase_37 AC 594 ms
79,336 KB
testcase_38 AC 593 ms
79,404 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/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)
0