結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 135 ms
54,104 KB
testcase_01 AC 141 ms
54,160 KB
testcase_02 AC 139 ms
54,072 KB
testcase_03 AC 148 ms
54,336 KB
testcase_04 AC 139 ms
54,040 KB
testcase_05 AC 148 ms
54,356 KB
testcase_06 AC 150 ms
54,140 KB
testcase_07 AC 142 ms
54,420 KB
testcase_08 AC 158 ms
54,104 KB
testcase_09 AC 172 ms
54,200 KB
testcase_10 AC 169 ms
54,360 KB
testcase_11 AC 165 ms
54,392 KB
testcase_12 AC 144 ms
54,076 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 167 ms
54,324 KB
testcase_17 AC 175 ms
54,176 KB
testcase_18 AC 141 ms
54,412 KB
testcase_19 AC 175 ms
54,240 KB
testcase_20 AC 136 ms
54,368 KB
testcase_21 AC 130 ms
54,084 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 174 ms
54,180 KB
testcase_25 AC 173 ms
54,236 KB
testcase_26 WA -
testcase_27 AC 152 ms
54,272 KB
testcase_28 AC 166 ms
54,436 KB
testcase_29 AC 154 ms
54,220 KB
testcase_30 AC 133 ms
54,176 KB
testcase_31 AC 132 ms
53,876 KB
testcase_32 AC 151 ms
53,992 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
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];
        	}
        }
        
        boolean bad = true;
        for (boolean flg : comp) {
        	if(flg)
        		bad = false;
        }
        
        if (bad) {
        	System.out.println(-1);
        	return;
        }
        
        int[] dp = new int[N+1];
        Arrays.fill(dp, Integer.MAX_VALUE-500);
        int count = 0;
        for (int i = 1; i <= N; i+=map[i]) {
        	count++;
        	dp[i] = count;
        	
        }
        
        boolean changed = true;
        while(changed) {
        	changed = false;
        	for (int i = 1; i <=N; i++) {
        		if (i-map[i] > 0 && dp[i-map[i]] > dp[i]+1) {
        			dp[i-map[i]] = dp[i]+1;
        			changed = true;
        		}
        		
        		if (i+map[i] <= N && dp[i+map[i]] > dp[i]+1) {
        			dp[i+map[i]] = dp[i]+1;
        			changed = true;
        		}
        	}
        }
        
        System.out.println(dp[N]);
    }
}


0