結果

問題 No.3 ビットすごろく
ユーザー mastersatoshimastersatoshi
提出日時 2015-08-01 10:01:21
言語 Java21
(openjdk 21)
結果
AC  
実行時間 58 ms / 5,000 ms
コード長 1,479 bytes
コンパイル時間 2,434 ms
コンパイル使用メモリ 74,748 KB
実行使用メモリ 50,792 KB
最終ジャッジ日時 2023-09-13 23:18:29
合計ジャッジ時間 5,623 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
49,116 KB
testcase_01 AC 46 ms
49,336 KB
testcase_02 AC 46 ms
49,432 KB
testcase_03 AC 49 ms
49,364 KB
testcase_04 AC 45 ms
49,284 KB
testcase_05 AC 50 ms
49,396 KB
testcase_06 AC 47 ms
49,356 KB
testcase_07 AC 45 ms
49,252 KB
testcase_08 AC 46 ms
49,296 KB
testcase_09 AC 53 ms
49,496 KB
testcase_10 AC 53 ms
50,492 KB
testcase_11 AC 50 ms
49,424 KB
testcase_12 AC 50 ms
49,328 KB
testcase_13 AC 45 ms
49,304 KB
testcase_14 AC 51 ms
50,792 KB
testcase_15 AC 55 ms
49,752 KB
testcase_16 AC 55 ms
49,976 KB
testcase_17 AC 55 ms
50,388 KB
testcase_18 AC 47 ms
49,468 KB
testcase_19 AC 58 ms
50,436 KB
testcase_20 AC 43 ms
49,292 KB
testcase_21 AC 42 ms
49,416 KB
testcase_22 AC 57 ms
50,520 KB
testcase_23 AC 57 ms
50,452 KB
testcase_24 AC 58 ms
50,728 KB
testcase_25 AC 58 ms
50,356 KB
testcase_26 AC 43 ms
47,220 KB
testcase_27 AC 48 ms
49,248 KB
testcase_28 AC 56 ms
50,164 KB
testcase_29 AC 53 ms
49,508 KB
testcase_30 AC 43 ms
47,392 KB
testcase_31 AC 44 ms
49,188 KB
testcase_32 AC 52 ms
49,280 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) throws Exception {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int N = Integer.parseInt(br.readLine());

        int[] cource = new int[N + 1];

        ArrayList<Integer> now = new ArrayList();

        now.add(1);
        cource[1] = 1;
        for (int i = 2; true; i++) {
            ArrayList<Integer> next = new ArrayList();

            for (int tmp : now) {
                if (tmp == N) {
                    System.out.println(cource[N]);
                    return;
                }
                int bit = bitCount(tmp);

                if (tmp + bit <= N && cource[tmp + bit] == 0) {
                    cource[tmp + bit] = i;
                    next.add(tmp + bit);
                }

                if (tmp + bit >= 1 && cource[tmp - bit] == 0) {
                    cource[tmp - bit] = i;
                    next.add(tmp - bit);
                }
            }
            if (next.isEmpty()) {
                System.out.println(-1);
                return;
            }

            now = (ArrayList<Integer>) next.clone();
        }
    }

    private static int bitCount(int source) {
        int count = 0;
        for (int i = 8192; source > 0; i = i / 2) {
            if (source >= i) {
                source -= i;
                count++;
            }
        }
        return count;
    }
}
0