結果

問題 No.3 ビットすごろく
ユーザー phantomilephantomile
提出日時 2015-12-19 07:49:12
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 33 ms / 5,000 ms
コード長 1,126 bytes
コンパイル時間 202 ms
コンパイル使用メモリ 10,824 KB
実行使用メモリ 9,560 KB
最終ジャッジ日時 2023-09-13 23:33:07
合計ジャッジ時間 2,492 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
8,740 KB
testcase_01 AC 21 ms
8,816 KB
testcase_02 AC 21 ms
8,812 KB
testcase_03 AC 24 ms
9,000 KB
testcase_04 AC 22 ms
8,964 KB
testcase_05 AC 27 ms
9,168 KB
testcase_06 AC 24 ms
9,064 KB
testcase_07 AC 22 ms
8,908 KB
testcase_08 AC 26 ms
9,164 KB
testcase_09 AC 29 ms
9,236 KB
testcase_10 AC 30 ms
9,436 KB
testcase_11 AC 28 ms
9,292 KB
testcase_12 AC 26 ms
9,112 KB
testcase_13 AC 23 ms
9,040 KB
testcase_14 AC 30 ms
9,372 KB
testcase_15 AC 31 ms
9,376 KB
testcase_16 AC 32 ms
9,364 KB
testcase_17 AC 32 ms
9,560 KB
testcase_18 AC 23 ms
8,940 KB
testcase_19 AC 32 ms
9,484 KB
testcase_20 AC 22 ms
8,816 KB
testcase_21 AC 21 ms
8,768 KB
testcase_22 AC 30 ms
9,440 KB
testcase_23 AC 32 ms
9,560 KB
testcase_24 AC 32 ms
9,448 KB
testcase_25 AC 32 ms
9,368 KB
testcase_26 AC 21 ms
8,876 KB
testcase_27 AC 24 ms
8,944 KB
testcase_28 AC 33 ms
9,440 KB
testcase_29 AC 29 ms
9,116 KB
testcase_30 AC 21 ms
8,748 KB
testcase_31 AC 21 ms
8,888 KB
testcase_32 AC 28 ms
9,120 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

"""
Yukicoder No.3 ビットすごろく

author: yamaton
date: 2015-12-18
"""

import itertools as it
import functools
import operator
import collections
import math
import sys


def bitcount(n):
    res = 0
    while n > 0:
        res += n & 1
        n >>= 1
    return res


def solve(n):
    if n == 1:
        return 1
    q = collections.deque()
    q.append(1)
    path_to = dict()
    path_to[1] = None

    while q:
        curr = q.popleft()
        delta = bitcount(curr)
        for next_ in (curr + delta, curr - delta):
            if next_ not in path_to and 1 <= next_ <= n:
                q.append(next_)
                path_to[next_] = curr
            if next_ == n:
                # pp(path_to)
                return steps_from_start(path_to, n)
    else:
        return -1

def steps_from_start(path_to, n, start=1):
    cnt = 1
    while n != start:
        n = path_to[n]
        cnt += 1
    return cnt


def pp(*args, **kwargs):
    return print(*args, file=sys.stderr, **kwargs)


def main():
    n = int(input())
    result = solve(n)
    print(result)


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