結果

問題 No.3 ビットすごろく
ユーザー human_cipherhuman_cipher
提出日時 2021-06-15 01:43:01
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 31 ms / 5,000 ms
コード長 825 bytes
コンパイル時間 710 ms
コンパイル使用メモリ 10,792 KB
実行使用メモリ 11,608 KB
最終ジャッジ日時 2023-08-26 16:09:26
合計ジャッジ時間 2,230 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,536 KB
testcase_01 AC 16 ms
8,492 KB
testcase_02 AC 18 ms
8,544 KB
testcase_03 AC 21 ms
9,260 KB
testcase_04 AC 18 ms
8,660 KB
testcase_05 AC 24 ms
10,264 KB
testcase_06 AC 20 ms
9,304 KB
testcase_07 AC 19 ms
8,904 KB
testcase_08 AC 22 ms
9,900 KB
testcase_09 AC 24 ms
10,792 KB
testcase_10 AC 25 ms
11,124 KB
testcase_11 AC 24 ms
10,560 KB
testcase_12 AC 21 ms
10,132 KB
testcase_13 AC 18 ms
9,100 KB
testcase_14 AC 25 ms
11,044 KB
testcase_15 AC 31 ms
11,424 KB
testcase_16 AC 26 ms
11,508 KB
testcase_17 AC 27 ms
11,608 KB
testcase_18 AC 19 ms
9,004 KB
testcase_19 AC 28 ms
11,432 KB
testcase_20 AC 16 ms
8,668 KB
testcase_21 AC 16 ms
8,624 KB
testcase_22 AC 25 ms
11,128 KB
testcase_23 AC 27 ms
11,604 KB
testcase_24 AC 28 ms
11,584 KB
testcase_25 AC 27 ms
11,424 KB
testcase_26 AC 16 ms
8,564 KB
testcase_27 AC 18 ms
9,272 KB
testcase_28 AC 27 ms
11,364 KB
testcase_29 AC 22 ms
10,488 KB
testcase_30 AC 16 ms
8,512 KB
testcase_31 AC 16 ms
8,692 KB
testcase_32 AC 22 ms
10,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import Counter
from collections import deque

def hw(s):
    return bin(s).count("1")

def solve(n):
    G = [set() for _ in range(n+1)] #G[i]:i番目のマスからいけるマス
    for i in range(1,n+1):
        hwi = hw(i)
        if 1 <= i - hwi <= n:
            G[i].add(i-hwi)
        if 1 <= i + hwi <= n:
            G[i].add(i+hwi)

    INF = 10**8
    D = [INF for _ in range(n+1)] #D[i]:マス1からの最短距離
    DQ = deque()
    DQ.append(1)
    D[1] = 1
    while len(DQ) > 0:
        now = DQ.popleft()
        for nx in G[now]:
            if D[nx] == INF:
                D[nx] = D[now] + 1
                DQ.append(nx)
    if D[n] != INF:
        return D[n]
    else:
        return -1

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

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