結果

問題 No.3 ビットすごろく
ユーザー nebukuro09nebukuro09
提出日時 2016-09-02 15:22:41
言語 PyPy2
(7.3.15)
結果
AC  
実行時間 167 ms / 5,000 ms
コード長 936 bytes
コンパイル時間 1,714 ms
コンパイル使用メモリ 77,376 KB
実行使用メモリ 82,572 KB
最終ジャッジ日時 2023-09-14 00:06:37
合計ジャッジ時間 7,377 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
78,844 KB
testcase_01 AC 84 ms
78,908 KB
testcase_02 AC 83 ms
78,856 KB
testcase_03 AC 126 ms
80,696 KB
testcase_04 AC 87 ms
79,232 KB
testcase_05 AC 144 ms
81,488 KB
testcase_06 AC 130 ms
80,932 KB
testcase_07 AC 108 ms
80,152 KB
testcase_08 AC 140 ms
81,088 KB
testcase_09 AC 152 ms
81,788 KB
testcase_10 AC 158 ms
81,956 KB
testcase_11 AC 147 ms
81,400 KB
testcase_12 AC 144 ms
81,372 KB
testcase_13 AC 116 ms
80,164 KB
testcase_14 AC 159 ms
81,812 KB
testcase_15 AC 166 ms
82,268 KB
testcase_16 AC 165 ms
82,384 KB
testcase_17 AC 166 ms
82,572 KB
testcase_18 AC 112 ms
79,836 KB
testcase_19 AC 167 ms
82,344 KB
testcase_20 AC 86 ms
78,880 KB
testcase_21 AC 84 ms
79,120 KB
testcase_22 AC 160 ms
82,068 KB
testcase_23 AC 165 ms
82,332 KB
testcase_24 AC 164 ms
82,232 KB
testcase_25 AC 165 ms
82,384 KB
testcase_26 AC 84 ms
79,012 KB
testcase_27 AC 123 ms
80,536 KB
testcase_28 AC 163 ms
82,280 KB
testcase_29 AC 149 ms
81,312 KB
testcase_30 AC 85 ms
78,972 KB
testcase_31 AC 86 ms
79,132 KB
testcase_32 AC 152 ms
81,448 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq

N = input()
def bitcount(i):
    i = i - ((i >> 1) & 0x55555555)
    i = (i & 0x33333333) + ((i >> 2) & 0x33333333)
    i = (i + (i >> 4)) & 0x0f0f0f0f
    i = i + (i >> 8)
    i = i + (i >> 16)
    return i & 0x3f

bc = [bitcount(i) for i in xrange(10001)]
rinsetsu = [[] for i in xrange(N+1)]
for i in xrange(1, N+1):
    m = i + bc[i]
    if m <= N:
        rinsetsu[i].append(m)
    m = i - bc[i]
    if m >= 1:
        rinsetsu[i].append(m)
        
def dijkstra(start):
    d = [float('inf') for i in xrange(N+1)]
    d[start] = 0
    q = []
    heapq.heappush(q, (0, start))
    while len(q) != 0:
        c, node = heapq.heappop(q)
        if c > d[node]:
            continue
        for nn in rinsetsu[node]:
            alt = d[node] + 1
            if alt < d[nn]:
                d[nn] = alt
                heapq.heappush(q, (alt, nn))
    return d

d = dijkstra(1)[N] 
print -1 if d == float('inf') else d+1
0