結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2016-11-01 17:21:53 | 
| 言語 | Python2 (2.7.18) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 68 ms / 5,000 ms | 
| コード長 | 444 bytes | 
| コンパイル時間 | 41 ms | 
| コンパイル使用メモリ | 7,040 KB | 
| 実行使用メモリ | 7,296 KB | 
| 最終ジャッジ日時 | 2024-07-01 08:09:03 | 
| 合計ジャッジ時間 | 2,373 ms | 
| ジャッジサーバーID (参考情報) | judge4 / 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]
            
            
            
        