結果
| 問題 | No.3 ビットすごろく |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-05-16 22:11:13 |
| 言語 | Java (openjdk 25.0.2) |
| 結果 |
AC
|
| 実行時間 | 57 ms / 5,000 ms |
| コード長 | 1,199 bytes |
| 記録 | |
| コンパイル時間 | 1,523 ms |
| コンパイル使用メモリ | 82,712 KB |
| 実行使用メモリ | 43,332 KB |
| 最終ジャッジ日時 | 2026-03-22 04:12:14 |
| 合計ジャッジ時間 | 4,284 ms |
|
ジャッジサーバーID (参考情報) |
judge2_0 / judge1_1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
ソースコード
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;
}
}