結果

問題 No.3 ビットすごろく
ユーザー mitsushinomitsushino
提出日時 2017-12-11 16:45:11
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 26 ms / 5,000 ms
コード長 942 bytes
コンパイル時間 210 ms
コンパイル使用メモリ 10,984 KB
実行使用メモリ 8,968 KB
最終ジャッジ日時 2023-09-14 00:53:11
合計ジャッジ時間 2,079 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,508 KB
testcase_01 AC 18 ms
8,492 KB
testcase_02 AC 19 ms
8,636 KB
testcase_03 AC 23 ms
8,612 KB
testcase_04 AC 21 ms
8,636 KB
testcase_05 AC 24 ms
8,804 KB
testcase_06 AC 21 ms
8,592 KB
testcase_07 AC 20 ms
8,692 KB
testcase_08 AC 21 ms
8,632 KB
testcase_09 AC 24 ms
8,724 KB
testcase_10 AC 24 ms
8,852 KB
testcase_11 AC 23 ms
8,884 KB
testcase_12 AC 22 ms
8,732 KB
testcase_13 AC 20 ms
8,496 KB
testcase_14 AC 24 ms
8,852 KB
testcase_15 AC 25 ms
8,860 KB
testcase_16 AC 25 ms
8,824 KB
testcase_17 AC 25 ms
8,932 KB
testcase_18 AC 20 ms
8,560 KB
testcase_19 AC 25 ms
8,808 KB
testcase_20 AC 19 ms
8,540 KB
testcase_21 AC 18 ms
8,676 KB
testcase_22 AC 24 ms
8,876 KB
testcase_23 AC 26 ms
8,916 KB
testcase_24 AC 26 ms
8,812 KB
testcase_25 AC 26 ms
8,968 KB
testcase_26 AC 18 ms
8,548 KB
testcase_27 AC 20 ms
8,664 KB
testcase_28 AC 25 ms
8,832 KB
testcase_29 AC 24 ms
8,748 KB
testcase_30 AC 19 ms
8,688 KB
testcase_31 AC 19 ms
8,644 KB
testcase_32 AC 22 ms
8,768 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# -*- coding: utf-8 -*-
# 参考URL
# http://blog.livedoor.jp/yoshichi9-solve/archives/16290354.html
from collections import deque


def make_one_count_of_binary_digit(N):
    l = []
    for i in range(0, N + 1):
        l.append(bin(i).count('1'))
    return l


def bfs(N, binary_digit_one_list):
    d = [0 for _ in range(N + 1)]
    d[1] = 1
    q = deque()
    q.append(1)
    while q:
        idx = q.popleft()
        if idx == N:
            return d[idx]
        right = idx + binary_digit_one_list[idx]
        left = idx - binary_digit_one_list[idx]
        if (0 < right <= N) and d[right] == 0:
            q.append(right)
            d[right] = d[idx] + 1
        if (0 < left <= N) and d[left] == 0:
            q.append(left)
            d[left] = d[idx] + 1
    return -1


if __name__ == '__main__':
    N = int(input())
    binary_digit_one_list = make_one_count_of_binary_digit(N)
    print(bfs(N, binary_digit_one_list))
0