結果

問題 No.2152 [Cherry Anniversary 2] 19 Petals of Cherry
ユーザー tassei903tassei903
提出日時 2022-12-09 20:00:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 411 ms / 1,000 ms
コード長 2,603 bytes
コンパイル時間 345 ms
コンパイル使用メモリ 82,308 KB
実行使用メモリ 165,616 KB
最終ジャッジ日時 2024-10-14 18:49:45
合計ジャッジ時間 18,629 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 49
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
#input = lambda :sys.stdin.readline()[:-1]
ni = lambda :int(input())
na = lambda :list(map(int,input().split()))
yes = lambda :print("yes");Yes = lambda :print("Yes");YES = lambda : print("YES")
no = lambda :print("no");No = lambda :print("No");NO = lambda : print("NO")
#######################################################################
n = 19
c = [0] * n
for i in range(n):
    que = na()
    for j in que[1:]:
        c[i] += 1<<j-1

dp = [0] * (1<<n)
dp[0] = 1
for i in range(n):
    ndp = [0] * (1<<n)
    ndp, dp = dp, ndp
    for j in range(1<<n):
        if ndp[j]==0:
            continue
        for k in range(n):
            if j >> k & 1:
                continue
            dp[j+(1<<k)] += ndp[j] * (c[i] >> k & 1)
from fractions import Fraction
#2D matrix

def add(x,y):
    return x + y

def mul(x, y):
    return x * y


def mat_add(A, B, replace=False):
    assert len(A)==len(B) and len(A[0]) == len(B[0])
    if not replace:
        A = [a.copy() for a in A]
    n = len(A)
    m = len(A[0])
    for i in range(n):
        for j in range(m):
            A[i][j] = add(A[i][j], B[i][j])
    return A


def mat_mul(A,B):
    assert len(A[0]) == len(B)
    n = len(A)
    m = len(B[0])
    p = len(A[0])
    R = [[0 for j in range(m)]for i in range(n)]
    for i in range(n):
        for j in range(m):
            for k in range(p):
                R[i][j] = add(R[i][j],mul(A[i][k],B[k][j]))
    return R

def mat_pow(A, x):
    assert len(A)==len(A[0])
    n = len(A)
    R = [[0 for j in range(n)]for i in range(n)]
    while x > 0:
        if x&1:
            R = mat_mul(R, A)
        A = mat_mul(A,A)
        x >>= 1
    return R


def determinant(A, replace=False):
    if not replace:
        A = [a.copy() for a in A]
    n = len(A)
    res = 1
    for i, a_i in enumerate(A):
        if a_i[i] == 0:
            for j in range(i+1, n):
                if A[j][i]:
                    break
            else:
                return 0
            A[i], A[j] = A[j], A[i]
            a_i = A[i]
            res = -res
        inv = 1/a_i[i]
        for j in range(i+1, n):
            a_j = A[j]
            t = a_j[i] * inv 
            for k in range(i+1, n):
                a_j[k] -= t * a_i[k]
    for i in range(n):
        res *= A[i][i]
    return res

def mat_pri(A):
    for i in A:
        print(*i)

def fractionize(A):
    for i in range(len(A)):
        for j in range(len(A[0])):
            A[i][j] = Fraction(A[i][j])

A = [[Fraction(c[i]>>j&1) for j in range(n)]for i in range(n)]

D = determinant(A)
C = dp[-1]
print(int(C+D)//2,int(C-D)//2)
0