結果

問題 No.3 ビットすごろく
ユーザー mosmos_21mosmos_21
提出日時 2017-06-16 23:02:08
言語 Java21
(openjdk 21)
結果
AC  
実行時間 139 ms / 5,000 ms
コード長 806 bytes
コンパイル時間 2,121 ms
コンパイル使用メモリ 75,844 KB
実行使用メモリ 56,500 KB
最終ジャッジ日時 2023-09-14 00:45:10
合計ジャッジ時間 7,759 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
56,088 KB
testcase_01 AC 123 ms
55,804 KB
testcase_02 AC 124 ms
55,796 KB
testcase_03 AC 127 ms
55,540 KB
testcase_04 AC 126 ms
56,092 KB
testcase_05 AC 128 ms
55,904 KB
testcase_06 AC 127 ms
56,096 KB
testcase_07 AC 126 ms
56,104 KB
testcase_08 AC 127 ms
56,080 KB
testcase_09 AC 128 ms
55,776 KB
testcase_10 AC 135 ms
56,368 KB
testcase_11 AC 128 ms
55,812 KB
testcase_12 AC 127 ms
55,912 KB
testcase_13 AC 127 ms
56,144 KB
testcase_14 AC 135 ms
56,444 KB
testcase_15 AC 135 ms
55,976 KB
testcase_16 AC 137 ms
55,736 KB
testcase_17 AC 134 ms
55,812 KB
testcase_18 AC 128 ms
55,916 KB
testcase_19 AC 139 ms
55,748 KB
testcase_20 AC 126 ms
55,792 KB
testcase_21 AC 126 ms
55,860 KB
testcase_22 AC 134 ms
56,020 KB
testcase_23 AC 133 ms
55,560 KB
testcase_24 AC 134 ms
56,000 KB
testcase_25 AC 134 ms
55,972 KB
testcase_26 AC 122 ms
56,192 KB
testcase_27 AC 127 ms
56,112 KB
testcase_28 AC 134 ms
55,744 KB
testcase_29 AC 130 ms
55,768 KB
testcase_30 AC 127 ms
56,500 KB
testcase_31 AC 125 ms
55,804 KB
testcase_32 AC 131 ms
56,188 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.Scanner;

public class Main {
	static Scanner in = new Scanner(System.in);

	public static void main(String[] args) {
		int n = in.nextInt();
		int[] d = new int[n + 1];
		Arrays.fill(d, Integer.MAX_VALUE);
		d[1] = 1;
		Deque<Integer> q = new ArrayDeque<>();
		q.add(1);
		while (q.size() > 0) {
			int a = q.poll(), b = bit(a);
			if (0 < a - b && d[a - b] > d[a] + 1) {
				d[a - b] = d[a] + 1;
				q.add(a - b);
			}
			if (a + b <= n && d[a + b] > d[a] + 1) {
				d[a + b] = d[a] + 1;
				q.add(a + b);
			}
		}
		System.out.println(d[n] == Integer.MAX_VALUE ? -1 : d[n]);
	}

	public static int bit(int n) {
		int s = 0;
		while (n > 0) {
			if (n % 2 == 1) {
				s++;
			}
			n /= 2;
		}
		return s;
	}
}
0