結果

問題 No.3 ビットすごろく
ユーザー mitsuomitsuo
提出日時 2016-05-05 17:33:46
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,218 bytes
コンパイル時間 2,450 ms
コンパイル使用メモリ 81,044 KB
実行使用メモリ 63,776 KB
最終ジャッジ日時 2024-04-15 13:19:20
合計ジャッジ時間 20,869 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 129 ms
54,060 KB
testcase_01 AC 129 ms
54,104 KB
testcase_02 AC 129 ms
54,304 KB
testcase_03 AC 357 ms
62,892 KB
testcase_04 AC 197 ms
56,984 KB
testcase_05 AC 509 ms
63,608 KB
testcase_06 AC 376 ms
63,536 KB
testcase_07 AC 301 ms
60,812 KB
testcase_08 AC 413 ms
63,068 KB
testcase_09 AC 637 ms
63,640 KB
testcase_10 AC 754 ms
63,440 KB
testcase_11 AC 560 ms
63,212 KB
testcase_12 AC 487 ms
63,596 KB
testcase_13 AC 357 ms
63,156 KB
testcase_14 AC 728 ms
63,592 KB
testcase_15 AC 954 ms
63,224 KB
testcase_16 WA -
testcase_17 AC 975 ms
63,632 KB
testcase_18 AC 329 ms
62,960 KB
testcase_19 AC 991 ms
63,456 KB
testcase_20 AC 162 ms
55,624 KB
testcase_21 AC 128 ms
54,156 KB
testcase_22 AC 741 ms
63,656 KB
testcase_23 AC 981 ms
63,248 KB
testcase_24 AC 975 ms
63,544 KB
testcase_25 AC 949 ms
63,480 KB
testcase_26 AC 128 ms
54,332 KB
testcase_27 AC 362 ms
63,364 KB
testcase_28 AC 854 ms
63,336 KB
testcase_29 AC 574 ms
63,576 KB
testcase_30 AC 137 ms
54,324 KB
testcase_31 AC 152 ms
54,260 KB
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

package jp.fedom.challange.yuki.l2.q3;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		List<String> input = new ArrayList<>();
		while (sc.hasNext()) {
			input.add(sc.nextLine());
		}

		System.out.println(solve(input));

		sc.close();
	}

	public static int solve(List<String> in) {
		int N = Integer.valueOf(in.get(0));
		Map<Integer, Integer> av = new HashMap<Integer, Integer>();

		av.put(1, 1);
		for (int i = 1; i <= N; i++) {
//			 System.out.println(av + " / " + i);
			int bit = BigInteger.valueOf(i).bitCount();
			if (!av.containsKey(i)) {
				continue;
			}

			int minD = av.get(i);

			if (i + bit <= N) {
				if (!av.containsKey(i + bit)) {
					av.put(i + bit, minD + 1);
				}
				av.put(i + bit, Math.min(minD + 1, av.get(i + bit)));
			}

			if (i - bit >= 1) {
				if (!av.containsKey(i - bit)) {
					av.put(i - bit, minD + 1);
					i = 1;
					continue;
				}
				av.put(i - bit, Math.min(minD + 1, av.get(i - bit)));
			}
		}
		

		return av.containsKey(N) ? av.get(N) : -1;
	}

}
0