結果

問題 No.3 ビットすごろく
ユーザー mosmos_21mosmos_21
提出日時 2017-06-17 11:04:45
言語 Java21
(openjdk 21)
結果
AC  
実行時間 139 ms / 5,000 ms
コード長 915 bytes
コンパイル時間 2,418 ms
コンパイル使用メモリ 75,616 KB
実行使用メモリ 56,328 KB
最終ジャッジ日時 2023-09-14 00:45:22
合計ジャッジ時間 8,352 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 118 ms
55,820 KB
testcase_01 AC 123 ms
55,764 KB
testcase_02 AC 115 ms
56,144 KB
testcase_03 AC 119 ms
55,836 KB
testcase_04 AC 117 ms
55,812 KB
testcase_05 AC 120 ms
55,760 KB
testcase_06 AC 122 ms
55,744 KB
testcase_07 AC 119 ms
55,588 KB
testcase_08 AC 123 ms
56,060 KB
testcase_09 AC 123 ms
56,184 KB
testcase_10 AC 128 ms
55,548 KB
testcase_11 AC 121 ms
55,796 KB
testcase_12 AC 122 ms
55,756 KB
testcase_13 AC 121 ms
55,968 KB
testcase_14 AC 127 ms
56,168 KB
testcase_15 AC 131 ms
56,328 KB
testcase_16 AC 130 ms
55,972 KB
testcase_17 AC 129 ms
55,668 KB
testcase_18 AC 121 ms
55,788 KB
testcase_19 AC 126 ms
55,804 KB
testcase_20 AC 118 ms
55,888 KB
testcase_21 AC 116 ms
55,348 KB
testcase_22 AC 134 ms
55,312 KB
testcase_23 AC 134 ms
55,924 KB
testcase_24 AC 139 ms
55,500 KB
testcase_25 AC 130 ms
56,036 KB
testcase_26 AC 114 ms
55,860 KB
testcase_27 AC 113 ms
56,048 KB
testcase_28 AC 123 ms
55,816 KB
testcase_29 AC 121 ms
56,216 KB
testcase_30 AC 127 ms
55,552 KB
testcase_31 AC 116 ms
55,900 KB
testcase_32 AC 120 ms
55,752 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();
		if(n == 1){
			System.out.println(1);
			return;
		}
		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(a + b == n){
				System.out.println(d[a] + 1);
				return;
			}
			if (0 < a - b && d[a - b] == Integer.MAX_VALUE) {
				d[a - b] = d[a] + 1;
				q.add(a - b);
			}
			if (a + b < n && d[a + b] == Integer.MAX_VALUE) {
				d[a + b] = d[a] + 1;
				q.add(a + b);
			}
		}
		System.out.println(-1);
	}

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