結果

問題 No.3 ビットすごろく
ユーザー syunsukesyunsuke
提出日時 2019-07-06 22:26:23
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 254 ms / 5,000 ms
コード長 1,032 bytes
コンパイル時間 346 ms
コンパイル使用メモリ 86,956 KB
実行使用メモリ 81,860 KB
最終ジャッジ日時 2023-09-14 01:22:20
合計ジャッジ時間 7,770 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
71,712 KB
testcase_01 AC 91 ms
71,612 KB
testcase_02 AC 89 ms
71,784 KB
testcase_03 AC 184 ms
79,472 KB
testcase_04 AC 139 ms
78,732 KB
testcase_05 AC 211 ms
80,400 KB
testcase_06 AC 173 ms
79,448 KB
testcase_07 AC 159 ms
78,920 KB
testcase_08 AC 200 ms
80,424 KB
testcase_09 AC 236 ms
81,052 KB
testcase_10 AC 243 ms
81,416 KB
testcase_11 AC 228 ms
81,060 KB
testcase_12 AC 218 ms
80,412 KB
testcase_13 AC 170 ms
79,404 KB
testcase_14 AC 235 ms
81,416 KB
testcase_15 AC 247 ms
81,656 KB
testcase_16 AC 239 ms
81,540 KB
testcase_17 AC 254 ms
81,860 KB
testcase_18 AC 164 ms
79,056 KB
testcase_19 AC 245 ms
81,744 KB
testcase_20 AC 119 ms
77,640 KB
testcase_21 AC 88 ms
71,712 KB
testcase_22 AC 239 ms
81,236 KB
testcase_23 AC 241 ms
81,748 KB
testcase_24 AC 245 ms
81,792 KB
testcase_25 AC 245 ms
81,660 KB
testcase_26 AC 88 ms
71,672 KB
testcase_27 AC 171 ms
79,548 KB
testcase_28 AC 241 ms
81,508 KB
testcase_29 AC 225 ms
80,968 KB
testcase_30 AC 97 ms
76,800 KB
testcase_31 AC 102 ms
76,944 KB
testcase_32 AC 218 ms
80,936 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N=int(input())

L=[10**10 for i in range(N+1)]
L[0]=0
L[1]=1
n=[0 for i in range(N+1)]
n[1]=1
for i in range(2,N+1):
    a=i
    cnt=0
    for j in range(14):
        if a//(2**(13-j))==1:
            cnt+=1
        a%=(2**(13-j))
    n[i]=cnt
#print(n[:10])

import heapq
import copy
#[1からのlength,移動距離,今の場所]
Q=[[2,1,1]]
heapq.heapify(Q)

for i in range(10**7):
    if len(Q)==0:
        break
    else:
        q=copy.deepcopy(Q[0])
        #print(q)
        heapq.heappop(Q)
        if (q[2]+q[1])<=N:
            if L[q[2]+q[1]]>q[0]:
                L[q[2]+q[1]]=q[0]
                heapq.heappush(Q,[q[0]+1,n[q[2]+q[1]],q[2]+q[1]])
                heapq.heappush(Q,[q[0]+1,-n[q[2]+q[1]],q[2]+q[1]])
        if (q[2]-q[1])<=N:
            if L[q[2]-q[1]]>q[0]:
                L[q[2]-q[1]]=q[0]
                heapq.heappush(Q,[q[0]+1,n[q[2]-q[1]],q[2]-q[1]])
                heapq.heappush(Q,[q[0]+1,-n[q[2]-q[1]],q[2]-q[1]])
#print(L[:12])
if L[N]==10**10:
    print(-1)
else:
    print(L[N])
        
0