結果

問題 No.3 ビットすごろく
ユーザー dydy
提出日時 2020-10-11 01:45:07
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,274 bytes
コンパイル時間 682 ms
コンパイル使用メモリ 10,732 KB
実行使用メモリ 19,496 KB
最終ジャッジ日時 2023-09-27 22:45:15
合計ジャッジ時間 13,881 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
19,496 KB
testcase_01 AC 16 ms
8,608 KB
testcase_02 AC 16 ms
8,704 KB
testcase_03 AC 67 ms
8,684 KB
testcase_04 AC 27 ms
8,632 KB
testcase_05 AC 780 ms
9,108 KB
testcase_06 AC 71 ms
8,600 KB
testcase_07 AC 45 ms
8,656 KB
testcase_08 AC 276 ms
8,976 KB
testcase_09 AC 4,727 ms
10,516 KB
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)
                if steps[next_v] == -1:
                    steps[next_v] = steps[v] + 1
                else:
                    steps[next_v] = min(steps[next_v], steps[v] + 1)

print(steps[n])
0