結果

問題 No.3 ビットすごろく
ユーザー snipsnipsnipsnipsnipsnip
提出日時 2016-05-16 22:11:13
言語 Java21
(openjdk 21)
結果
AC  
実行時間 133 ms / 5,000 ms
コード長 1,199 bytes
コンパイル時間 3,146 ms
コンパイル使用メモリ 74,604 KB
実行使用メモリ 56,136 KB
最終ジャッジ日時 2023-09-13 23:54:24
合計ジャッジ時間 7,838 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
56,016 KB
testcase_01 AC 126 ms
55,584 KB
testcase_02 AC 127 ms
55,576 KB
testcase_03 AC 129 ms
56,136 KB
testcase_04 AC 126 ms
53,744 KB
testcase_05 AC 129 ms
55,844 KB
testcase_06 AC 129 ms
55,692 KB
testcase_07 AC 127 ms
55,736 KB
testcase_08 AC 128 ms
55,984 KB
testcase_09 AC 129 ms
53,712 KB
testcase_10 AC 127 ms
55,788 KB
testcase_11 AC 128 ms
55,548 KB
testcase_12 AC 129 ms
55,536 KB
testcase_13 AC 125 ms
56,088 KB
testcase_14 AC 128 ms
56,092 KB
testcase_15 AC 129 ms
55,996 KB
testcase_16 AC 129 ms
56,080 KB
testcase_17 AC 129 ms
55,960 KB
testcase_18 AC 133 ms
55,908 KB
testcase_19 AC 129 ms
55,812 KB
testcase_20 AC 127 ms
56,020 KB
testcase_21 AC 126 ms
55,580 KB
testcase_22 AC 129 ms
56,024 KB
testcase_23 AC 128 ms
55,792 KB
testcase_24 AC 130 ms
55,820 KB
testcase_25 AC 131 ms
55,888 KB
testcase_26 AC 127 ms
55,808 KB
testcase_27 AC 127 ms
55,840 KB
testcase_28 AC 128 ms
55,928 KB
testcase_29 AC 128 ms
55,584 KB
testcase_30 AC 125 ms
53,536 KB
testcase_31 AC 126 ms
55,724 KB
testcase_32 AC 127 ms
55,752 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 d; }
      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