結果
問題 | No.1275 綺麗な式 |
ユーザー |
👑 ![]() |
提出日時 | 2020-10-30 22:13:12 |
言語 | PyPy3 (7.3.15) |
結果 |
RE
|
実行時間 | - |
コード長 | 10,187 bytes |
コンパイル時間 | 154 ms |
コンパイル使用メモリ | 82,424 KB |
実行使用メモリ | 70,388 KB |
最終ジャッジ日時 | 2024-07-22 00:59:57 |
合計ジャッジ時間 | 4,296 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 57 RE * 3 |
ソースコード
class Modulo_Error(Exception):passclass Modulo():def __init__(self,a,n):self.a=a%nself.n=ndef __str__(self):return "{} (mod {})".format(self.a,self.n)#+,-def __pos__(self):return selfdef __neg__(self):return Modulo(-self.a,self.n)#等号,不等号def __eq__(self,other):if isinstance(other,Modulo):return (self.a==other.a) and (self.n==other.n)elif isinstance(other,int):return (self-other).a==0def __neq__(self,other):return not(self==other)#加法def __add__(self,other):if isinstance(other,Modulo):if self.n!=other.n:raise Modulo_Error("異なる法同士の演算です.")return Modulo(self.a+other.a,self.n)elif isinstance(other,int):return Modulo(self.a+other,self.n)def __radd__(self,other):if isinstance(other,int):return Modulo(self.a+other,self.n)#減法def __sub__(self,other):return self+(-other)def __rsub__(self,other):if isinstance(other,int):return -self+other#乗法def __mul__(self,other):if isinstance(other,Modulo):if self.n!=other.n:raise Modulo_Error("異なる法同士の演算です.")return Modulo(self.a*other.a,self.n)elif isinstance(other,int):return Modulo(self.a*other,self.n)def __rmul__(self,other):if isinstance(other,int):return Modulo(self.a*other,self.n)#Modulo逆数def inverse(self):return self.Modulo_Inverse()def Modulo_Inverse(self):x0, y0, x1, y1 = 1, 0, 0, 1a,b=self.a,self.nwhile b != 0:q, a, b = a // b, b, a % bx0, x1 = x1, x0 - q * x1y0, y1 = y1, y0 - q * y1if a!=1:raise Modulo_Error("{}の逆数が存在しません".format(self))else:return Modulo(x0,self.n)#除法def __truediv__(self,other):return self*(other.Modulo_Inverse())def __rtruediv__(self,other):return other*(self.Modulo_Inverse())#累乗def __pow__(self,m):u=abs(m)r=Modulo(pow(self.a,m,self.n),self.n)if m>=0:return relse:return r.Modulo_Inverse()from copy import copy,deepcopyclass Matrix_Error(Exception):passclass Matrix():#入力def __init__(self,M=[]):self.ele=MR=len(M)if R!=0:C=len(M[0])else:C=0self.row=Rself.col=Cself.size=(R,C)#出力def __str__(self):T=""(r,c)=self.sizefor i in range(r):U="["for j in range(c):U+=str(self.ele[i][j])+" "T+=U[:-1]+"]\n"return "["+T[:-1]+"]"#+,-def __pos__(self):return selfdef __neg__(self):return self.__scale__(-1)#加法def __add__(self,other):A=selfB=otherif A.size!=B.size:raise Matrix_Error("2つの行列のサイズが異なります.({},{})".format(A.size,B.size))M=A.eleN=B.eleL=[]for i in range(A.row):E=[]for j in range(A.col):E.append(M[i][j]+N[i][j])L.append(E)return Matrix(L)#減法def __sub__(self,other):return self+(-other)#乗法def __mul__(self,other):A=selfB=otherif isinstance(B,Matrix):R=A.rowC=B.colif A.col!=B.row:raise Matrix_Error("左側の列と右側の行が一致しません.({},{})".format(A.size,B.size))G=A.colM=A.eleN=B.eleE=[]for i in range(R):F=[]for j in range(C):S=0for k in range(G):S+=M[i][k]*N[k][j]F.append(S)E.append(F)return Matrix(E)elif isinstance(B,int):return A.__scale__(B)def __rmul__(self,other):if isinstance(other,int):return self*otherdef Inverse(self):from copy import copyM=selfif M.row!=M.col:raise Matrix_Error("正方行列ではありません.")R=M.rowI=[[1*(i==j) for j in range(R)] for i in range(R)]G=M.Column_Union(Matrix(I))G=G.Row_Reduce()A,B=[],[]for i in range(R):A.append(copy(G.ele[i][:R]))B.append(copy(G.ele[i][R:]))if A==I:return Matrix(B)else:raise Matrix_Error("正則ではありません.")#スカラー倍def __scale__(self,r):M=self.eleL=[[r*M[i][j] for j in range(self.col)] for i in range(self.row)]return Matrix(L)#累乗def __pow__(self,n):A=selfif A.row!=A.col:raise Matrix_Error("正方行列ではありません.")if n<0:return (A**(-n)).Inverse()R=Matrix([[1*(i==j) for j in range(A.row)] for i in range(A.row)])D=Awhile n>0:if n%2==1:R*=DD*=Dn=n>>1return R#等号def __eq__(self,other):A=selfB=otherif A.size!=B.size:return Falsefor i in range(A.row):for j in range(A.col):if A.ele[i][j]!=B.ele[i][j]:return Falsereturn True#不等号def __neq__(self,other):return not(self==other)#転置def Transpose(self):self.col,self.row=self.row,self.colself.ele=list(map(list,zip(*self.ele)))#行基本変形def Row_Reduce(self):M=self(R,C)=M.sizeT=[]for i in range(R):U=[]for j in range(C):U.append(M.ele[i][j])T.append(U)I=0for 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]breakif T[I][J]!=0:u=T[I][J]for j in range(C):T[I][j]/=ufor i in range(R):if i!=I:v=T[i][J]for j in range(C):T[i][j]-=v*T[I][j]I+=1if I==R:breakreturn Matrix(T)#列基本変形def Column_Reduce(self):M=self(R,C)=M.sizeT=[]for i in range(R):U=[]for j in range(C):U.append(M.ele[i][j])T.append(U)J=0for 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]breakif T[I][J]!=0:u=T[I][J]for i in range(R):T[i][J]/=ufor j in range(C):if j!=J:v=T[I][j]for i in range(R):T[i][j]-=v*T[i][J]J+=1if J==C:breakreturn Matrix(T)#行列の階数def Rank(self,ep=None):M=self.Row_Reduce()(R,C)=M.sizeT=M.eleS=0for i in range(R):f=Falseif ep==None:for j in range(C):if T[i][j]!=0:f=Trueelse:for j in range(C):if abs(T[i][j])>=ep:f=Trueif f:S+=1else:breakreturn S#行の結合def Row_Union(self,other):return 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 Matrix(E)#------------------------------------------------------------#単位行列def Identity_Matrix(n):return Matrix([[1*(i==j) for j in range(n)] for i in range(n)])#零行列def Zero_Matrix(r,c=None):if c==None:c=rreturn Matrix([[0]*c for i in range(r)])#正方行列?def Is_Square(M):return M.row==M.col#対角行列def Diagonal_Matrix(*A):N=len(A)return Matrix([[A[i]*(i==j) for j in range(N)] for i in range(N)])#跡def Trace(M):if not Is_Square(M):raise Matrix_Error("正方行列ではありません")T=0for i in range(M.col):T+=M.ele[i][i]return T#行列式def Det(M):if not Is_Square(M):raise Matrix_Error("正方行列ではありません")R=M.rowT=deepcopy(M.ele)I=0D=1for J in range(R):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]D*=-1breakif T[I][J]!=0:u=T[I][J]for j in range(R):T[I][j]/=uD*=ufor i in range(I+1,R):v=T[i][J]for j in range(R):T[i][j]-=v*T[I][j]I+=1if I==R:breakfor i in range(R):D*=T[i][i]return D#================================================Mod=10**9+7a,b=map(lambda x:Modulo(int(x),Mod),input().split())n=int(input())M=Matrix([[a,b],[Modulo(1,Mod),a]])**nprint((2*M.ele[0][0]).a)