結果

問題 No.3 ビットすごろく
ユーザー nakamura sosukenakamura sosuke
提出日時 2019-07-05 10:17:03
言語 Ruby
(3.3.0)
結果
AC  
実行時間 154 ms / 5,000 ms
コード長 509 bytes
コンパイル時間 63 ms
コンパイル使用メモリ 11,608 KB
実行使用メモリ 15,696 KB
最終ジャッジ日時 2023-09-14 01:21:44
合計ジャッジ時間 5,257 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 84 ms
15,176 KB
testcase_01 AC 84 ms
15,064 KB
testcase_02 AC 82 ms
15,104 KB
testcase_03 AC 92 ms
15,348 KB
testcase_04 AC 86 ms
15,300 KB
testcase_05 AC 109 ms
15,552 KB
testcase_06 AC 94 ms
15,472 KB
testcase_07 AC 88 ms
15,460 KB
testcase_08 AC 104 ms
15,548 KB
testcase_09 AC 122 ms
15,580 KB
testcase_10 AC 142 ms
15,384 KB
testcase_11 AC 117 ms
15,640 KB
testcase_12 AC 107 ms
15,488 KB
testcase_13 AC 89 ms
15,308 KB
testcase_14 AC 137 ms
15,364 KB
testcase_15 AC 149 ms
15,696 KB
testcase_16 AC 151 ms
15,648 KB
testcase_17 AC 149 ms
15,496 KB
testcase_18 AC 89 ms
15,320 KB
testcase_19 AC 151 ms
15,536 KB
testcase_20 AC 84 ms
15,156 KB
testcase_21 AC 84 ms
15,328 KB
testcase_22 AC 140 ms
15,572 KB
testcase_23 AC 151 ms
15,588 KB
testcase_24 AC 149 ms
15,460 KB
testcase_25 AC 154 ms
15,560 KB
testcase_26 AC 81 ms
15,304 KB
testcase_27 AC 89 ms
15,340 KB
testcase_28 AC 144 ms
15,636 KB
testcase_29 AC 118 ms
15,460 KB
testcase_30 AC 82 ms
15,180 KB
testcase_31 AC 84 ms
15,080 KB
testcase_32 AC 114 ms
15,328 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:22: warning: mismatched indentations at 'end' with 'if' at 17
Main.rb:29: warning: ambiguous first argument; put parentheses or a space even after `-' operator
Syntax OK

ソースコード

diff #

N = gets.to_i
INF = 10**10

dp = Array.new(N+1){ INF }
dp[0], dp[1] = 0, 1

def move(dp, from)
  dist = from.to_s(2).count('1')
	# ++
	if from + dist <= N
    if dp[from + dist] > dp[from] + 1
      dp[from + dist] = dp[from] + 1
      dp = move(dp, from + dist)
    end
	end
	# --
	if from - dist >= 0
    if dp[from - dist] > dp[from] + 1
      dp[from - dist] = dp[from] + 1
      dp = move(dp, from - dist)
    end
  end
  return dp
end

dp = move(dp, 1)

if dp[-1] == INF
  puts -1
else
  puts dp[-1]
end
0