結果

問題 No.3 ビットすごろく
ユーザー にゃんにゃん
提出日時 2022-02-13 03:07:30
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 33 ms / 5,000 ms
コード長 506 bytes
コンパイル時間 639 ms
コンパイル使用メモリ 10,996 KB
実行使用メモリ 8,976 KB
最終ジャッジ日時 2023-09-11 15:16:00
合計ジャッジ時間 2,195 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,516 KB
testcase_01 AC 18 ms
8,656 KB
testcase_02 AC 18 ms
8,656 KB
testcase_03 AC 21 ms
8,628 KB
testcase_04 AC 19 ms
8,640 KB
testcase_05 AC 26 ms
8,676 KB
testcase_06 AC 22 ms
8,692 KB
testcase_07 AC 20 ms
8,548 KB
testcase_08 AC 24 ms
8,576 KB
testcase_09 AC 27 ms
8,796 KB
testcase_10 AC 30 ms
8,844 KB
testcase_11 AC 27 ms
8,804 KB
testcase_12 AC 26 ms
8,752 KB
testcase_13 AC 21 ms
8,660 KB
testcase_14 AC 29 ms
8,840 KB
testcase_15 AC 32 ms
8,824 KB
testcase_16 AC 31 ms
8,944 KB
testcase_17 AC 32 ms
8,960 KB
testcase_18 AC 21 ms
8,476 KB
testcase_19 AC 33 ms
8,976 KB
testcase_20 AC 19 ms
8,600 KB
testcase_21 AC 19 ms
8,592 KB
testcase_22 AC 30 ms
8,740 KB
testcase_23 AC 32 ms
8,956 KB
testcase_24 AC 32 ms
8,824 KB
testcase_25 AC 32 ms
8,820 KB
testcase_26 AC 18 ms
8,528 KB
testcase_27 AC 22 ms
8,688 KB
testcase_28 AC 31 ms
8,860 KB
testcase_29 AC 28 ms
8,640 KB
testcase_30 AC 18 ms
8,676 KB
testcase_31 AC 19 ms
8,548 KB
testcase_32 AC 26 ms
8,680 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
def bit(x):
    x = bin(x)
    x = x.count('1')
    return x

def bit_fb(x):
    a = x - bit(x)
    b = x + bit(x)
    return a,b

N = int(input())
A = [0] * (N+1)
A[1] = 1
B = deque()
B.append(1)
while B:
    C = B.popleft()
    if C == N:
        print(A[C])
        break
    D,E = bit_fb(C)
    if 0 < D <= N and A[D] == 0:
        B.append(D)
        A[D] = A[C] + 1
    if 0 < E <= N and A[E] == 0:
        B.append(E)
        A[E] = A[C] + 1
if A[N] == 0:
    print(-1)
0