結果

問題 No.2446 完全列
ユーザー 👑 KazunKazun
提出日時 2023-08-26 01:00:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 65 ms / 2,000 ms
コード長 13,321 bytes
コンパイル時間 407 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 68,352 KB
最終ジャッジ日時 2024-06-06 20:30:27
合計ジャッジ時間 2,817 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
56,960 KB
testcase_01 AC 49 ms
56,448 KB
testcase_02 AC 49 ms
57,344 KB
testcase_03 AC 50 ms
57,216 KB
testcase_04 AC 50 ms
57,088 KB
testcase_05 AC 52 ms
57,344 KB
testcase_06 AC 49 ms
56,576 KB
testcase_07 AC 48 ms
56,960 KB
testcase_08 AC 49 ms
57,216 KB
testcase_09 AC 49 ms
56,960 KB
testcase_10 AC 50 ms
57,088 KB
testcase_11 AC 49 ms
57,216 KB
testcase_12 AC 47 ms
56,704 KB
testcase_13 AC 47 ms
56,960 KB
testcase_14 AC 50 ms
57,088 KB
testcase_15 AC 49 ms
56,576 KB
testcase_16 AC 49 ms
56,832 KB
testcase_17 AC 48 ms
57,344 KB
testcase_18 AC 46 ms
57,088 KB
testcase_19 AC 48 ms
56,704 KB
testcase_20 AC 50 ms
57,600 KB
testcase_21 AC 49 ms
57,600 KB
testcase_22 AC 48 ms
57,344 KB
testcase_23 AC 50 ms
57,344 KB
testcase_24 AC 51 ms
57,984 KB
testcase_25 AC 64 ms
67,712 KB
testcase_26 AC 64 ms
68,096 KB
testcase_27 AC 65 ms
68,352 KB
testcase_28 AC 49 ms
56,704 KB
testcase_29 AC 49 ms
57,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from copy import deepcopy

class Modulo_Matrix():
    __slots__=("ele","row","col","size")

    #入力
    def __init__(self,M):
        """ 行列 M の定義

        M: 行列
        ※ Mod: 法はグローバル変数から指定
        """

        self.ele=[[x%Mod for x in X] for X in M]
        R=len(M)
        if R!=0:
            C=len(M[0])
        else:
            C=0
        self.row=R
        self.col=C
        self.size=(R,C)

    #出力
    def __str__(self):
        return "["+"\n".join(map(str,self.ele))+"]"

    def __repr__(self):
        return str(self)

    #+,-
    def __pos__(self):
        return self

    def __neg__(self):
        return self.__scale__(-1)

    #加法
    def __add__(self,other):
        M=self.ele; N=other.ele

        L=[[0]*self.col for _ in range(self.row)]
        for i in range(self.row):
            Li,Mi,Ni=L[i],M[i],N[i]
            for j in range(self.col):
                Li[j]=Mi[j]+Ni[j]
        return Modulo_Matrix(L)

    def __iadd__(self,other):
        M=self.ele; N=other.ele

        for i in range(self.row):
            Mi,Ni=M[i],N[i]
            for j in range(self.col):
                Mi[j]+=Ni[j]
                Mi[j]%=Mod
        return self

    #減法
    def __sub__(self,other):
        M=self.ele; N=other.ele

        L=[[0]*self.col for _ in range(self.row)]
        for i in range(self.row):
            Li,Mi,Ni=L[i],M[i],N[i]
            for j in range(self.col):
                Li[j]=Mi[j]-Ni[j]
        return Modulo_Matrix(L)

    def __isub__(self,other):
        M=self.ele; N=other.ele

        for i in range(self.row):
            Mi,Ni=M[i],N[i]
            for j in range(self.col):
                Mi[j]-=Ni[j]
                Mi[j]%=Mod
        return self

    #乗法
    def __mul__(self,other):
        if isinstance(other,Modulo_Matrix):
            assert self.col==other.row, "左側の列と右側の行が一致しません.({},{})".format(self.size,other.size)

            M=self.ele; N=other.ele
            E=[[0]*other.col for _ in range(self.row)]

            for i in range(self.row):
                Ei,Mi=E[i],M[i]
                for k in range(self.col):
                    m_ik,Nk=Mi[k],N[k]
                    for j in range(other.col):
                        Ei[j]+=m_ik*Nk[j]
                        Ei[j]%=Mod
            return Modulo_Matrix(E)
        elif isinstance(other,int):
            return self.__scale__(other)

    def __rmul__(self,other):
        if isinstance(other,int):
            return self.__scale__(other)

    def inverse(self):
        assert self.row==self.col,"正方行列ではありません."

        M=self
        N=M.row
        R=[[1 if i==j else 0 for j in range(N)] for i in range(N)]
        T=deepcopy(M.ele)

        for j in range(N):
            if T[j][j]==0:
                for i in range(j+1,N):
                    if T[i][j]:
                        break
                else:
                    assert 0, "正則行列ではありません"

                T[j],T[i]=T[i],T[j]
                R[j],R[i]=R[i],R[j]
            Tj,Rj=T[j],R[j]
            inv=pow(Tj[j], Mod-2, Mod)
            for k in range(N):
                Tj[k]*=inv; Tj[k]%=Mod
                Rj[k]*=inv; Rj[k]%=Mod
            for i in range(N):
                if i==j: continue
                c=T[i][j]
                Ti,Ri=T[i],R[i]
                for k in range(N):
                    Ti[k]-=Tj[k]*c; Ti[k]%=Mod
                    Ri[k]-=Rj[k]*c; Ri[k]%=Mod
        return Modulo_Matrix(R)

    #スカラー倍
    def __scale__(self,r):
        M=self.ele
        r%=Mod
        L=[[(r*M[i][j])%Mod for j in range(self.col)] for i in range(self.row)]
        return Modulo_Matrix(L)

    #累乗
    def __pow__(self,n):
        assert self.row==self.col, "正方行列ではありません."

        r=self.col

        def __mat_mul(A,B):
            E=[[0]*r for _ in range(r)]
            for i in range(r):
                a=A[i]; e=E[i]
                for k in range(r):
                    b=B[k]
                    for j in range(r):
                        e[j]+=a[k]*b[j]
                        e[j]%=Mod
            return E

        X=deepcopy(self.ele)
        E=[[1 if i==j else 0 for j in range(r)] for i in range(r)]

        sgn=1 if n>=0 else -1
        n=abs(n)

        while True:
            if n&1:
                E=__mat_mul(E,X)
            n>>=1
            if n:
                X=__mat_mul(X,X)
            else:
                break

        if sgn==1:
            return Modulo_Matrix(E)
        else:
            return Modulo_Matrix(E).inverse()

    #等号
    def __eq__(self,other):
        return self.ele==other.ele

    #不等号
    def __neq__(self,other):
        return not(self==other)

    #転置
    def transpose(self):
        return Modulo_Matrix(list(map(list,zip(*self.ele))))

    #行基本変形
    def row_reduce(self):
        M=self
        (R,C)=M.size
        T=[]

        for i in range(R):
            U=[]
            for j in range(C):
                U.append(M.ele[i][j])
            T.append(U)

        I=0
        for J in range(C):
            if T[I][J]==0:
                for i in range(I+1,R):
                    if T[i][J]!=0:
                        T[i],T[I]=T[I],T[i]
                        break

            if T[I][J]!=0:
                u=T[I][J]
                u_inv=pow(u, Mod-2, Mod)
                for j in range(C):
                    T[I][j]*=u_inv
                    T[I][j]%=Mod

                for i in range(R):
                    if i!=I:
                        v=T[i][J]
                        for j in range(C):
                            T[i][j]-=v*T[I][j]
                            T[i][j]%=Mod
                I+=1
                if I==R:
                    break

        return Modulo_Matrix(T)

    #列基本変形
    def column_reduce(self):
        M=self
        (R,C)=M.size

        T=[]
        for i in range(R):
            U=[]
            for j in range(C):
                U.append(M.ele[i][j])
            T.append(U)

        J=0
        for I in range(R):
            if T[I][J]==0:
                for j in range(J+1,C):
                    if T[I][j]!=0:
                        for k in range(R):
                            T[k][j],T[k][J]=T[k][J],T[k][j]
                        break

            if T[I][J]!=0:
                u=T[I][J]
                u_inv=pow(u, Mod-2, Mod)
                for i in range(R):
                    T[i][J]*=u_inv
                    T[i][J]%=Mod

                for j in range(C):
                    if j!=J:
                        v=T[I][j]
                        for i in range(R):
                            T[i][j]-=v*T[i][J]
                            T[i][j]%=Mod
                J+=1
                if J==C:
                    break

        return Modulo_Matrix(T)

    #行列の階数
    def rank(self):
        M=self.row_reduce()
        (R,C)=M.size
        T=M.ele

        rnk=0
        for i in range(R):
            f=False
            for j in range(C):
                if T[i][j]!=0:
                    f=True
                    break

            if f:
                rnk+=1
            else:
                break

        return rnk

    #行の結合
    def row_union(self,other):
        return Modulo_Matrix(self.ele+other.ele)

    #列の結合
    def column_union(self,other):
        E=[]
        for i in range(self.row):
            E.append(self.ele[i]+other.ele[i])

        return Modulo_Matrix(E)

    def __getitem__(self,index):
        if isinstance(index, int):
            return self.ele[index]
        else:
            return self.ele[index[0]][index[1]]

    def __setitem__(self,index,val):
        assert isinstance(index,tuple) and len(index)==2
        self.ele[index[0]][index[1]]=val

#==================================================
class Modulo_Vector:
    def __init__(self, vector):
        self.vec = [vi % Mod for vi in vector]
        self.size = len(vector)

        #出力
    def __str__(self):
        return str(self.vec)

    def __repr__(self):
        return str(self)

    def __bool__(self):
        return any(self.vec)

    def __iter__(self):
        yield from self.vec

    #+,-
    def __pos__(self):
        return self

    def __neg__(self):
        return self.__scale__(-1)

    #加法
    def __add__(self, other):
        assert self.size == other.size, f"2つのベクトルのサイズが異なります. ({self.size}, {other.size})"
        return Modulo_Vector([vi + wi for vi, wi in zip(self, other)])

    #減法
    def __sub__(self, other):
        return self+(-other)

    def __rsub__(self, other):
        return (-self)+other

    #乗法
    def __mul__(self,other):
        pass

    def __rmul__(self,other):
        return self.__scale__(other)

    #スカラー倍
    def __scale__(self, r):
        return Modulo_Vector([r * vi for vi in self])

    #内積
    def inner(self,other):
        assert self.size == other.size, f"2つのベクトルのサイズが異なります. ({self.size}, {other.size})"
        return sum(vi * wi % Mod for vi, wi in zip(self, other)) % Mod

    #累乗
    def __pow__(self,n):
        pass

    #等号
    def __eq__(self, other):
        return self.vec == other.vec

    def __len__(self):
        return self.size

    #不等号
    def __neq__(self, other):
        return not (self == other)

    def __getitem__(self,index):
        assert isinstance(index,int)
        return self.vec[index]

    def __setitem__(self,index,val):
        assert isinstance(index,int)
        self.vec[index]=val

class Modulo_Vector_Space:
    def __init__(self, dim):
        """ 次元が dim のベクトル空間の部分空間を生成する.

        """

        self.dim=dim
        self.basis=[]
        self.__ind=[]

    def __contains__(self, v):
        for i,u in zip(self.__ind, self.basis):
            v-=v[i]*u
        return not bool(v)

    def add_vectors(self, *S):
        for v in S:
            assert len(v)==self.dim
            for i,u in zip(self.__ind, self.basis):
                v-=v[i]*u

            if bool(v):
                for j in range(self.dim):
                    if v[j]:
                        self.__ind.append(j)
                        break
                v=pow(v[j], Mod-2, Mod) * v
                self.basis.append(v)

                for k in range(len(self.basis)-1):
                    self.basis[k]-=self.basis[k][j]*v

    def dimension(self):
        return len(self.basis)

    def __le__(self, other):
        for u in self.basis:
            if u not in other:
                return False
        return True

    def __ge__(self, other):
        return other<=self

    def __eq__(self, other):
        return (self<=other) and (other<=self)

    def refresh(self):
        I=sorted(range(len(self.__ind)), key=lambda i:self.__ind[i])
        self.basis=[self.basis[i] for i in I]
        self.__ind=[self.__ind[i] for i in I]

    def projection(self, v):
        for i,u in zip(self.__ind, self.basis):
            v-=v[i]*u
        return v

def Kernel_Space(A):
    """ 行列 A の核空間 Ker A (Ax=0 となる x の空間) を求める.

    """

    row,col=A.size
    T=deepcopy(A.ele)

    p=[]; q=[]
    rnk=0
    for j in range(col):
        for i in range(rnk,row):
            if T[i][j]!=0:
                break
        else:
            q.append(j)
            continue
        if j==col:
            return Modulo_Vector_Space(col)
        p.append(j)
        T[rnk],T[i]=T[i],T[rnk]

        inv=pow(T[rnk][j], Mod-2, Mod)
        for k in range(col):
            T[rnk][k]=(inv*T[rnk][k])%Mod

        for s in range(row):
            if s==rnk:
                continue
            c=-T[s][j]
            for t in range(col):
                T[s][t]=(T[s][t]+c*T[rnk][t])%Mod
        rnk+=1

    x=[0]*col
    for i in range(rnk):
        x[p[i]]=T[i][-1]

    ker_dim=col-rnk
    ker=[[0]*col for _ in range(ker_dim)]

    for i in range(ker_dim):
        ker[i][q[i]]=1

    for i in range(ker_dim):
        for j in range(rnk):
            ker[i][p[j]]=-T[j][q[i]]%Mod

    Ker=Modulo_Vector_Space(col)
    Ker.add_vectors(*[Modulo_Vector(v) for v in ker])
    return Ker

def Column_Vector(A):
    """ 行列 A の列ベクトルを生成する.

    A: Modulo_Matrix
    """

    return [Modulo_Vector(v) for v in zip(*A.ele)]

def Image_Space(A):
    """ A の像空間 Im A を求める.

    """

    V=Modulo_Vector_Space(A.row)
    V.add_vectors(*Column_Vector(A))
    return V

#==================================================
def solve():
    L, M, N = map(int, input().split())
    A = [None] * L
    for i in range(L):
        A[i] = list(map(int, input().split()))

    B = [None] * M
    for i in range(M):
        B[i] = list(map(int, input().split()))

    def calc(mod):
        global Mod; Mod = mod
        X = Modulo_Matrix(A)
        Y = Modulo_Matrix(B)
        return Image_Space(Y) == Kernel_Space(X)

    return all(calc(mod) for mod in [998244353, 10**9 + 7, 10**9 + 9, 17737, 17747, 17749])

#==================================================
print("Yes" if solve() else "No")
0