結果

問題 No.3 ビットすごろく
ユーザー sue_charosue_charo
提出日時 2017-04-12 16:40:21
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 28 ms / 5,000 ms
コード長 667 bytes
コンパイル時間 97 ms
コンパイル使用メモリ 10,788 KB
実行使用メモリ 8,940 KB
最終ジャッジ日時 2023-09-14 00:39:20
合計ジャッジ時間 2,205 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
8,624 KB
testcase_01 AC 22 ms
8,568 KB
testcase_02 AC 19 ms
8,572 KB
testcase_03 AC 21 ms
8,616 KB
testcase_04 AC 19 ms
8,480 KB
testcase_05 AC 24 ms
8,596 KB
testcase_06 AC 20 ms
8,584 KB
testcase_07 AC 20 ms
8,488 KB
testcase_08 AC 22 ms
8,752 KB
testcase_09 AC 24 ms
8,676 KB
testcase_10 AC 26 ms
8,760 KB
testcase_11 AC 25 ms
8,648 KB
testcase_12 AC 23 ms
8,716 KB
testcase_13 AC 20 ms
8,580 KB
testcase_14 AC 26 ms
8,836 KB
testcase_15 AC 28 ms
8,808 KB
testcase_16 AC 27 ms
8,940 KB
testcase_17 AC 27 ms
8,852 KB
testcase_18 AC 20 ms
8,668 KB
testcase_19 AC 27 ms
8,872 KB
testcase_20 AC 19 ms
8,512 KB
testcase_21 AC 18 ms
8,604 KB
testcase_22 AC 26 ms
8,708 KB
testcase_23 AC 28 ms
8,812 KB
testcase_24 AC 28 ms
8,812 KB
testcase_25 AC 27 ms
8,844 KB
testcase_26 AC 19 ms
8,456 KB
testcase_27 AC 20 ms
8,576 KB
testcase_28 AC 27 ms
8,736 KB
testcase_29 AC 24 ms
8,832 KB
testcase_30 AC 19 ms
8,552 KB
testcase_31 AC 19 ms
8,504 KB
testcase_32 AC 24 ms
8,716 KB
権限があれば一括ダウンロードができます

ソースコード

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