結果

問題 No.3 ビットすごろく
ユーザー te-shte-sh
提出日時 2016-08-27 18:12:12
言語 D
(dmd 2.106.1)
結果
TLE  
実行時間 -
コード長 816 bytes
コンパイル時間 727 ms
コンパイル使用メモリ 92,664 KB
実行使用メモリ 67,820 KB
最終ジャッジ日時 2023-09-02 21:29:07
合計ジャッジ時間 10,789 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,372 KB
testcase_01 AC 2 ms
4,368 KB
testcase_02 AC 1 ms
4,368 KB
testcase_03 AC 19 ms
5,648 KB
testcase_04 AC 4 ms
4,368 KB
testcase_05 AC 316 ms
34,228 KB
testcase_06 AC 21 ms
5,648 KB
testcase_07 AC 10 ms
4,368 KB
testcase_08 AC 106 ms
14,416 KB
testcase_09 AC 2,055 ms
63,468 KB
testcase_10 TLE -
testcase_11 AC 1,232 ms
63,564 KB
testcase_12 AC 255 ms
26,604 KB
testcase_13 AC 15 ms
4,660 KB
testcase_14 AC 4,572 ms
67,508 KB
testcase_15 TLE -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.array, std.range;
import std.string, std.conv;
import std.math;
import std.stdio, std.typecons;

alias Tuple!(int, "p", int, "c") qitem;

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

  writeln(calc(n));
}

int calc(int n)
{
  auto q = [qitem(1, 1)];
  auto visited = new bool[n + 1];

  while (!q.empty) {
    auto qi = q.front;
    q.popFront;

    visited[qi.p] = true;

    if (qi.p == n) {
      return qi.c;
    } else {
      auto b = bits(qi.p);

      auto p1 = qi.p - b;
      if (p1 >= 1 && !visited[p1])
        q ~= qitem(p1, qi.c + 1);

      auto p2 = qi.p + b;
      if (p2 <= n && !visited[p2])
        q ~= qitem(p2, qi.c + 1);
    }
  }
  return -1;
}

int bits(int n)
{
  auto c = 0;
  while (n > 0) {
    if ((n & 1) == 1)
      c += 1;
    n >>= 1;
  }
  return c;
}
0