結果

問題 No.3 ビットすごろく
ユーザー 141sksk141sksk
提出日時 2019-06-25 13:39:59
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 23 ms / 5,000 ms
コード長 640 bytes
コンパイル時間 102 ms
コンパイル使用メモリ 10,920 KB
実行使用メモリ 8,532 KB
最終ジャッジ日時 2023-09-14 01:20:41
合計ジャッジ時間 2,068 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
7,916 KB
testcase_01 AC 16 ms
7,952 KB
testcase_02 AC 16 ms
7,872 KB
testcase_03 AC 18 ms
7,856 KB
testcase_04 AC 17 ms
7,916 KB
testcase_05 AC 19 ms
8,184 KB
testcase_06 AC 18 ms
7,920 KB
testcase_07 AC 17 ms
7,752 KB
testcase_08 AC 18 ms
8,364 KB
testcase_09 AC 21 ms
8,376 KB
testcase_10 AC 22 ms
8,496 KB
testcase_11 AC 20 ms
8,188 KB
testcase_12 AC 19 ms
8,296 KB
testcase_13 AC 18 ms
7,860 KB
testcase_14 AC 21 ms
8,508 KB
testcase_15 AC 23 ms
8,240 KB
testcase_16 AC 22 ms
8,488 KB
testcase_17 AC 23 ms
8,532 KB
testcase_18 AC 18 ms
7,888 KB
testcase_19 AC 22 ms
8,512 KB
testcase_20 AC 17 ms
7,856 KB
testcase_21 AC 16 ms
7,800 KB
testcase_22 AC 22 ms
8,352 KB
testcase_23 AC 23 ms
8,496 KB
testcase_24 AC 23 ms
8,504 KB
testcase_25 AC 22 ms
8,476 KB
testcase_26 AC 16 ms
7,880 KB
testcase_27 AC 18 ms
7,868 KB
testcase_28 AC 22 ms
8,344 KB
testcase_29 AC 20 ms
8,320 KB
testcase_30 AC 16 ms
7,860 KB
testcase_31 AC 16 ms
7,872 KB
testcase_32 AC 20 ms
8,188 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():

    goal = int(input())

    def bin1(n):
        return bin(n).count("1")

    Q = [0] * (goal + 1)
    q = [1]
    Q[1] = 1
 
    while q:
        position = q.pop(0)
        stepLength = bin1(position)
        stepCount = Q[position]
        go = position + stepLength
        back = position - stepLength
        if go < goal + 1 and Q[go] == 0:
            Q[go] = stepCount + 1
            q.append(go)
        if back > 0 and Q[back] == 0:
            Q[back] = stepCount + 1
            q.append(back)

    

    if Q[goal] != 0:
        print(Q[goal])
    else:
        print(-1)

if __name__ == "__main__":
    main()
0