結果

問題 No.3 ビットすごろく
ユーザー shinshin
提出日時 2022-05-17 16:18:26
言語 Java19
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,184 bytes
コンパイル時間 3,411 ms
コンパイル使用メモリ 75,684 KB
実行使用メモリ 54,960 KB
最終ジャッジ日時 2023-10-13 14:13:58
合計ジャッジ時間 7,976 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
49,308 KB
testcase_01 AC 40 ms
49,196 KB
testcase_02 AC 41 ms
49,048 KB
testcase_03 AC 81 ms
53,708 KB
testcase_04 WA -
testcase_05 AC 91 ms
54,164 KB
testcase_06 AC 83 ms
53,708 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 93 ms
54,104 KB
testcase_13 AC 69 ms
51,232 KB
testcase_14 AC 100 ms
53,684 KB
testcase_15 AC 110 ms
54,960 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 43 ms
49,092 KB
testcase_22 AC 101 ms
53,788 KB
testcase_23 AC 109 ms
54,332 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 39 ms
49,132 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 41 ms
49,592 KB
testcase_31 WA -
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Queue;

public class No3 {

	public static void main(String[] args) throws IOException{
		//ビットすごろく
		int N = Integer.parseInt(readStr()[0]) , k = 0;
		int[] root = new int[N + 1];
		root[1] = 1;
		Queue<Integer> qu = new ArrayDeque<>();
		qu.add(1);
		
		
		while(qu.size() > 0 && (k = qu.poll()) < N) {
			if(k + Integer.bitCount(k) <= N) {
				root[k + Integer.bitCount(k)] = root[k] + 1;
				qu.add(k + Integer.bitCount(k));
			}
			if(k - Integer.bitCount(k) >= 1 && root[k - Integer.bitCount(k)] == 0) {
				root[k - Integer.bitCount(k)] = root[k] + 1;
				qu.add(k - Integer.bitCount(k));
			}
		}
		
		System.out.println(root[N] == 0 ? -1 : root[N]);

	}
	
	public static String[] readStr() throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		ArrayList<String> list = new ArrayList<>();

		do {
			list.add(br.readLine());
		}while(br.ready());

		br.close();

		String[] text = new String[list.size()];
		list.toArray(text);

		return text;

	}

}
0