結果

問題 No.1881 Everything is the same...
ユーザー colognecologne
提出日時 2022-03-18 02:53:57
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,519 bytes
コンパイル時間 340 ms
コンパイル使用メモリ 81,848 KB
実行使用メモリ 82,532 KB
最終ジャッジ日時 2024-10-02 13:32:54
合計ジャッジ時間 14,782 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
権限があれば一括ダウンロードができます

ソースコード

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 C = prod(Cp^ai), backtrack for B / <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(G)
    print('Y' if G == 0 else 'X')


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