結果

問題 No.2 素因数ゲーム
ユーザー maruyuki95maruyuki95
提出日時 2020-05-29 12:47:29
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,512 bytes
コンパイル時間 5,699 ms
コンパイル使用メモリ 87,864 KB
実行使用メモリ 53,696 KB
最終ジャッジ日時 2024-04-23 00:50:46
合計ジャッジ時間 10,935 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 103 ms
41,456 KB
testcase_01 AC 101 ms
40,184 KB
testcase_02 AC 112 ms
41,392 KB
testcase_03 AC 113 ms
41,444 KB
testcase_04 AC 112 ms
41,048 KB
testcase_05 AC 119 ms
41,744 KB
testcase_06 AC 120 ms
41,344 KB
testcase_07 AC 122 ms
41,292 KB
testcase_08 AC 134 ms
40,556 KB
testcase_09 AC 120 ms
41,276 KB
testcase_10 AC 108 ms
40,288 KB
testcase_11 AC 114 ms
41,472 KB
testcase_12 AC 114 ms
41,572 KB
testcase_13 AC 113 ms
41,252 KB
testcase_14 AC 106 ms
41,568 KB
testcase_15 AC 104 ms
41,428 KB
testcase_16 AC 100 ms
41,588 KB
testcase_17 AC 109 ms
41,224 KB
testcase_18 AC 116 ms
41,472 KB
testcase_19 AC 283 ms
41,848 KB
testcase_20 AC 240 ms
41,464 KB
testcase_21 AC 447 ms
41,448 KB
testcase_22 AC 131 ms
41,468 KB
testcase_23 AC 118 ms
41,520 KB
testcase_24 AC 113 ms
41,276 KB
testcase_25 AC 109 ms
41,708 KB
testcase_26 AC 111 ms
41,440 KB
testcase_27 WA -
testcase_28 AC 120 ms
40,036 KB
testcase_29 AC 109 ms
41,728 KB
testcase_30 AC 110 ms
41,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {

	private static final String FIRST_PLAYER_NAME = "Alice";
	private static final String SECOND_PLAYER_NAME = "Bob";
	private static final int MIN_PRIME = 2;

	public static void main(String[] args) {
		new Main().execute();
	}

	private void execute() {
		int number = loadInputValue();
		String winnerName = judgeWinner(number);
		outputResult(winnerName);
	}

	String judgeWinner(int number) {
		Map<Integer, Integer> primeFactorizationMap = calculatePrimeFactorization(number, MIN_PRIME);

		if (isFirstPlayerWinner(primeFactorizationMap)) {
			return FIRST_PLAYER_NAME;
		}
		return SECOND_PLAYER_NAME;
	}

	/**
	 * 素因数分解する
	 * @param number	素因数分解する数	(e.g.)24
	 * @param minCheckingPrime	素因数かどうかチェックし始める素数の最小値	(e.g.)2
	 * @return keyに素因数、valueにその指数を保持するMap	(e.g.) {2=3, 3=1}
	 */
	private Map<Integer, Integer> calculatePrimeFactorization(int number, int minCheckingPrime) {
		if (number == 1) {
			return new HashMap<Integer, Integer>();
		}

		for (int i = minCheckingPrime; i <= number; i++) {
			if (number % i == 0) {
				Map<Integer, Integer> primeFactorizationMap = calculatePrimeFactorization(number / i, i);

				Integer exponent = primeFactorizationMap.get(i);
				if (exponent == null) {
					exponent = 0;
				}
				primeFactorizationMap.put(i, ++exponent);
				return primeFactorizationMap;
			}

		}
		throw new IllegalStateException("対象の数値「" + number + "」には約数が存在しません。");
	}

	private boolean isFirstPlayerWinner(Map<Integer, Integer> primeFactorizationMap) {
		return isFirstPlayerWinnerRecursively(new ArrayList<Integer>(primeFactorizationMap.values()));
	}

	private boolean isFirstPlayerWinnerRecursively(List<Integer> exponents) {
		if (exponents.size() == 0) {
			return false;
		}
		if (exponents.size() == 1) {
			return true;
		}

		Integer exponentValue1 = exponents.remove(0);
		Integer exponentValue2 = exponents.remove(0);
		if (exponentValue1 != exponentValue2) {
			exponents.add(Math.abs(exponentValue1 - exponentValue2));
		}
		return isFirstPlayerWinnerRecursively(exponents);
	}

	@SuppressWarnings("resource")
	private int loadInputValue() {
		Scanner sc = new Scanner(System.in);
		return sc.nextInt();
	}

	private void outputResult(String result) {
		System.out.println(result);
	}

}
0