結果

問題 No.3 ビットすごろく
ユーザー sue_charosue_charo
提出日時 2017-04-12 16:38:55
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 680 bytes
コンパイル時間 372 ms
コンパイル使用メモリ 10,876 KB
実行使用メモリ 8,944 KB
最終ジャッジ日時 2023-09-25 13:06:11
合計ジャッジ時間 2,330 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 19 ms
8,548 KB
testcase_03 AC 20 ms
8,548 KB
testcase_04 WA -
testcase_05 AC 23 ms
8,624 KB
testcase_06 AC 21 ms
8,592 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 23 ms
8,656 KB
testcase_13 AC 20 ms
8,456 KB
testcase_14 AC 25 ms
8,692 KB
testcase_15 AC 28 ms
8,936 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 25 ms
8,876 KB
testcase_23 AC 27 ms
8,708 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 18 ms
8,548 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

# coding: utf-8
from collections import deque

N = int(input())

def solve():
    count = [0] * (N + 1)
    count[1] = 1
    q = deque()
    q.append(1)
    while q:
        num = q.popleft()
        if num == N:
            return count[num]

        bit1_count = "{:b}".format(num).count("1")
        num_front = num + bit1_count
        num_back = num - bit1_count

        if 0 < num_front < N and count[num_front] == 0:
            q.append(num_front)
            count[num_front] = count[num] + 1
        
        if 0 < num_back < N and count[num_back] == 0:
            q.append(num_back)
            count[num_back] = count[num] + 1
        
    return -1

print(solve())
0