結果

問題 No.3 ビットすごろく
ユーザー HydruHydru
提出日時 2023-01-15 13:25:38
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 169 ms / 5,000 ms
コード長 571 bytes
コンパイル時間 544 ms
コンパイル使用メモリ 87,040 KB
実行使用メモリ 78,548 KB
最終ジャッジ日時 2023-08-27 21:01:13
合計ジャッジ時間 5,308 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 88 ms
71,544 KB
testcase_01 AC 88 ms
71,860 KB
testcase_02 AC 91 ms
71,540 KB
testcase_03 AC 169 ms
77,684 KB
testcase_04 AC 98 ms
76,504 KB
testcase_05 AC 115 ms
77,804 KB
testcase_06 AC 105 ms
77,872 KB
testcase_07 AC 102 ms
77,840 KB
testcase_08 AC 112 ms
77,760 KB
testcase_09 AC 118 ms
78,468 KB
testcase_10 AC 119 ms
78,548 KB
testcase_11 AC 120 ms
78,424 KB
testcase_12 AC 115 ms
77,952 KB
testcase_13 AC 106 ms
77,752 KB
testcase_14 AC 122 ms
78,380 KB
testcase_15 AC 125 ms
78,536 KB
testcase_16 AC 123 ms
78,540 KB
testcase_17 AC 122 ms
78,248 KB
testcase_18 AC 103 ms
77,716 KB
testcase_19 AC 124 ms
78,428 KB
testcase_20 AC 97 ms
76,568 KB
testcase_21 AC 89 ms
71,832 KB
testcase_22 AC 124 ms
78,420 KB
testcase_23 AC 125 ms
78,444 KB
testcase_24 AC 123 ms
78,508 KB
testcase_25 AC 123 ms
78,420 KB
testcase_26 AC 90 ms
71,540 KB
testcase_27 AC 106 ms
77,816 KB
testcase_28 AC 122 ms
78,380 KB
testcase_29 AC 122 ms
78,352 KB
testcase_30 AC 91 ms
71,704 KB
testcase_31 AC 92 ms
71,460 KB
testcase_32 AC 119 ms
77,896 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

n=int(input())

def bit_cnt(n):
    result=0
    while(n!=0):
        if n%2==1:
            result+=1
        n//=2
    return result

inf=float("inf")

dist=[inf for _ in range(n)]
dist[0]=1

Q=deque()
Q.append(0)

while(Q):
    a=Q.popleft()
    x=bit_cnt(a+1)
    if 0<=a+x<n:
        if dist[a+x]>dist[a]+1:
            dist[a+x]=dist[a]+1
            Q.append(a+x)
    if 0<=a-x<n:
        if dist[a-x]>dist[a]+1:
            dist[a-x]=dist[a]+1
            Q.append(a-x)

ans=dist[n-1]
if ans==inf:
    print(-1)
else:
    print(ans)
0