結果
| 問題 |
No.1881 Everything is the same...
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-01-18 02:33:53 |
| 言語 | Java (openjdk 23) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 5,162 bytes |
| コンパイル時間 | 2,837 ms |
| コンパイル使用メモリ | 93,864 KB |
| 実行使用メモリ | 79,960 KB |
| 最終ジャッジ日時 | 2024-10-02 13:30:26 |
| 合計ジャッジ時間 | 24,304 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 41 WA * 11 |
ソースコード
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.NoSuchElementException;
class Main {
public static void main(String[] args) {
new Main().run();
}
int MAX=(int)1e5+1;
boolean[] isPrime=new boolean[MAX];
ArrayList<Integer> primes=new ArrayList<>();
ArrayList<List<Integer>>[] pe=new ArrayList[MAX];
{
for (int i=0;i<pe.length;++i) pe[i]=new ArrayList<>();
Arrays.fill(isPrime, true);
isPrime[0]=isPrime[1]=false;
for (int i=2;i<MAX;++i) {
if (!isPrime[i]) continue;
primes.add(i);
for (int j=i;j<MAX;j+=i) {
if (j>i) isPrime[j]=false;
int e=0;
int v=j;
while (v%i==0) {
v/=i;
++e;
}
pe[j].add(List.of(i, e));
}
}
}
int grundy(int n) {
HashMap<Integer, ArrayList<Integer>> decode=new HashMap<>();
for (var pair : pe[n]) {
int p=pair.get(0);
int e=pair.get(1);
if (p != 2) {
if (e >= 2) decode.computeIfAbsent(p, key -> new ArrayList<>()).add(e-1);
for (var pair2 : pe[p-1]) {
int p2=pair2.get(0);
int e2=pair2.get(1);
decode.computeIfAbsent(p2, key -> new ArrayList<>()).add(e2);
}
} else {
if (e == 2) {
decode.computeIfAbsent(p, key -> new ArrayList<>()).add(1);
} else if (e >= 3){
decode.computeIfAbsent(p, key -> new ArrayList<>()).add(e-2);
decode.computeIfAbsent(p, key -> new ArrayList<>()).add(1);
}
}
}
int g=0;
for (var entryset : decode.entrySet()) {
int[] key=new int[bits];
for (int i=0;i<entryset.getValue().size();++i) {
key[i]=entryset.getValue().get(i);
}
Arrays.sort(key);
g+=grundy.get(hash(key));
}
return g;
}
long pow(long a, long n) {
if (n==0) return 1;
return pow(a*a, n/2) * (n%2==1? a : 1) ;
}
long hash(int[] a) {
long ret=1;
for (int i=0;i<a.length;++i) {
ret*=pow(primes.get(i), a[a.length-1-i]);
}
return ret;
}
HashMap<Long, Integer> grundy=new HashMap<>();
int bits=16;
{
dfs1(new int[bits], bits, bits-1);
}
void dfs1(int[] a, int res, int pos) {
if (pos >= 0) {
int upper = pos+1 != a.length ? Math.min(res, a[pos+1]) : res;
for (int nxt=0; nxt <= upper;++nxt) {
a[pos]=nxt;
dfs1(a, res-nxt, pos-1);
}
} else {
int[] b=new int[a.length];
boolean[] vis=new boolean[17];
dfs2(a, b, vis, 0, false);
int g=0;
while (vis[g]) ++g;
grundy.put(hash(a), g);
}
}
void dfs2(int[] a, int[] b, boolean[] vis, int pos, boolean less) {
if (pos < a.length) {
for (int i= pos == 0 ? 0 : a[pos-1];i<=a[pos];++i) {
b[pos]=i;
dfs2(a, b, vis, pos+1, less | i < a[pos]);
}
} else {
long key=hash(b);
if (!less && grundy.containsKey(key)) throw new AssertionError();//ハッシュが衝突
if (less)
vis[grundy.get(key)]=true;
}
}
void run() {
solve();
}
void solve() {
FastScanner sc=new FastScanner();
int N=sc.nextInt();
int[] A=new int[N];
int g=0;
for (int i=0;i<N;++i) {
A[i]=sc.nextInt();
g^=grundy(A[i]);
}
System.out.println(g==0?"X":"Y");
}
static void tr(Object...objects) {
System.out.println(Arrays.deepToString(objects));
}
}
class FastScanner {
private final InputStream in = System.in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
}else{
ptr = 0;
try {
buflen = in.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;}
public boolean hasNext() { skipUnprintable(); return hasNextByte();}
public String next() {
if (!hasNext()) throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while(isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext()) throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while(true){
if ('0' <= b && b <= '9') {
n *= 10;
n += b - '0';
}else if(b == -1 || !isPrintableChar(b)){
return minus ? -n : n;
}else{
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
return (int)nextLong();
}
}