結果

問題 No.3 ビットすごろく
コンテスト
ユーザー te-sh
提出日時 2017-01-11 17:38:27
言語 D
(dmd 2.112.0)
コンパイル:
dmd -fPIE -m64 -w -wi -O -release -inline -I/opt/dmd/src/druntime/import/ -I/opt/dmd/src/phobos -L-L/opt/dmd/linux/lib64/ -fPIC _filename_
実行:
./Main
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 643 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 428 ms
コンパイル使用メモリ 80,024 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2026-03-05 11:35:30
合計ジャッジ時間 1,428 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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