結果

問題 No.3 ビットすごろく
ユーザー human_cipherhuman_cipher
提出日時 2021-06-15 01:41:42
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 821 bytes
コンパイル時間 228 ms
コンパイル使用メモリ 10,876 KB
実行使用メモリ 11,592 KB
最終ジャッジ日時 2023-08-26 16:07:29
合計ジャッジ時間 2,143 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,472 KB
testcase_01 AC 19 ms
8,652 KB
testcase_02 AC 18 ms
8,636 KB
testcase_03 AC 20 ms
9,260 KB
testcase_04 AC 18 ms
8,656 KB
testcase_05 AC 24 ms
10,196 KB
testcase_06 AC 21 ms
9,424 KB
testcase_07 AC 19 ms
9,088 KB
testcase_08 AC 22 ms
9,820 KB
testcase_09 AC 26 ms
10,820 KB
testcase_10 AC 27 ms
11,144 KB
testcase_11 AC 24 ms
10,508 KB
testcase_12 AC 23 ms
10,244 KB
testcase_13 AC 20 ms
9,168 KB
testcase_14 AC 26 ms
10,984 KB
testcase_15 AC 29 ms
11,416 KB
testcase_16 AC 28 ms
11,532 KB
testcase_17 AC 28 ms
11,544 KB
testcase_18 AC 19 ms
9,000 KB
testcase_19 AC 30 ms
11,544 KB
testcase_20 AC 18 ms
8,716 KB
testcase_21 WA -
testcase_22 AC 27 ms
11,056 KB
testcase_23 AC 29 ms
11,480 KB
testcase_24 AC 29 ms
11,480 KB
testcase_25 AC 30 ms
11,592 KB
testcase_26 WA -
testcase_27 AC 20 ms
9,092 KB
testcase_28 AC 28 ms
11,352 KB
testcase_29 AC 24 ms
10,476 KB
testcase_30 AC 17 ms
8,616 KB
testcase_31 AC 17 ms
8,572 KB
testcase_32 AC 23 ms
10,500 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 = n
    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