結果

問題 No.3 ビットすごろく
ユーザー dydy
提出日時 2020-10-11 01:47:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,444 ms / 5,000 ms
コード長 1,274 bytes
コンパイル時間 360 ms
コンパイル使用メモリ 86,992 KB
実行使用メモリ 271,472 KB
最終ジャッジ日時 2023-09-14 01:53:03
合計ジャッジ時間 15,506 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 89 ms
71,592 KB
testcase_01 AC 89 ms
71,428 KB
testcase_02 AC 92 ms
71,472 KB
testcase_03 AC 114 ms
77,504 KB
testcase_04 AC 106 ms
77,512 KB
testcase_05 AC 138 ms
79,592 KB
testcase_06 AC 112 ms
77,424 KB
testcase_07 AC 110 ms
77,484 KB
testcase_08 AC 122 ms
77,796 KB
testcase_09 AC 287 ms
102,072 KB
testcase_10 AC 603 ms
155,828 KB
testcase_11 AC 218 ms
92,152 KB
testcase_12 AC 132 ms
79,364 KB
testcase_13 AC 113 ms
77,704 KB
testcase_14 AC 514 ms
141,296 KB
testcase_15 AC 1,253 ms
271,404 KB
testcase_16 AC 727 ms
174,756 KB
testcase_17 AC 1,107 ms
255,748 KB
testcase_18 AC 110 ms
77,724 KB
testcase_19 AC 1,421 ms
271,472 KB
testcase_20 AC 105 ms
77,420 KB
testcase_21 AC 91 ms
71,764 KB
testcase_22 AC 544 ms
143,936 KB
testcase_23 AC 1,444 ms
271,356 KB
testcase_24 AC 1,427 ms
271,248 KB
testcase_25 AC 1,264 ms
271,300 KB
testcase_26 AC 92 ms
71,672 KB
testcase_27 AC 111 ms
77,864 KB
testcase_28 AC 690 ms
170,200 KB
testcase_29 AC 224 ms
92,324 KB
testcase_30 AC 96 ms
71,628 KB
testcase_31 AC 95 ms
71,760 KB
testcase_32 AC 178 ms
84,652 KB
権限があれば一括ダウンロードができます

ソースコード

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