結果

問題 No.3 ビットすごろく
ユーザー katsumikatsumi
提出日時 2019-01-31 14:22:19
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,271 bytes
コンパイル時間 2,315 ms
コンパイル使用メモリ 77,700 KB
実行使用メモリ 56,228 KB
最終ジャッジ日時 2024-04-27 14:14:16
合計ジャッジ時間 8,233 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 135 ms
53,916 KB
testcase_01 AC 139 ms
54,308 KB
testcase_02 AC 133 ms
53,924 KB
testcase_03 AC 127 ms
53,052 KB
testcase_04 WA -
testcase_05 AC 144 ms
54,072 KB
testcase_06 AC 140 ms
54,136 KB
testcase_07 AC 140 ms
54,044 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 145 ms
54,096 KB
testcase_13 AC 140 ms
53,756 KB
testcase_14 AC 152 ms
53,812 KB
testcase_15 AC 162 ms
54,244 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 137 ms
54,124 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 131 ms
54,092 KB
testcase_22 AC 152 ms
54,252 KB
testcase_23 AC 161 ms
54,024 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 133 ms
53,788 KB
testcase_31 WA -
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;
public class Main {
	
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int N = sc.nextInt();
        
        if (N == 1) {
        	System.out.println(0);
        	return;
        }
        
        int[] map = new int[N+1];
        for (int i = 1; i <= N; i++) {
        	map[i] = Integer.bitCount(i);
        }
        
        boolean[] comp = new boolean[N+1];
        for (int i = 1; i < N; i++) {
        	int p = i;
        	while(p < N) {
        		if (p+map[p] == N) {
        			comp[i] = true;
        		}
        		
        		p += map[p];
        	}
        }
        
        int p = 1;
        int count = 1;
        int min = Integer.MAX_VALUE;
        while(p < N) {
        	if (!comp[p] && p-map[p] > 0 && comp[p-map[p]]) {
        		int tmpCount = count+1;
        		int tmpP = p-map[p];
        		while(tmpP != N) {
        			tmpCount++;
        			tmpP += map[tmpP];
        		}
        		
        		min = Math.min(min, tmpCount);
        	}
        	
        	count++;
        	if (p+map[p] == N) {
        		min = Math.min(min, count);
        	}
        	p += map[p];
        }
        
        System.out.println(min == Integer.MAX_VALUE ? -1 : min);
    }
}

0