結果

問題 No.3 ビットすごろく
ユーザー actu3actu3
提出日時 2015-06-20 14:36:36
言語 Java21
(openjdk 21)
結果
AC  
実行時間 118 ms / 5,000 ms
コード長 2,046 bytes
コンパイル時間 4,637 ms
コンパイル使用メモリ 74,748 KB
実行使用メモリ 53,264 KB
最終ジャッジ日時 2023-09-13 23:15:52
合計ジャッジ時間 7,385 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
49,348 KB
testcase_01 AC 45 ms
49,540 KB
testcase_02 AC 45 ms
49,456 KB
testcase_03 AC 61 ms
50,936 KB
testcase_04 AC 56 ms
50,876 KB
testcase_05 AC 73 ms
51,288 KB
testcase_06 AC 60 ms
50,952 KB
testcase_07 AC 58 ms
50,980 KB
testcase_08 AC 68 ms
51,216 KB
testcase_09 AC 84 ms
51,156 KB
testcase_10 AC 99 ms
51,832 KB
testcase_11 AC 81 ms
51,284 KB
testcase_12 AC 74 ms
51,252 KB
testcase_13 AC 58 ms
51,024 KB
testcase_14 AC 95 ms
51,340 KB
testcase_15 AC 116 ms
51,292 KB
testcase_16 AC 108 ms
51,480 KB
testcase_17 AC 115 ms
51,636 KB
testcase_18 AC 59 ms
51,080 KB
testcase_19 AC 118 ms
53,264 KB
testcase_20 AC 55 ms
50,956 KB
testcase_21 AC 45 ms
49,336 KB
testcase_22 AC 96 ms
51,344 KB
testcase_23 AC 117 ms
51,316 KB
testcase_24 AC 118 ms
51,644 KB
testcase_25 AC 116 ms
52,016 KB
testcase_26 AC 45 ms
49,428 KB
testcase_27 AC 60 ms
51,172 KB
testcase_28 AC 106 ms
51,680 KB
testcase_29 AC 83 ms
51,784 KB
testcase_30 AC 46 ms
49,424 KB
testcase_31 AC 46 ms
49,544 KB
testcase_32 AC 79 ms
51,032 KB
権限があれば一括ダウンロードができます

ソースコード

diff #


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

public class Yuki3 {

	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		Solver solver = new Solver();
		solver.solve(1, in, out);
		out.close();
	}
	
static class Solver {
	
	final int INF = Integer.MAX_VALUE;
    public void solve(int testNumber, InputReader in, PrintWriter out) {
    	n = in.nextInt();
    	moves = new int[n+1];
    	Arrays.fill(moves, INF);
    	moves[1] = 1;
    	
    	dfs(1, 1);
    	
    	int ans = moves[n] == INF ? -1 : moves[n];
    	
    	System.out.println(ans);
    	
    	
    	
    }
    
    int n;
    int[] moves;
    
    public void dfs(int cur, int move) {
    	int dist = Integer.bitCount(cur);
    	
    	int next = cur-dist;
    	if (0 < next) {
    		if (move+1 < moves[next]) {
    			moves[next] = move+1;
    			dfs(next, move+1);
    		}
    	}
    	next = cur+dist;
    	if (next <= n) {
    		if (move+1 < moves[next]) {
    			moves[next] = move+1;
    			dfs(next, move+1);
    		}
    	}
    	
    }
    
    
}

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