結果

問題 No.3 ビットすごろく
ユーザー te-sh
提出日時 2017-01-11 17:38:27
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 643 bytes
コンパイル時間 749 ms
コンパイル使用メモリ 98,048 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-12 06:19:30
合計ジャッジ時間 1,742 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import core.bitop;    // bit operation

void main()
{
  auto n = readln.chomp.to!int;
  writeln(calc(n));
}

auto calc(int n)
{
  if (n == 1) return 1;

  auto dp = new bool[](n + 1);
  dp[1] = true;

  auto a = [1];

  for (auto c = 1; !a.empty; ++c) {
    int[] b = [];
    foreach (i; a) {
      auto e = i.popcnt;
      if (i + e <= n && !dp[i + e]) {
        if (i + e == n) return c + 1;
        dp[i + e] = true;
        b ~= i + e;
      }
      if (i - e >= 1 && !dp[i - e]) {
        dp[i - e] = true;
        b ~= i - e;
      }
    }
    a = b;
  }

  return -1;
}
0