結果

問題 No.103 素因数ゲーム リターンズ
ユーザー ぴろずぴろず
提出日時 2014-12-21 12:17:21
言語 Java21
(openjdk 21)
結果
AC  
実行時間 454 ms / 5,000 ms
コード長 2,337 bytes
コンパイル時間 2,434 ms
コンパイル使用メモリ 78,488 KB
実行使用メモリ 59,352 KB
最終ジャッジ日時 2023-09-02 20:39:51
合計ジャッジ時間 15,388 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 442 ms
58,464 KB
testcase_01 AC 436 ms
58,592 KB
testcase_02 AC 444 ms
58,584 KB
testcase_03 AC 439 ms
58,440 KB
testcase_04 AC 440 ms
58,672 KB
testcase_05 AC 441 ms
58,884 KB
testcase_06 AC 440 ms
58,556 KB
testcase_07 AC 441 ms
58,748 KB
testcase_08 AC 440 ms
58,624 KB
testcase_09 AC 443 ms
58,884 KB
testcase_10 AC 445 ms
58,852 KB
testcase_11 AC 444 ms
58,288 KB
testcase_12 AC 444 ms
59,016 KB
testcase_13 AC 449 ms
58,756 KB
testcase_14 AC 454 ms
58,912 KB
testcase_15 AC 449 ms
58,808 KB
testcase_16 AC 445 ms
58,272 KB
testcase_17 AC 449 ms
58,520 KB
testcase_18 AC 447 ms
58,608 KB
testcase_19 AC 453 ms
58,568 KB
testcase_20 AC 451 ms
58,852 KB
testcase_21 AC 447 ms
59,196 KB
testcase_22 AC 445 ms
59,352 KB
testcase_23 AC 446 ms
58,528 KB
testcase_24 AC 452 ms
58,828 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package no002;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		ArrayList<Integer> primeList = Sieve.primeList(10000);
		int[] grundy = new int[10001];
		Arrays.fill(grundy, -1);
		grundy[0] = grundy[1] = 0;
		for(int i=2;i<=10000;i++) {
			ArrayList<Sieve.Factor> factor = Sieve.primeFactor(primeList, i);
			HashSet<Integer> hs = new HashSet<>();
			for(Sieve.Factor f:factor) {
				hs.add(grundy[i/f.base]);
				if (f.exp >= 2) {
					hs.add(grundy[i/f.base/f.base]);
				}
			}
			PriorityQueue<Integer> pq = new PriorityQueue<>(hs);
			for(int j=0;j<=10000;j++) {
				if (pq.isEmpty() || pq.poll() != j) {
					grundy[i] = j;
					break;
				}
			}
		}
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int ans = 0;
		for(int i=0;i<n;i++) {
			int m = sc.nextInt();
			ans ^= grundy[m];
		}
		if (ans == 0) {
			System.out.println("Bob");
		}else{
			System.out.println("Alice");
		}
	}

}
class Sieve {
	public static boolean[] isPrimeArray(int max) {
		boolean[] isPrime = new boolean[max+1];
		Arrays.fill(isPrime, true);
		isPrime[0] = isPrime[1] = false;
		for(int i=2;i*i<=max;i++) {
			if (isPrime[i]) {
				int j = i * 2;
				while(j<=max) {
					isPrime[j] = false;
					j += i;
				}
			}
		}
		return isPrime;
	}
	public static ArrayList<Integer> primeList(int max) {
		boolean[] isPrime = isPrimeArray(max);
		ArrayList<Integer> primeList = new ArrayList<>();
		for(int i=2;i<=max;i++) {
			if (isPrime[i]) {
				primeList.add(i);
			}
		}
		return primeList;
	}
	public static ArrayList<Factor> primeFactor(ArrayList<Integer> primeList,long num) {
		ArrayList<Factor> ret = new ArrayList<Factor>();
		for(int p:primeList) {
			int exp = 0;
			while(num % p == 0) {
				num /= p;
				exp++;
			}
			if (exp > 0) {
				ret.add(new Factor(p,exp));
			}
		}
		if (num >= 2) {
			ret.add(new Factor((int) num,1));
		}
		return ret;
	}
	public static ArrayList<Factor> primeFactor(long num) {
		return primeFactor(primeList((int) (Math.sqrt(num) + 0.5)), num);
	}
	public static class Factor {
		int base,exp;
		public Factor(int base,int exp) {
			this.base = base;
			this.exp = exp;
		}
		public String toString() {
			return base + "^" + exp;
		}
	}
}
0