結果

問題 No.3 ビットすごろく
ユーザー aaaaasatoriaaaaasatori
提出日時 2018-02-18 13:39:40
言語 Java21
(openjdk 21)
結果
AC  
実行時間 134 ms / 5,000 ms
コード長 906 bytes
コンパイル時間 4,950 ms
コンパイル使用メモリ 81,204 KB
実行使用メモリ 56,540 KB
最終ジャッジ日時 2023-09-14 00:56:14
合計ジャッジ時間 8,722 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
55,996 KB
testcase_01 AC 124 ms
56,540 KB
testcase_02 AC 120 ms
56,180 KB
testcase_03 AC 124 ms
55,600 KB
testcase_04 AC 121 ms
55,604 KB
testcase_05 AC 124 ms
55,704 KB
testcase_06 AC 121 ms
55,900 KB
testcase_07 AC 131 ms
55,648 KB
testcase_08 AC 132 ms
55,744 KB
testcase_09 AC 126 ms
55,860 KB
testcase_10 AC 133 ms
55,652 KB
testcase_11 AC 125 ms
53,788 KB
testcase_12 AC 122 ms
55,724 KB
testcase_13 AC 123 ms
55,960 KB
testcase_14 AC 131 ms
55,756 KB
testcase_15 AC 134 ms
56,216 KB
testcase_16 AC 130 ms
55,796 KB
testcase_17 AC 128 ms
56,012 KB
testcase_18 AC 120 ms
55,836 KB
testcase_19 AC 132 ms
55,820 KB
testcase_20 AC 122 ms
55,696 KB
testcase_21 AC 121 ms
55,964 KB
testcase_22 AC 129 ms
55,900 KB
testcase_23 AC 127 ms
54,200 KB
testcase_24 AC 128 ms
55,976 KB
testcase_25 AC 128 ms
56,200 KB
testcase_26 AC 117 ms
55,464 KB
testcase_27 AC 120 ms
55,696 KB
testcase_28 AC 127 ms
55,600 KB
testcase_29 AC 121 ms
56,168 KB
testcase_30 AC 118 ms
55,740 KB
testcase_31 AC 116 ms
55,924 KB
testcase_32 AC 122 ms
55,740 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class No3 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int hosuu[] = new int[N+1];
		hosuu[1] = 1;
		Queue<Integer> queue = new LinkedList();
		queue.offer(1);
		
		while(true) {
			Integer i = queue.poll();
			if(i == null) {
				break;
			}
			if(i - bitcount(i) > 0 && hosuu[i - bitcount(i)] == 0){
				hosuu[i - bitcount(i)] = hosuu[i] + 1;
				queue.offer(i - bitcount(i));
			}
			if(i + bitcount(i) <= N && hosuu[i + bitcount(i)] == 0) {
				hosuu[i + bitcount(i)] = hosuu[i] + 1;
				queue.offer(i + bitcount(i));
			}
		}
		
		if(hosuu[N] == 0) {
			System.out.println(-1);
		}else {
			System.out.println(hosuu[N]);
		}
	}
	
	public static int bitcount(int x) {
		int bit = 0;
		while(x != 0) {
			bit += x % 2;
			x /= 2;
		}
		return bit;
	}
}
0