結果

問題 No.3 ビットすごろく
ユーザー gahougahou
提出日時 2016-11-01 17:21:53
言語 Python2
(2.7.18)
結果
AC  
実行時間 65 ms / 5,000 ms
コード長 444 bytes
コンパイル時間 642 ms
コンパイル使用メモリ 6,600 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2023-09-14 00:11:29
合計ジャッジ時間 2,754 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
6,532 KB
testcase_01 AC 15 ms
6,484 KB
testcase_02 AC 14 ms
6,488 KB
testcase_03 AC 26 ms
6,616 KB
testcase_04 AC 18 ms
6,600 KB
testcase_05 AC 40 ms
6,588 KB
testcase_06 AC 27 ms
6,564 KB
testcase_07 AC 22 ms
6,504 KB
testcase_08 AC 35 ms
6,668 KB
testcase_09 AC 48 ms
6,596 KB
testcase_10 AC 56 ms
6,812 KB
testcase_11 AC 45 ms
6,608 KB
testcase_12 AC 40 ms
6,700 KB
testcase_13 AC 25 ms
6,504 KB
testcase_14 AC 55 ms
6,776 KB
testcase_15 AC 63 ms
6,820 KB
testcase_16 AC 60 ms
6,804 KB
testcase_17 AC 63 ms
6,776 KB
testcase_18 AC 23 ms
6,500 KB
testcase_19 AC 64 ms
6,716 KB
testcase_20 AC 17 ms
6,556 KB
testcase_21 AC 14 ms
6,604 KB
testcase_22 AC 55 ms
6,628 KB
testcase_23 AC 65 ms
6,680 KB
testcase_24 AC 65 ms
6,816 KB
testcase_25 AC 64 ms
6,676 KB
testcase_26 AC 15 ms
6,456 KB
testcase_27 AC 25 ms
6,532 KB
testcase_28 AC 59 ms
6,732 KB
testcase_29 AC 46 ms
6,584 KB
testcase_30 AC 14 ms
6,456 KB
testcase_31 AC 15 ms
6,484 KB
testcase_32 AC 43 ms
6,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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]
0