結果

問題 No.3 ビットすごろく
ユーザー bambambambam
提出日時 2019-08-22 11:20:50
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,147 bytes
コンパイル時間 2,436 ms
コンパイル使用メモリ 77,696 KB
実行使用メモリ 61,292 KB
最終ジャッジ日時 2024-04-20 08:22:20
合計ジャッジ時間 9,315 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
54,336 KB
testcase_01 AC 125 ms
54,156 KB
testcase_02 AC 113 ms
53,096 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	static boolean memo[];
	static int ans, min;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		StringBuilder sb = new StringBuilder();
		int n = sc.nextInt();
		int map[] = new int[n + 1];
		int cnt;
		String bin;
		for (int i = 1; i <= n; i++) {
			cnt = 0;
			bin = Integer.toBinaryString(i);
			for (int j = 0; j < bin.length(); j++) {
				if (bin.charAt(j) == '1') {
					cnt++;
				}
			}
			map[i] = cnt;
		}
		memo = new boolean[n + 1];
		memo[1] = true;
		ans = 0;
		min = Integer.MAX_VALUE;
		solve(map, 1, n);
		if (min == Integer.MAX_VALUE) {
			System.out.println(-1);
		} else {
			System.out.println(min + 1);
		}
	}

	private static void solve(int[] map, int s, int n) {
		if (s == n) {
			min = Math.min(min, ans);
			return;
		}

		if (s + map[s] <= n && !memo[s + map[s]]) {
			memo[s + map[s]] = true;
			ans++;
			solve(map, s + map[s], n);
			ans--;
			memo[s + map[s]] = false;
		}
		if (s - map[s] > 0 && !memo[s - map[s]]) {
			memo[s - map[s]] = true;
			ans++;
			solve(map, s - map[s], n);
			ans--;
			memo[s - map[s]] = false;
		}
	}
}
0