結果

問題 No.3 ビットすごろく
ユーザー HydruHydru
提出日時 2023-01-15 13:54:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 114 ms / 5,000 ms
コード長 961 bytes
コンパイル時間 1,508 ms
コンパイル使用メモリ 86,760 KB
実行使用メモリ 77,928 KB
最終ジャッジ日時 2023-08-27 21:28:43
合計ジャッジ時間 5,939 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
71,512 KB
testcase_01 AC 92 ms
71,712 KB
testcase_02 AC 91 ms
71,636 KB
testcase_03 AC 106 ms
77,808 KB
testcase_04 AC 93 ms
71,748 KB
testcase_05 AC 110 ms
77,820 KB
testcase_06 AC 102 ms
77,640 KB
testcase_07 AC 101 ms
77,628 KB
testcase_08 AC 108 ms
77,588 KB
testcase_09 AC 108 ms
77,808 KB
testcase_10 AC 113 ms
77,896 KB
testcase_11 AC 113 ms
77,704 KB
testcase_12 AC 109 ms
77,560 KB
testcase_13 AC 105 ms
77,576 KB
testcase_14 AC 112 ms
77,812 KB
testcase_15 AC 111 ms
77,884 KB
testcase_16 AC 112 ms
77,844 KB
testcase_17 AC 114 ms
77,928 KB
testcase_18 AC 103 ms
77,624 KB
testcase_19 AC 110 ms
77,872 KB
testcase_20 AC 93 ms
71,752 KB
testcase_21 AC 90 ms
71,640 KB
testcase_22 AC 108 ms
77,624 KB
testcase_23 AC 111 ms
77,748 KB
testcase_24 AC 109 ms
77,824 KB
testcase_25 AC 110 ms
77,688 KB
testcase_26 AC 91 ms
71,812 KB
testcase_27 AC 102 ms
77,824 KB
testcase_28 AC 111 ms
77,496 KB
testcase_29 AC 107 ms
77,720 KB
testcase_30 AC 91 ms
71,640 KB
testcase_31 AC 91 ms
71,376 KB
testcase_32 AC 109 ms
77,704 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

n=int(input())

def bit_cnt(n):
    #nは64bit未満
    count = (n & 0x5555555555555555) + ((n >> 1) & 0x5555555555555555)
    count = (count & 0x3333333333333333) + ((count >> 2) & 0x3333333333333333)
    count = (count & 0x0f0f0f0f0f0f0f0f) + ((count >> 4) & 0x0f0f0f0f0f0f0f0f)
    count = (count & 0x00ff00ff00ff00ff) + ((count >> 8) & 0x00ff00ff00ff00ff)
    count = (count & 0x0000ffff0000ffff) + ((count >> 16) & 0x0000ffff0000ffff)
    return int((count & 0x00000000ffffffff) + ((count >> 32) & 0x00000000ffffffff))

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