結果

問題 No.3 ビットすごろく
ユーザー mosmos_21mosmos_21
提出日時 2017-06-17 11:02:59
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 861 bytes
コンパイル時間 3,905 ms
コンパイル使用メモリ 78,980 KB
実行使用メモリ 57,836 KB
最終ジャッジ日時 2024-04-08 17:24:18
合計ジャッジ時間 8,283 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 135 ms
57,616 KB
testcase_01 AC 131 ms
57,548 KB
testcase_02 AC 129 ms
57,580 KB
testcase_03 AC 135 ms
57,828 KB
testcase_04 AC 145 ms
57,676 KB
testcase_05 AC 135 ms
57,572 KB
testcase_06 AC 136 ms
57,656 KB
testcase_07 AC 134 ms
57,560 KB
testcase_08 AC 134 ms
57,700 KB
testcase_09 AC 138 ms
57,828 KB
testcase_10 AC 145 ms
57,648 KB
testcase_11 AC 136 ms
57,796 KB
testcase_12 AC 136 ms
57,692 KB
testcase_13 AC 167 ms
57,652 KB
testcase_14 AC 141 ms
57,656 KB
testcase_15 AC 142 ms
57,572 KB
testcase_16 AC 141 ms
57,576 KB
testcase_17 AC 148 ms
57,648 KB
testcase_18 AC 136 ms
57,656 KB
testcase_19 AC 142 ms
57,688 KB
testcase_20 AC 133 ms
57,700 KB
testcase_21 AC 130 ms
57,652 KB
testcase_22 AC 144 ms
57,652 KB
testcase_23 AC 148 ms
57,836 KB
testcase_24 AC 165 ms
57,572 KB
testcase_25 AC 144 ms
57,660 KB
testcase_26 WA -
testcase_27 AC 154 ms
57,708 KB
testcase_28 AC 142 ms
57,568 KB
testcase_29 AC 140 ms
57,676 KB
testcase_30 AC 133 ms
57,724 KB
testcase_31 AC 132 ms
57,656 KB
testcase_32 AC 137 ms
57,572 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(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