結果

問題 No.3 ビットすごろく
ユーザー tsukasatsukasa
提出日時 2019-08-02 14:07:48
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 46 ms / 5,000 ms
コード長 890 bytes
コンパイル時間 97 ms
コンパイル使用メモリ 10,916 KB
実行使用メモリ 8,608 KB
最終ジャッジ日時 2023-09-14 01:24:27
合計ジャッジ時間 2,427 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
7,884 KB
testcase_01 AC 16 ms
7,860 KB
testcase_02 AC 16 ms
7,912 KB
testcase_03 AC 21 ms
8,308 KB
testcase_04 AC 18 ms
7,820 KB
testcase_05 AC 29 ms
8,164 KB
testcase_06 AC 22 ms
8,352 KB
testcase_07 AC 19 ms
7,832 KB
testcase_08 AC 26 ms
8,360 KB
testcase_09 AC 36 ms
8,324 KB
testcase_10 AC 40 ms
8,500 KB
testcase_11 AC 33 ms
8,292 KB
testcase_12 AC 30 ms
8,156 KB
testcase_13 AC 20 ms
8,200 KB
testcase_14 AC 39 ms
8,576 KB
testcase_15 AC 45 ms
8,480 KB
testcase_16 AC 43 ms
8,468 KB
testcase_17 AC 45 ms
8,344 KB
testcase_18 AC 19 ms
8,316 KB
testcase_19 AC 46 ms
8,456 KB
testcase_20 AC 17 ms
7,884 KB
testcase_21 AC 16 ms
7,912 KB
testcase_22 AC 40 ms
8,608 KB
testcase_23 AC 46 ms
8,368 KB
testcase_24 AC 46 ms
8,284 KB
testcase_25 AC 46 ms
8,460 KB
testcase_26 AC 16 ms
7,756 KB
testcase_27 AC 20 ms
8,188 KB
testcase_28 AC 42 ms
8,468 KB
testcase_29 AC 34 ms
8,268 KB
testcase_30 AC 16 ms
7,768 KB
testcase_31 AC 17 ms
7,824 KB
testcase_32 AC 31 ms
8,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    ret = solve(int(input()))
    print(ret)


def solve(n: int) -> int:
    impossible = 10 ** 7
    steps = [step(i) for i in range(n + 1)]
    memo = [impossible for _ in range(n + 1)]
    stack = [1]
    memo[1] = 1
    while stack:
        cur = stack.pop()
        back = cur - steps[cur]
        front = cur + steps[cur]
        if back < 1:
            pass
        elif memo[back] > memo[cur] + 1:
            memo[back] = memo[cur] + 1
            stack.append(back)

        if front > n:
            pass
        elif memo[front] > memo[cur] + 1:
            memo[front] = memo[cur] + 1
            stack.append(front)

    if memo[n] == impossible:
        return -1
    else:
        return memo[n]


def step(n):
    count = 0
    while n > 0:
        if n % 2 != 0:
            count += 1
        n //= 2
    return count


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