結果
| 問題 | No.3 ビットすごろく |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2014-11-22 16:18:01 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 326 ms / 5,000 ms |
| コード長 | 1,099 bytes |
| コンパイル時間 | 3,523 ms |
| コンパイル使用メモリ | 78,128 KB |
| 実行使用メモリ | 55,928 KB |
| 最終ジャッジ日時 | 2024-07-01 07:08:20 |
| 合計ジャッジ時間 | 12,103 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
ソースコード
import java.util.Arrays;
import java.util.Scanner;
public class Yukicoder03 {
static int N;
static int[] bitCount = new int[10001];
static int[] memo = new int[10001];
static int count = 0;
static int min = Integer.MAX_VALUE;
static boolean found = false;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
String buf;
Arrays.fill(bitCount, 0);
for (int i = 1; i <= 10000; i++) {
buf = Integer.toBinaryString(i);
for (int j = 0; j < buf.length(); j++) {
if (buf.charAt(j) == '1') bitCount[i]++;
}
}
// search
Arrays.fill(memo, 0);
count = 1;
search(1);
if (found)
System.out.println(min);
else
System.out.println(-1);
}
private static void search(int n) {
if (memo[n] != 0 && memo[n] <= count) return;
memo[n] = count;
if (n == N) {
found = true;
min = Math.min(min, count);
return;
}
count++;
if (n - bitCount[n] >= 1)
search(n - bitCount[n]);
if (n + bitCount[n] <= N)
search(n + bitCount[n]);
count--;
}
}