結果

問題 No.3 ビットすごろく
ユーザー katsumikatsumi
提出日時 2019-01-31 14:52:31
言語 Java21
(openjdk 21)
結果
AC  
実行時間 161 ms / 5,000 ms
コード長 1,629 bytes
コンパイル時間 2,060 ms
コンパイル使用メモリ 74,184 KB
実行使用メモリ 56,276 KB
最終ジャッジ日時 2023-09-14 01:10:39
合計ジャッジ時間 7,929 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 119 ms
55,788 KB
testcase_01 AC 121 ms
55,664 KB
testcase_02 AC 121 ms
55,748 KB
testcase_03 AC 131 ms
55,612 KB
testcase_04 AC 122 ms
55,848 KB
testcase_05 AC 132 ms
55,960 KB
testcase_06 AC 128 ms
55,584 KB
testcase_07 AC 128 ms
56,032 KB
testcase_08 AC 131 ms
55,724 KB
testcase_09 AC 145 ms
55,500 KB
testcase_10 AC 148 ms
56,276 KB
testcase_11 AC 144 ms
55,660 KB
testcase_12 AC 136 ms
55,836 KB
testcase_13 AC 130 ms
55,748 KB
testcase_14 AC 149 ms
55,524 KB
testcase_15 AC 157 ms
55,856 KB
testcase_16 AC 152 ms
55,336 KB
testcase_17 AC 156 ms
55,564 KB
testcase_18 AC 133 ms
55,928 KB
testcase_19 AC 161 ms
55,892 KB
testcase_20 AC 124 ms
55,988 KB
testcase_21 AC 120 ms
55,948 KB
testcase_22 AC 146 ms
55,612 KB
testcase_23 AC 154 ms
56,068 KB
testcase_24 AC 157 ms
55,684 KB
testcase_25 AC 154 ms
55,596 KB
testcase_26 AC 117 ms
56,000 KB
testcase_27 AC 129 ms
55,792 KB
testcase_28 AC 150 ms
56,116 KB
testcase_29 AC 142 ms
56,136 KB
testcase_30 AC 119 ms
55,948 KB
testcase_31 AC 121 ms
56,100 KB
testcase_32 AC 139 ms
55,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(1);
        	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] == Integer.MAX_VALUE-500 ? -1 : dp[N]);
    }
}


0