結果

問題 No.3 ビットすごろく
ユーザー snipsnipsnipsnipsnipsnip
提出日時 2016-05-16 22:09:40
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,209 bytes
コンパイル時間 2,163 ms
コンパイル使用メモリ 77,452 KB
実行使用メモリ 41,744 KB
最終ジャッジ日時 2024-04-15 22:39:03
合計ジャッジ時間 7,526 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 133 ms
41,252 KB
testcase_01 AC 134 ms
41,240 KB
testcase_02 AC 134 ms
41,104 KB
testcase_03 AC 134 ms
40,960 KB
testcase_04 AC 131 ms
40,880 KB
testcase_05 AC 133 ms
41,048 KB
testcase_06 AC 138 ms
41,212 KB
testcase_07 AC 138 ms
41,156 KB
testcase_08 AC 135 ms
41,172 KB
testcase_09 AC 134 ms
41,424 KB
testcase_10 AC 134 ms
41,348 KB
testcase_11 AC 136 ms
41,236 KB
testcase_12 AC 133 ms
41,328 KB
testcase_13 AC 134 ms
41,192 KB
testcase_14 AC 121 ms
40,228 KB
testcase_15 AC 136 ms
41,160 KB
testcase_16 AC 135 ms
41,080 KB
testcase_17 AC 130 ms
41,084 KB
testcase_18 AC 117 ms
40,312 KB
testcase_19 AC 120 ms
40,328 KB
testcase_20 AC 126 ms
41,136 KB
testcase_21 AC 120 ms
40,724 KB
testcase_22 AC 120 ms
39,872 KB
testcase_23 AC 131 ms
41,032 KB
testcase_24 AC 129 ms
41,176 KB
testcase_25 AC 126 ms
41,180 KB
testcase_26 WA -
testcase_27 AC 119 ms
40,144 KB
testcase_28 AC 133 ms
41,360 KB
testcase_29 AC 132 ms
41,364 KB
testcase_30 AC 130 ms
41,048 KB
testcase_31 AC 131 ms
41,040 KB
testcase_32 AC 129 ms
41,036 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

public class Main {
  public static void main(String[] args) {
    int n = new java.util.Scanner(System.in).nextInt();
    new Main(n).solve();
  }

  private int n;

  Main(int n) {
    this.n = n;
  }

  void solve() {
    System.out.println(path(n));
  }

  int bits(int n) {
    int c = 0;
    while (n > 0) {
      c += n % 2;
      n /= 2;
    }
    return c;
  }

  int path(int n) {
    boolean[] visited = new boolean[n];
    int len = 64;
    short[] q = new short[len];
    short[] depth = new short[len];
    int head = 0;
    int tail = 0;

    q[0] = 1;
    depth[0] = 1;
    tail++;

    while (head != tail) {
      short h = q[head];
      short d = depth[head];
      head = (head + 1) % len;
      if (h == n) { return depth[head]; }
      if (h < 1 || n < h || visited[h - 1]) { continue; }

      visited[h - 1] = true;

      int b = bits(h);

      q[tail] = (short)(h + b);
      depth[tail] = (short)(d + 1);
      tail = (tail + 1) % len;
      if (head == tail) { throw new RuntimeException(); }

      q[tail] = (short)(h - b);
      depth[tail] = (short)(d + 1);
      tail = (tail + 1) % len;
      if (head == tail) { throw new RuntimeException(); }
    }

    return -1;
  }
}
0