結果

問題 No.3 ビットすごろく
ユーザー nikumakarenikumakare
提出日時 2021-07-16 20:55:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 54 ms / 5,000 ms
コード長 463 bytes
コンパイル時間 315 ms
コンパイル使用メモリ 82,572 KB
実行使用メモリ 70,416 KB
最終ジャッジ日時 2024-07-06 07:41:49
合計ジャッジ時間 2,981 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

n=int(input())
INF=10**10

dp=[INF for i in range(n+1)]
dp[1]=1

def popcount(x):
    return bin(x).count("1")

que=[1]
head=0
while head<len(que):
    now=que[head]
    head+=1
    dif=popcount(now)
    if 1<=now+dif<=n and dp[now+dif]==INF:
        dp[now+dif]=dp[now]+1
        que.append(now+dif)
    dif*=-1
    if 1<=now+dif<=n and dp[now+dif]==INF:
        dp[now+dif]=dp[now]+1
        que.append(now+dif)

print(dp[-1]) if dp[-1]<INF else print(-1)
    
0