結果

問題 No.3 ビットすごろく
ユーザー kano_townkano_town
提出日時 2014-11-18 22:40:44
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,215 bytes
コンパイル時間 4,446 ms
コンパイル使用メモリ 74,620 KB
実行使用メモリ 53,880 KB
最終ジャッジ日時 2023-08-30 20:55:57
合計ジャッジ時間 7,870 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
50,576 KB
testcase_01 AC 77 ms
51,408 KB
testcase_02 AC 78 ms
51,560 KB
testcase_03 AC 77 ms
50,952 KB
testcase_04 AC 78 ms
51,400 KB
testcase_05 AC 78 ms
52,112 KB
testcase_06 AC 76 ms
50,732 KB
testcase_07 AC 77 ms
51,844 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 79 ms
52,096 KB
testcase_13 AC 80 ms
51,808 KB
testcase_14 AC 78 ms
49,088 KB
testcase_15 AC 79 ms
52,096 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 77 ms
50,832 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 80 ms
51,716 KB
testcase_22 AC 80 ms
52,164 KB
testcase_23 AC 69 ms
51,708 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 66 ms
50,764 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 75 ms
51,672 KB
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;


public class Yukicoder3 {

	static int N;
	static int[] bitCount = new int[10001];
	static boolean[] memo = new boolean[10001];
	static int count = 0;
	static int min = Integer.MAX_VALUE;
	static boolean found = false;

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		N = Integer.parseInt(br.readLine());

		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, false);
		count = 1;
		search(1);

		if (found)
			System.out.println(min);
		else 
			System.out.println(-1);
	}

	private static void search(int n) {
		if (n < 1 || n > N || memo[n]) {
			count--;
			return;
		}
		memo[n] = true;
		if (n == N) {
			found = true;
			min = Math.min(min, count);
			return;
		}
		count++;
		search(n + bitCount[n]);
		count++;
		search(n - bitCount[n]);
	}

}
0