結果

問題 No.1881 Everything is the same...
ユーザー 👑 colognecologne
提出日時 2022-03-18 02:59:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 375 ms / 2,000 ms
コード長 2,510 bytes
コンパイル時間 757 ms
コンパイル使用メモリ 82,520 KB
実行使用メモリ 82,748 KB
最終ジャッジ日時 2024-04-10 11:34:49
合計ジャッジ時間 14,209 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
56,244 KB
testcase_01 AC 45 ms
56,748 KB
testcase_02 AC 45 ms
56,128 KB
testcase_03 AC 45 ms
56,276 KB
testcase_04 AC 45 ms
56,596 KB
testcase_05 AC 50 ms
56,840 KB
testcase_06 AC 185 ms
78,932 KB
testcase_07 AC 45 ms
57,236 KB
testcase_08 AC 46 ms
56,284 KB
testcase_09 AC 46 ms
55,796 KB
testcase_10 AC 340 ms
80,892 KB
testcase_11 AC 328 ms
81,384 KB
testcase_12 AC 351 ms
81,448 KB
testcase_13 AC 356 ms
81,076 KB
testcase_14 AC 373 ms
81,908 KB
testcase_15 AC 366 ms
81,676 KB
testcase_16 AC 358 ms
81,612 KB
testcase_17 AC 341 ms
80,808 KB
testcase_18 AC 350 ms
81,420 KB
testcase_19 AC 375 ms
82,748 KB
testcase_20 AC 367 ms
82,068 KB
testcase_21 AC 358 ms
80,664 KB
testcase_22 AC 356 ms
81,124 KB
testcase_23 AC 364 ms
80,900 KB
testcase_24 AC 355 ms
81,012 KB
testcase_25 AC 348 ms
81,124 KB
testcase_26 AC 350 ms
81,344 KB
testcase_27 AC 359 ms
81,116 KB
testcase_28 AC 349 ms
80,848 KB
testcase_29 AC 340 ms
80,984 KB
testcase_30 AC 360 ms
82,636 KB
testcase_31 AC 354 ms
81,872 KB
testcase_32 AC 341 ms
81,164 KB
testcase_33 AC 371 ms
82,120 KB
testcase_34 AC 347 ms
81,440 KB
testcase_35 AC 362 ms
82,292 KB
testcase_36 AC 358 ms
80,940 KB
testcase_37 AC 368 ms
81,100 KB
testcase_38 AC 353 ms
81,996 KB
testcase_39 AC 365 ms
82,000 KB
testcase_40 AC 369 ms
81,916 KB
testcase_41 AC 344 ms
80,868 KB
testcase_42 AC 45 ms
56,132 KB
testcase_43 AC 45 ms
55,868 KB
testcase_44 AC 45 ms
56,108 KB
testcase_45 AC 46 ms
56,860 KB
testcase_46 AC 45 ms
56,380 KB
testcase_47 AC 45 ms
56,452 KB
testcase_48 AC 45 ms
56,216 KB
testcase_49 AC 45 ms
55,780 KB
testcase_50 AC 45 ms
56,896 KB
testcase_51 AC 45 ms
57,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from functools import reduce
from itertools import count
from operator import xor


def factorize(N):
    ret = []
    for p in count(2):
        if p*p > N:
            break
        if N % p == 0:
            k = 0
            while N % p == 0:
                k += 1
                N //= p
            ret.append((p, k))
    if N != 1:
        ret.append((N, 1))
    return ret


memo = {}


def bktk(A):
    if len(A) == 0:
        yield []
        return

    for i in range(A[-1]):
        for a in bktk(A[:-1]):
            a.append(i)
            yield a
    return


def calc(A):
    # Calculate grundy number of cyclic group, prod(Cp^a)
    # A should be sorted
    if A in memo:
        return memo[A]
    if len(A) == 0:
        return 0

    grundy = set()

    # for group G = prod(Cp^ai), backtrack for B = G / <x>
    # B = prod(Cp^bi), then a_{i-1} <= b_i <= a_i holds.
    # this means, 0 <= ai-bi <= a_i-a_{i-1}
    cA = [1 + A[i] - (0 if i == 0 else A[i-1]) for i in range(len(A))]
    for dA in bktk(cA):
        if max(dA) == 0:
            continue
        B = tuple(a-da for a, da in zip(A, dA) if a != da)
        grundy.add(calc(B))

    for i in count(0):
        if i not in grundy:
            memo[A] = i
            return i


def solve(x):
    # express G as product of cyclic group
    # C[p] = [a, b, ...] denotes (Z/xZ)* = Cp^a*Cp^b*...
    C = {}
    for p, k in factorize(x):
        if p not in C:
            C[p] = []
        if p == 2:
            if k >= 2:
                C[p].append(1)
            if k >= 3:
                C[p].append(k-2)
        else:
            if k >= 2:
                C[p].append(k-1)
            for q, t in factorize(p-1):
                if q not in C:
                    C[q] = []
                C[q].append(t)

    # simple transpose algorithm
    def T(arr):
        if len(arr) == 0:
            return ()
        ret = [0] * max(arr)
        for i in arr:
            for j in range(i):
                ret[j] += 1
        return tuple(ret)

    # Here, uses transpose trick.
    # grundy number of cyclic group quotient can be transposed into game where
    # arbitary positive number of decks were chosen, and -1 from chosen numbers
    part = []
    for v in C.values():
        part.extend(T(v))
    part = T(part)

    return calc(part[::-1])


def main():
    int(input())  # N
    A = map(int, input().split())
    G = reduce(xor, map(solve, A))
    print('X' if G == 0 else 'Y')


if __name__ == '__main__':
    main()
0