結果
| 問題 |
No.103 素因数ゲーム リターンズ
|
| コンテスト | |
| ユーザー |
ぴろず
|
| 提出日時 | 2014-12-21 12:17:21 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 401 ms / 5,000 ms |
| コード長 | 2,337 bytes |
| コンパイル時間 | 2,143 ms |
| コンパイル使用メモリ | 84,256 KB |
| 実行使用メモリ | 57,256 KB |
| 最終ジャッジ日時 | 2024-06-12 03:00:07 |
| 合計ジャッジ時間 | 12,860 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 20 |
ソースコード
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;
}
}
}
ぴろず