結果

問題 No.3 ビットすごろく
ユーザー kano_townkano_town
提出日時 2014-11-22 16:18:01
言語 Java21
(openjdk 21)
結果
AC  
実行時間 277 ms / 5,000 ms
コード長 1,099 bytes
コンパイル時間 3,007 ms
コンパイル使用メモリ 75,092 KB
実行使用メモリ 58,168 KB
最終ジャッジ日時 2023-09-13 22:56:35
合計ジャッジ時間 10,683 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 142 ms
55,752 KB
testcase_01 AC 146 ms
56,500 KB
testcase_02 AC 143 ms
55,860 KB
testcase_03 AC 154 ms
55,916 KB
testcase_04 AC 154 ms
56,024 KB
testcase_05 AC 183 ms
56,284 KB
testcase_06 AC 160 ms
55,816 KB
testcase_07 AC 150 ms
53,796 KB
testcase_08 AC 177 ms
55,748 KB
testcase_09 AC 218 ms
55,816 KB
testcase_10 AC 225 ms
56,300 KB
testcase_11 AC 207 ms
56,012 KB
testcase_12 AC 177 ms
58,168 KB
testcase_13 AC 152 ms
56,008 KB
testcase_14 AC 223 ms
54,184 KB
testcase_15 AC 266 ms
55,976 KB
testcase_16 AC 241 ms
56,036 KB
testcase_17 AC 257 ms
56,628 KB
testcase_18 AC 151 ms
56,012 KB
testcase_19 AC 277 ms
56,384 KB
testcase_20 AC 151 ms
55,712 KB
testcase_21 AC 139 ms
56,008 KB
testcase_22 AC 225 ms
56,044 KB
testcase_23 AC 270 ms
56,440 KB
testcase_24 AC 264 ms
56,584 KB
testcase_25 AC 254 ms
56,224 KB
testcase_26 AC 144 ms
56,012 KB
testcase_27 AC 160 ms
56,052 KB
testcase_28 AC 246 ms
56,568 KB
testcase_29 AC 196 ms
54,312 KB
testcase_30 AC 144 ms
56,324 KB
testcase_31 AC 140 ms
56,132 KB
testcase_32 AC 188 ms
56,004 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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--;
	}
}
0