結果

問題 No.1275 綺麗な式
ユーザー KazunKazun
提出日時 2020-10-30 22:14:18
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 49 ms / 2,000 ms
コード長 10,220 bytes
コンパイル時間 158 ms
コンパイル使用メモリ 82,192 KB
実行使用メモリ 63,232 KB
最終ジャッジ日時 2024-07-22 01:01:00
合計ジャッジ時間 4,311 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
57,216 KB
testcase_01 AC 45 ms
57,984 KB
testcase_02 AC 46 ms
57,984 KB
testcase_03 AC 44 ms
57,088 KB
testcase_04 AC 47 ms
57,472 KB
testcase_05 AC 47 ms
57,216 KB
testcase_06 AC 44 ms
57,728 KB
testcase_07 AC 46 ms
57,600 KB
testcase_08 AC 45 ms
57,600 KB
testcase_09 AC 46 ms
57,472 KB
testcase_10 AC 48 ms
57,600 KB
testcase_11 AC 46 ms
57,216 KB
testcase_12 AC 45 ms
57,728 KB
testcase_13 AC 45 ms
57,472 KB
testcase_14 AC 47 ms
57,088 KB
testcase_15 AC 45 ms
57,344 KB
testcase_16 AC 43 ms
57,856 KB
testcase_17 AC 47 ms
57,728 KB
testcase_18 AC 45 ms
57,600 KB
testcase_19 AC 46 ms
56,960 KB
testcase_20 AC 48 ms
57,088 KB
testcase_21 AC 47 ms
57,716 KB
testcase_22 AC 47 ms
57,344 KB
testcase_23 AC 47 ms
57,472 KB
testcase_24 AC 46 ms
57,344 KB
testcase_25 AC 46 ms
57,856 KB
testcase_26 AC 47 ms
57,344 KB
testcase_27 AC 47 ms
57,856 KB
testcase_28 AC 46 ms
57,600 KB
testcase_29 AC 46 ms
57,600 KB
testcase_30 AC 43 ms
56,064 KB
testcase_31 AC 42 ms
56,192 KB
testcase_32 AC 43 ms
56,448 KB
testcase_33 AC 42 ms
56,192 KB
testcase_34 AC 43 ms
56,320 KB
testcase_35 AC 41 ms
55,808 KB
testcase_36 AC 44 ms
56,448 KB
testcase_37 AC 44 ms
55,936 KB
testcase_38 AC 44 ms
56,832 KB
testcase_39 AC 45 ms
56,448 KB
testcase_40 AC 41 ms
56,064 KB
testcase_41 AC 42 ms
56,576 KB
testcase_42 AC 43 ms
56,320 KB
testcase_43 AC 42 ms
56,064 KB
testcase_44 AC 45 ms
57,472 KB
testcase_45 AC 44 ms
57,856 KB
testcase_46 AC 46 ms
57,728 KB
testcase_47 AC 45 ms
57,600 KB
testcase_48 AC 44 ms
57,344 KB
testcase_49 AC 49 ms
56,960 KB
testcase_50 AC 46 ms
57,472 KB
testcase_51 AC 47 ms
63,232 KB
testcase_52 AC 45 ms
57,600 KB
testcase_53 AC 47 ms
57,728 KB
testcase_54 AC 44 ms
57,216 KB
testcase_55 AC 45 ms
57,600 KB
testcase_56 AC 46 ms
56,960 KB
testcase_57 AC 46 ms
57,600 KB
testcase_58 AC 42 ms
57,472 KB
testcase_59 AC 43 ms
57,600 KB
testcase_60 AC 44 ms
57,728 KB
testcase_61 AC 45 ms
56,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Modulo_Error(Exception):
    pass

class Modulo():
    def __init__(self,a,n):
        self.a=a%n
        self.n=n

    def __str__(self):
        return "{} (mod {})".format(self.a,self.n)

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

    def __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==0

    def __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, 1
        a,b=self.a,self.n
        while b != 0:
            q, a, b = a // b, b, a % b
            x0, x1 = x1, x0 - q * x1
            y0, y1 = y1, y0 - q * y1

        if 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 r
        else:
            return r.Modulo_Inverse()

from copy import copy,deepcopy

class Matrix_Error(Exception):
    pass

class Matrix():
    #入力
    def __init__(self,M=[]):
        self.ele=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):
        T=""
        (r,c)=self.size
        for 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 self

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

    #加法
    def __add__(self,other):
        A=self
        B=other
        if A.size!=B.size:
            raise Matrix_Error("2つの行列のサイズが異なります.({},{})".format(A.size,B.size))
        M=A.ele
        N=B.ele

        L=[]
        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=self
        B=other
        if isinstance(B,Matrix):
            R=A.row
            C=B.col

            if A.col!=B.row:
                 raise Matrix_Error("左側の列と右側の行が一致しません.({},{})".format(A.size,B.size))
            G=A.col

            M=A.ele
            N=B.ele

            E=[]
            for i in range(R):
                F=[]
                for j in range(C):
                    S=0
                    for 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*other

    def Inverse(self):
        from copy import copy
        M=self
        if M.row!=M.col:
            raise Matrix_Error("正方行列ではありません.")

        R=M.row
        I=[[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.ele
        L=[[r*M[i][j] for j in range(self.col)] for i in range(self.row)]
        return Matrix(L)

    #累乗
    def __pow__(self,n):
        A=self
        if 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=A

        while n>0:
            if n%2==1:
                R*=D
            D*=D
            n=n>>1

        return R

    #等号
    def __eq__(self,other):
        A=self
        B=other
        if A.size!=B.size:
            return False

        for i in range(A.row):
            for j in range(A.col):
                if A.ele[i][j]!=B.ele[i][j]:
                    return False

        return True

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

    #転置
    def Transpose(self):
        self.col,self.row=self.row,self.col
        self.ele=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]
                for j in range(C):
                    T[I][j]/=u

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

        return 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]
                for i in range(R):
                    T[i][J]/=u

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

        return Matrix(T)

    #行列の階数
    def Rank(self,ep=None):
        M=self.Row_Reduce()
        (R,C)=M.size
        T=M.ele

        S=0
        for i in range(R):
            f=False
            if ep==None:
                for j in range(C):
                    if T[i][j]!=0:
                        f=True
            else:
                for j in range(C):
                    if abs(T[i][j])>=ep:
                        f=True

            if f:
                S+=1
            else:
                break

        return 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=r
    return 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=0
    for 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.row
    T=deepcopy(M.ele)

    I=0
    D=1
    for 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*=-1
                    break

        if T[I][J]!=0:
            u=T[I][J]
            for j in range(R):
                T[I][j]/=u
            D*=u

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

    for i in range(R):
        D*=T[i][i]

    return D
#================================================
Mod=10**9+7

a,b=map(lambda x:Modulo(int(x),Mod),input().split())
n=int(input())

M=Matrix([[a,b],[Modulo(1,Mod),a]])**n

if n==0:
    print(2)
else:
    print((2*M.ele[0][0]).a)
0