結果

問題 No.3 ビットすごろく
ユーザー edashedash
提出日時 2018-08-19 20:48:32
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 25 ms / 5,000 ms
コード長 526 bytes
コンパイル時間 718 ms
コンパイル使用メモリ 10,724 KB
実行使用メモリ 8,532 KB
最終ジャッジ日時 2023-09-14 01:05:46
合計ジャッジ時間 2,192 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,864 KB
testcase_01 AC 17 ms
7,956 KB
testcase_02 AC 16 ms
7,952 KB
testcase_03 AC 18 ms
7,904 KB
testcase_04 AC 16 ms
7,892 KB
testcase_05 AC 21 ms
8,344 KB
testcase_06 AC 18 ms
7,852 KB
testcase_07 AC 18 ms
7,784 KB
testcase_08 AC 20 ms
7,812 KB
testcase_09 AC 22 ms
8,232 KB
testcase_10 AC 23 ms
8,248 KB
testcase_11 AC 23 ms
8,252 KB
testcase_12 AC 21 ms
8,240 KB
testcase_13 AC 18 ms
7,780 KB
testcase_14 AC 23 ms
8,304 KB
testcase_15 AC 25 ms
8,404 KB
testcase_16 AC 24 ms
8,400 KB
testcase_17 AC 25 ms
8,396 KB
testcase_18 AC 18 ms
7,868 KB
testcase_19 AC 25 ms
8,392 KB
testcase_20 AC 16 ms
7,856 KB
testcase_21 AC 17 ms
7,868 KB
testcase_22 AC 23 ms
8,368 KB
testcase_23 AC 25 ms
8,532 KB
testcase_24 AC 25 ms
8,496 KB
testcase_25 AC 24 ms
8,504 KB
testcase_26 AC 16 ms
7,784 KB
testcase_27 AC 18 ms
7,812 KB
testcase_28 AC 24 ms
8,396 KB
testcase_29 AC 23 ms
8,248 KB
testcase_30 AC 17 ms
7,816 KB
testcase_31 AC 17 ms
7,848 KB
testcase_32 AC 21 ms
8,396 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n=int(input())

def f():
    lst = [0]*(n+1)
    lst[1] = 1
    cnt = [1]        
    while(cnt):
        num = cnt.pop(0)
        if num == n:
            return lst[num]

        step = str(bin(num))[2:].count("1")
        fowd_n = num + step
        back_n = num - step
    
        if fowd_n <= n and lst[fowd_n]==0:
            cnt.append(fowd_n)
            lst[fowd_n] =lst[num] + 1
        if back_n > 0 and lst[back_n]==0:
            cnt.append(back_n)
            lst[back_n] =lst[num] + 1
    return -1

print(f())
0