結果

問題 No.2 素因数ゲーム
ユーザー fujisufujisu
提出日時 2015-02-02 20:30:04
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,565 ms / 5,000 ms
コード長 4,123 bytes
コンパイル時間 2,373 ms
コンパイル使用メモリ 78,148 KB
実行使用メモリ 151,980 KB
最終ジャッジ日時 2023-08-27 03:46:19
合計ジャッジ時間 50,482 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,460 ms
151,440 KB
testcase_01 AC 1,449 ms
151,120 KB
testcase_02 AC 1,453 ms
151,348 KB
testcase_03 AC 1,461 ms
151,336 KB
testcase_04 AC 1,456 ms
151,544 KB
testcase_05 AC 1,475 ms
151,444 KB
testcase_06 AC 1,477 ms
151,332 KB
testcase_07 AC 1,450 ms
151,352 KB
testcase_08 AC 1,464 ms
151,300 KB
testcase_09 AC 1,468 ms
151,112 KB
testcase_10 AC 1,471 ms
151,240 KB
testcase_11 AC 1,472 ms
151,300 KB
testcase_12 AC 1,466 ms
151,544 KB
testcase_13 AC 1,469 ms
151,316 KB
testcase_14 AC 1,466 ms
151,264 KB
testcase_15 AC 1,434 ms
151,300 KB
testcase_16 AC 1,450 ms
151,104 KB
testcase_17 AC 1,447 ms
151,296 KB
testcase_18 AC 1,424 ms
151,980 KB
testcase_19 AC 1,565 ms
151,224 KB
testcase_20 AC 1,447 ms
151,268 KB
testcase_21 AC 1,446 ms
151,692 KB
testcase_22 AC 1,440 ms
151,744 KB
testcase_23 AC 1,448 ms
149,540 KB
testcase_24 AC 1,455 ms
151,740 KB
testcase_25 AC 1,453 ms
151,344 KB
testcase_26 AC 1,472 ms
151,736 KB
testcase_27 AC 1,483 ms
151,296 KB
testcase_28 AC 1,476 ms
151,392 KB
testcase_29 AC 1,464 ms
151,584 KB
testcase_30 AC 1,470 ms
151,268 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