結果

問題 No.2 素因数ゲーム
ユーザー fujisufujisu
提出日時 2015-02-02 20:30:04
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,485 ms / 5,000 ms
コード長 4,123 bytes
コンパイル時間 2,066 ms
コンパイル使用メモリ 80,176 KB
実行使用メモリ 137,520 KB
最終ジャッジ日時 2024-06-07 23:52:56
合計ジャッジ時間 50,219 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,485 ms
137,116 KB
testcase_01 AC 1,477 ms
137,380 KB
testcase_02 AC 1,469 ms
137,380 KB
testcase_03 AC 1,469 ms
137,460 KB
testcase_04 AC 1,450 ms
137,212 KB
testcase_05 AC 1,445 ms
137,112 KB
testcase_06 AC 1,456 ms
137,112 KB
testcase_07 AC 1,464 ms
137,416 KB
testcase_08 AC 1,469 ms
137,520 KB
testcase_09 AC 1,460 ms
137,520 KB
testcase_10 AC 1,458 ms
137,376 KB
testcase_11 AC 1,461 ms
137,392 KB
testcase_12 AC 1,467 ms
137,368 KB
testcase_13 AC 1,452 ms
137,464 KB
testcase_14 AC 1,455 ms
137,376 KB
testcase_15 AC 1,458 ms
137,364 KB
testcase_16 AC 1,451 ms
137,368 KB
testcase_17 AC 1,464 ms
137,460 KB
testcase_18 AC 1,461 ms
137,392 KB
testcase_19 AC 1,453 ms
137,080 KB
testcase_20 AC 1,461 ms
137,384 KB
testcase_21 AC 1,483 ms
137,060 KB
testcase_22 AC 1,481 ms
137,216 KB
testcase_23 AC 1,460 ms
137,112 KB
testcase_24 AC 1,481 ms
137,164 KB
testcase_25 AC 1,470 ms
137,384 KB
testcase_26 AC 1,454 ms
137,252 KB
testcase_27 AC 1,465 ms
137,416 KB
testcase_28 AC 1,459 ms
137,428 KB
testcase_29 AC 1,452 ms
137,320 KB
testcase_30 AC 1,445 ms
137,296 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.List;
import java.util.TreeMap;

public class Main {
	boolean[] prime;
	TreeMap<int[], Integer> map;

	int dp(int[] a) {
		if (a.length == 1) {
			return 1;
		}
		if (map.containsKey(a)) {
			return map.get(a);
		}

		int n = a.length;
		int res = -1;
		for (int i = 0; i < n; i++) {
			if (i != 0 && a[i - 1] == a[i])
				continue;
			for (int j = 1; j <= a[i]; j++) {
				int[] next = reflesh(a, i, j);
				if (dp(next) == -1) {
					res = 1;
				}
			}
		}

		map.put(a, res);
		return res;
	}

	int[] reflesh(int[] a, int k, int t) {
		int n = a.length;
		if (a[k] == t)
			n--;
		int[] res = new int[n];
		int id = 0;
		for (int i = 0; i < a.length; i++) {
			if (i != k) {
				res[id++] = a[i];
			} else if (0 < a[i] - t) {
				res[id++] = a[i] - t;
			}
		}
		return res;
	}

	void run() {

		MyScanner sc = new MyScanner();
		int MAX = 100_000_000 + 1;
		prime = new boolean[MAX];
		Arrays.fill(prime, true);
		prime[0] = prime[1] = false;
		for (int i = 0; i * i <= MAX; i++) {
			if (prime[i]) {
				for (int j = i * i; j < MAX; j += i) {
					prime[j] = false;
				}
			}
		}

		List<Integer> list = new LinkedList<Integer>();
		for (int i = 0; i * i <= MAX; i++) {
			if (prime[i]) {
				list.add(i);
			}
		}

		int n = sc.nextInt();
		List<Integer> div = new LinkedList<Integer>();
		for (Integer I : list) {
			int cnt = 0;
			while (n % I == 0) {
				cnt++;
				n /= I;
			}
			if (0 < cnt) {
				div.add(cnt);
			}
			if (n == 1) {
				break;
			}
		}
		if (1 < n) {
			div.add(1);
		}

		int[] s = new int[div.size()];
		int id = 0;
		for (Integer I : div) {
			s[id++] = I;
		}
		Arrays.sort(s);
		map = new TreeMap<int[], Integer>(new Comparator<int[]>() {
			@Override
			public int compare(int[] o1, int[] o2) {
				int n = o1.length;
				if (n != o2.length) {
					return n - o2.length;
				}
				for (int i = 0; i < n; i++) {
					if (o1[i] != o2[i]) {
						return o1[i] - o2[i];
					}
				}
				return 0;
			}
		});
		System.out.println(dp(s) > 0 ? "Alice" : "Bob");
	}

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

	public void mapDebug(int[][] a) {
		System.out.println("--------map display---------");
		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < a[i].length; j++) {
				System.out.printf("%3d ", a[i][j]);
			}
			System.out.println();
		}
		System.out.println("----------------------------" + '\n');
	}

	class MyScanner {
		int read() {
			try {
				return System.in.read();
			} catch (IOException e) {
				throw new InputMismatchException();
			}
		}

		boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		boolean isEndline(int c) {
			return c == '\n' || c == '\r' || c == -1;
		}

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

		int[] nextIntArray(int n) {
			int[] array = new int[n];
			for (int i = 0; i < n; i++)
				array[i] = nextInt();
			return array;
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		long[] nextLongArray(int n) {
			long[] array = new long[n];
			for (int i = 0; i < n; i++)
				array[i] = nextLong();
			return array;
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		double[] nextDoubleArray(int n) {
			double[] array = new double[n];
			for (int i = 0; i < n; i++)
				array[i] = nextDouble();
			return array;
		}

		String next() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		String[] nextStringArray(int n) {
			String[] array = new String[n];
			for (int i = 0; i < n; i++)
				array[i] = next();

			return array;
		}

		String nextLine() {
			int c = read();
			while (isEndline(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isEndline(c));
			return res.toString();
		}
	}
}
0