結果

問題 No.3 ビットすごろく
ユーザー ynishimuraynishimura
提出日時 2020-10-08 19:29:43
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 27 ms / 5,000 ms
コード長 2,595 bytes
コンパイル時間 92 ms
コンパイル使用メモリ 10,984 KB
実行使用メモリ 9,524 KB
最終ジャッジ日時 2023-09-14 01:52:10
合計ジャッジ時間 2,171 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
9,132 KB
testcase_01 AC 20 ms
9,096 KB
testcase_02 AC 20 ms
9,128 KB
testcase_03 AC 22 ms
9,188 KB
testcase_04 AC 20 ms
9,220 KB
testcase_05 AC 24 ms
9,136 KB
testcase_06 AC 22 ms
9,152 KB
testcase_07 AC 21 ms
9,180 KB
testcase_08 AC 23 ms
9,212 KB
testcase_09 AC 25 ms
9,476 KB
testcase_10 AC 25 ms
9,412 KB
testcase_11 AC 25 ms
9,476 KB
testcase_12 AC 23 ms
9,152 KB
testcase_13 AC 21 ms
9,064 KB
testcase_14 AC 25 ms
9,388 KB
testcase_15 AC 27 ms
9,392 KB
testcase_16 AC 27 ms
9,380 KB
testcase_17 AC 27 ms
9,420 KB
testcase_18 AC 21 ms
9,252 KB
testcase_19 AC 27 ms
9,396 KB
testcase_20 AC 20 ms
9,252 KB
testcase_21 AC 19 ms
9,196 KB
testcase_22 AC 25 ms
9,420 KB
testcase_23 AC 27 ms
9,396 KB
testcase_24 AC 27 ms
9,416 KB
testcase_25 AC 27 ms
9,524 KB
testcase_26 AC 20 ms
9,252 KB
testcase_27 AC 22 ms
9,140 KB
testcase_28 AC 27 ms
9,324 KB
testcase_29 AC 25 ms
9,456 KB
testcase_30 AC 19 ms
9,132 KB
testcase_31 AC 20 ms
9,120 KB
testcase_32 AC 24 ms
9,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
N = int(input())


def movable_distance(num):
    "2進数で表現した時の1のビット数 => 前後に移動できる距離"
    # print(f'num:{num}, b:{bin(num)}')
    # 2進数表示した場合の1の数を数える
    return bin(num).count("1")


def bit():
    que = deque()
    que.append(1)
    # マスの数を表す整数N(1<N<10000)が与えられます。
    trout = [-1] * (10**5)

    # 1から開始のマス
    trout[1] = 1

    while (len(que) > 0):
        # print(f'#### {que}')
        now = que.popleft()  #現在のマス目

        # debug
        # for i in range(N):
        #     if (i + 1 == now):
        #         print('*', end='')  #現在のマス目
        #     elif (now == N):
        #         print(f'gaol => {trout[now]}', end='')  #終了
        #         break
        #     elif (trout[i + 1] != -1):
        #         print('+', end='')  #探索済マス
        #     else:
        #         print('-', end='')  #未探索マス
        # print('|')

        if now == N:
            break
        movecnt = movable_distance(now)  #現在のマス目を2進数に変換した時の1の数
        # print(f'now {now}, movecnt {movecnt}')

        if (trout[now - movecnt] == -1
                and now - movecnt > 0):  #後ろのマスに移動するときの判定・処理
            trout[now - movecnt] = trout[now] + 1  #移動した先に移動数の最小値を記録(+1回しておく)
            que.append(now - movecnt)  #このマス目から次は探索を行うためキューに入れる
            # print(
            #     f'マス{now - movecnt}への移動数の最小値を記録: {trout[now - movecnt]}, 探索を行うための位置をappend: {now - movecnt}'
            # )

        if (trout[now + movecnt] == -1
                and now + movecnt <= N):  #前のマスに移動する時の判定・処理
            trout[now + movecnt] = trout[now] + 1  #移動した先に移動数の最小値を記録
            que.append(now + movecnt)  #このマス目から次は探索を行うためキューに入れる
            # print(
            #     f'マス{now + movecnt}への移動数の最小値を記録: {trout[now + movecnt]}, 探索を行うための位置をappend: {now + movecnt}'
            # )

    print(trout[N])
    # for i in range(N):
    #     if trout[i + 1] == -1:
    #         print('-|', end='')  #未探索
    #     else:
    #         print(f'{trout[i + 1]}|', end='')  #最小値移動数
    # print()


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