結果
| 問題 | No.3 ビットすごろく |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-11-01 17:21:53 |
| 言語 | PyPy2 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 315 ms / 5,000 ms |
| コード長 | 444 bytes |
| 記録 | |
| コンパイル時間 | 145 ms |
| コンパイル使用メモリ | 77,184 KB |
| 最終ジャッジ日時 | 2025-12-03 22:31:48 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
ソースコード
import Queue
def countBit(n):
s=str(bin(n))
count=0
for i in xrange(len(s)):
if s[i]=="1": count+=1
return count
N=int(raw_input())
lst=[10**9]*(N+1)
lst[1]=1
q=Queue.Queue()
q.put(1)
while(not q.empty()):
a=q.get()
c=countBit(a)
if a-c>0 and lst[a-c] > lst[a]+1:
lst[a-c] = lst[a]+1
q.put(a-c)
if a+c<=N and lst[a+c] > lst[a]+1:
lst[a+c] = lst[a]+1
q.put(a+c)
if lst[N]==10**9: print -1
else: print lst[N]