結果
| 問題 | No.3 ビットすごろく |
| コンテスト | |
| ユーザー |
mastersatoshi
|
| 提出日時 | 2015-08-01 10:01:21 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 74 ms / 5,000 ms |
| コード長 | 1,479 bytes |
| コンパイル時間 | 2,118 ms |
| コンパイル使用メモリ | 78,928 KB |
| 実行使用メモリ | 37,904 KB |
| 最終ジャッジ日時 | 2024-07-01 07:28:43 |
| 合計ジャッジ時間 | 5,106 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
ソースコード
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int[] cource = new int[N + 1];
ArrayList<Integer> now = new ArrayList();
now.add(1);
cource[1] = 1;
for (int i = 2; true; i++) {
ArrayList<Integer> next = new ArrayList();
for (int tmp : now) {
if (tmp == N) {
System.out.println(cource[N]);
return;
}
int bit = bitCount(tmp);
if (tmp + bit <= N && cource[tmp + bit] == 0) {
cource[tmp + bit] = i;
next.add(tmp + bit);
}
if (tmp + bit >= 1 && cource[tmp - bit] == 0) {
cource[tmp - bit] = i;
next.add(tmp - bit);
}
}
if (next.isEmpty()) {
System.out.println(-1);
return;
}
now = (ArrayList<Integer>) next.clone();
}
}
private static int bitCount(int source) {
int count = 0;
for (int i = 8192; source > 0; i = i / 2) {
if (source >= i) {
source -= i;
count++;
}
}
return count;
}
}
mastersatoshi