結果
問題 |
No.3225 2×2行列相似判定 〜easy〜
|
ユーザー |
👑 |
提出日時 | 2025-07-25 14:43:32 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 6,134 bytes |
コンパイル時間 | 167 ms |
コンパイル使用メモリ | 82,172 KB |
実行使用メモリ | 98,000 KB |
最終ジャッジ日時 | 2025-07-27 20:11:18 |
合計ジャッジ時間 | 6,643 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | TLE * 1 -- * 2 |
other | -- * 33 |
ソースコード
class ModB: B = 181 length_bound = 10**6 #User definition length_max = min( length_bound , B ) inverse = [None,1] factorial = [1] factorial_inverse = [1] def SetModulo(B): ModB.B = int(B) assert(ModB.B > 0) ModB.length_max = min( ModB.length_bound , ModB.B ) ModB.inverse = [None,1] if ModB.B>1 else [0] ModB.factorial = [1 if ModB.B>1 else 0] ModB.factorial_inverse = [1 if ModB.B>1 else 0] def __init__(self,val,valid = False): self.val = int(val) if not valid and not(0 <= self.val < ModB.B):self.val %= ModB.B def get(n): return n.val if n.__class__ == __class__ else n def copy(self): return ModB(self.val,True) def __str__(self): return str(self.val) def __eq__(self,x): if x.__class__ != __class__:x=ModB(x) return x.val==self.val def __ne__(self,other): return not( self == other ) def __iadd__(self,x): self.val += ModB.ref(x).val if self.val >= ModB.B:self.val -= ModB.B return self def __add__(self,x): a = self.copy() a += x return a def __radd__(self,x): return ModB(x + self.val) def __neg__(self): return ModB(ModB.B - self.val if self.val else 0,True) def __isub__(self,x): self.val -= ModB.ref(x).val if self.val < 0:self.val += ModB.B return self def __sub__(self,x): a = self.copy() a -= x return a def __rsub__(self,x): return ModB(x - self.val) def __mul__(self,x): return ModB.get(x) * self def __rmul__(self,x): return ModB(self.val * x) def __pow__(self,n): #Supported only if n>=0. assert 0<=n answer = ModB(1) power = self.copy() while n > 0: if n&1:answer *= power.val power *= power.val n >>= 1 return answer def __xor__(self,n): #Supported only if B is a prime and val!=0, or n>=0. return self ** ( ( n * (2 - ModB.B) )if n < 0 else n ) def Inverse(n): #Supported only if B is a prime. if n.__class__ == __class__:n=n.val if n >= ModB.B:n %= ModB.B assert n > 0 or ModB.B == 1 if n < ModB.length_max: while len(ModB.inverse) <= n:ModB.inverse+=[ModB.B - ModB.inverse[ModB.B % len(ModB.inverse)] * ( ModB.B // len(ModB.inverse) ) % ModB.B] return ModB(ModB.inverse[n],True) return ModB(n) ** ( ModB.B - 2 ) def __truediv__(self,x): return ModB.Inverse(x) * self def __rtruediv__(self,x): return x * ModB.Inverse(self.val) def Factorial(n): while len(ModB.factorial) <= n:ModB.factorial+=[ModB.factorial[-1] * len(ModB.factorial) % ModB.B] return ModB(ModB.factorial[n],True) def FactorialInverse(n): #Supported only if B is a prime. while len(ModB.factorial_inverse) <= n:ModB.factorial_inverse+=[ModB.factorial_inverse[-1] * ModB.Inverse( len(ModB.factorial_inverse) ).val % ModB.B] return ModB(ModB.factorial_inverse[n],True) def Combination(n,m): #Supported only if B is a prime. return ModB.Factorial(n) * (ModB.FactorialInverse(m).val * ModB.FactorialInverse(n-m).val)if 0<=m<=n else ModB(0,True) #private: def ref(n): return n if n.__class__ == __class__ else ModB(n) def copy(n):return n.copy()if hasattr(n,"copy")else n class TwoByTwoMatrix: zero=None one=None def __init__(self,M00,M01,M10,M11): self.M00 = copy(M00) self.M01 = copy(M01) self.M10 = copy(M10) self.M11 = copy(M11) def copy(self): return self.__class__(self.M00,self.M01,self.M10,self.M11) def __eq__(self,other): return self.M00 == other.M00 and self.M01 == other.M01 and self.M10 == other.M10 and self.M11 == other.M11 def __ne__(self,other): return not( self == other ) def __iadd__(self,other): self.M00 += other.M00 self.M01 += other.M01 self.M10 += other.M10 self.M11 += other.M11 return self def __add__(self,other): M = self.copy() M += other return M def __isub__(self,other): self.M00 -= other.M00 self.M01 -= other.M01 self.M10 -= other.M10 self.M11 -= other.M11 return self def __sub__(self,other): M = self.copy() M -= other return M def __neg__(self): return self.__class__(-self.M00,-self.M01,-self.M10,-self.M11) def __mul__(self,other): return self.__class__(self.M00 * other.M00 + self.M01 * other.M10,self.M00 * other.M01 + self.M01 * other.M11,self.M10 * other.M00 + self.M11 * other.M10,self.M10 * other.M01 + self.M11 * other.M11) def __imul__(self,other): self.M00 , self.M01 , self.M10 , self.M11 = self.M00 * other.M00 + self.M01 * other.M10 , self.M00 * other.M01 + self.M01 * other.M11 , self.M10 * other.M00 + self.M11 * other.M10 , self.M10 * other.M01 + self.M11 * other.M11 return self def ScalarMultiply(self,x): self.M00 *= x self.M01 *= x self.M10 *= x self.M11 *= x return self def det(self): return self.M00 * self.M11 - self.M01 * self.M10 def tr(self): return self.M00 + self.M11 def Adjugate(self): return self.__class__( self.M11 , - self.M01 , - self.M10 , self.M00 ) def Inverse(self): return self.Adjugate().ScalarMultiply( 1 / self.det() ) #d = self.det() #assert( d in [1,-1] ) #For the case of integer coefficients #return self.Adjugate().ScalarMultiply( d ) def __truediv__(self,other): return self * other.Inverse() def __itruediv__(self,other): self *= other.Inverse() return self def __pow__(self,n): #Supported only when n>=0 answer = self.__class__.one.copy() power = self.copy() while n > 0: if n&1:answer *= power power.Square() n >>= 1 return answer def __xor__(self,n): return self.Inverse()**(-n)if n < 0 else self ** n #private: def Square(self): self.M00 , self.M01 , self.M10 , self.M11 = self.M00 ** 2 + self.M01 * self.M10 , ( self.M00 + self.M11 ) * self.M01 , self.M10 * ( self.M00 + self.M11 ) , self.M10 * self.M01 + self.M11 ** 2 TwoByTwoMatrix.zero = TwoByTwoMatrix(0,0,0,0) #User's definition TwoByTwoMatrix.one = TwoByTwoMatrix(1,0,0,1) #User's definition R=range J=lambda:[[ModB(x,1)for x in map(int,input().split())]for i in R(2)] A=J();A=TwoByTwoMatrix(*(A[0]+A[1])) B=J();B=TwoByTwoMatrix(*(B[0]+B[1])) P=TwoByTwoMatrix(ModB(0,1),ModB(0,1),ModB(0,1),ModB(0,1)) L=R(ModB.B) one=ModB(1,1) for a in R(2): for b in L: for c in L: for d in L: if P.det().val and P*A==B*P:exit(print("Yes")) P.M11+=one P.M10+=one P.M01+=one P.M00+=one print("No")