結果

問題 No.3 ビットすごろく
ユーザー dydy
提出日時 2020-10-11 01:42:49
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,139 bytes
コンパイル時間 83 ms
コンパイル使用メモリ 10,980 KB
実行使用メモリ 16,068 KB
最終ジャッジ日時 2023-09-27 22:45:01
合計ジャッジ時間 12,536 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,536 KB
testcase_01 AC 18 ms
8,580 KB
testcase_02 AC 18 ms
8,580 KB
testcase_03 AC 64 ms
8,580 KB
testcase_04 WA -
testcase_05 AC 658 ms
9,012 KB
testcase_06 AC 70 ms
8,676 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 TLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque


def popcount(x):
    '''xの立っているビット数をカウントする関数
    (xは64bit整数)'''

    # 2bitごとの組に分け、立っているビット数を2bitで表現する
    x = x - ((x >> 1) & 0x5555555555555555)

    # 4bit整数に 上位2bit + 下位2bit を計算した値を入れる
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)

    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f  # 8bitごと
    x = x + (x >> 8)  # 16bitごと
    x = x + (x >> 16)  # 32bitごと
    x = x + (x >> 32)  # 64bitごと = 全部の合計
    return x & 0x0000007f


n = int(input())

steps = [None, 1]
visited = [None, False]
for _ in range(2, n+1):
    steps.append(-1)
    visited.append(False)

q = deque([])
start = 1
q.append(start)
while q:
    v = q.popleft()
    visited[v] = True
    if v == n:
        # print("goal")
        break
    num = popcount(v)
    for next_v in [v + num, v - num]:
        if 1 <= next_v and next_v <= n:
            if visited[next_v] == False:
                q.append(next_v)
                steps[next_v] = steps[v] + 1

print(steps[n])
0