結果

問題 No.3 ビットすごろく
ユーザー zimphazimpha
提出日時 2017-03-23 11:55:39
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,601 bytes
コンパイル時間 2,455 ms
コンパイル使用メモリ 74,728 KB
実行使用メモリ 51,252 KB
最終ジャッジ日時 2023-09-19 08:47:13
合計ジャッジ時間 6,251 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
49,628 KB
testcase_01 AC 47 ms
49,400 KB
testcase_02 AC 47 ms
49,596 KB
testcase_03 AC 50 ms
49,396 KB
testcase_04 AC 47 ms
50,268 KB
testcase_05 AC 51 ms
49,508 KB
testcase_06 AC 50 ms
49,484 KB
testcase_07 AC 49 ms
49,520 KB
testcase_08 AC 50 ms
49,524 KB
testcase_09 AC 52 ms
50,376 KB
testcase_10 AC 52 ms
49,780 KB
testcase_11 AC 52 ms
49,904 KB
testcase_12 AC 53 ms
49,496 KB
testcase_13 AC 49 ms
49,452 KB
testcase_14 AC 54 ms
50,536 KB
testcase_15 AC 58 ms
50,928 KB
testcase_16 AC 57 ms
51,252 KB
testcase_17 AC 57 ms
50,744 KB
testcase_18 AC 48 ms
49,420 KB
testcase_19 AC 55 ms
50,192 KB
testcase_20 AC 46 ms
49,484 KB
testcase_21 AC 45 ms
49,608 KB
testcase_22 AC 52 ms
49,872 KB
testcase_23 AC 57 ms
50,328 KB
testcase_24 AC 55 ms
50,836 KB
testcase_25 AC 54 ms
50,852 KB
testcase_26 AC 45 ms
49,600 KB
testcase_27 AC 49 ms
49,888 KB
testcase_28 WA -
testcase_29 AC 52 ms
49,540 KB
testcase_30 AC 45 ms
49,616 KB
testcase_31 AC 45 ms
49,528 KB
testcase_32 AC 52 ms
49,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Collection;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Queue;
import java.io.BufferedReader;
import java.util.LinkedList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Chiaki.Hoshinomori
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task003 solver = new Task003();
        solver.solve(1, in, out);
        out.close();
    }

    static class Task003 {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int[] dis = new int[n + 2];
            Arrays.fill(dis, -1);
            Queue<Integer> queue = new LinkedList<>();
            dis[1] = 1;
            queue.add(1);
            while (!queue.isEmpty()) {
                int u = queue.poll();
                int v = popcount(u);
                if (u - v > 0 && dis[u - v] == -1) {
                    dis[u - v] = dis[u] + 1;
                    queue.add(u - v);
                }
                if (u + v <= n + 1 && dis[u + v] == -1) {
                    dis[u + v] = dis[u] + 1;
                    queue.add(u + v);
                }
            }
            out.println(dis[n]);
        }

        private int popcount(int n) {
            int r = 0;
            while (n > 0) {
                r += n % 2;
                n >>= 1;
            }
            return r;
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}

0