結果

問題 No.1073 無限すごろく
ユーザー 👑 KazunKazun
提出日時 2020-06-07 17:33:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 95 ms / 2,000 ms
コード長 5,088 bytes
コンパイル時間 397 ms
コンパイル使用メモリ 87,040 KB
実行使用メモリ 76,980 KB
最終ジャッジ日時 2023-08-25 11:30:38
合計ジャッジ時間 4,749 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,624 KB
testcase_01 AC 73 ms
71,372 KB
testcase_02 AC 86 ms
76,504 KB
testcase_03 AC 74 ms
71,448 KB
testcase_04 AC 75 ms
71,448 KB
testcase_05 AC 81 ms
76,736 KB
testcase_06 AC 80 ms
76,376 KB
testcase_07 AC 78 ms
76,324 KB
testcase_08 AC 79 ms
76,636 KB
testcase_09 AC 77 ms
76,160 KB
testcase_10 AC 81 ms
76,376 KB
testcase_11 AC 80 ms
76,392 KB
testcase_12 AC 80 ms
76,392 KB
testcase_13 AC 85 ms
76,788 KB
testcase_14 AC 85 ms
76,804 KB
testcase_15 AC 86 ms
76,940 KB
testcase_16 AC 89 ms
76,944 KB
testcase_17 AC 87 ms
76,940 KB
testcase_18 AC 85 ms
76,740 KB
testcase_19 AC 86 ms
76,752 KB
testcase_20 AC 85 ms
76,952 KB
testcase_21 AC 93 ms
76,712 KB
testcase_22 AC 86 ms
76,748 KB
testcase_23 AC 89 ms
76,920 KB
testcase_24 AC 91 ms
76,540 KB
testcase_25 AC 88 ms
76,784 KB
testcase_26 AC 90 ms
76,888 KB
testcase_27 AC 90 ms
76,772 KB
testcase_28 AC 88 ms
76,544 KB
testcase_29 AC 91 ms
76,820 KB
testcase_30 AC 94 ms
76,980 KB
testcase_31 AC 95 ms
76,808 KB
testcase_32 AC 93 ms
76,792 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Matrix_Error(Exception):
    pass

class Matrix():
    #入力
    def __init__(self,M=[]):
        self.ele=M
        R=len(M)
        if R!=0:
            C=len(M[1])
        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__(A,B):
        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__(A,B):
        return A+(-B)

    #乗法
    def __mul__(A,B):
        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__(A,B):
        if isinstance(B,int):
            return A*B
    
    #スカラー倍
    def __scale__(A,r):
        M=A.ele
        L=[[r*M[i][j] for j in range(A.col)] for i in range(A.row)]
        return Matrix(L)

    #累乗
    def __pow__(A,n):
        if A.row!=A.col:
            raise Matrix_Error("正方行列ではありません")

        if n<0:
            raise Matrix_Error("nが負です.")

        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__(A,B):
        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__(A,B):
        return not(A==B)

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 __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 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 __pow__(self,m):
        u=abs(m)

        r=Modulo(1,self.n)

        while u>0:
            if u%2==1:
                r*=self
            self*=self
            u=u>>1

        if m>=0:
            return r
        else:
            return r.Modulo_Inverse()


N=int(input())
a=Modulo(6,10**9+7).Modulo_Inverse()
P=[[a,a,a,a,a,a],[1,0,0,0,0,0],[0,1,0,0,0,0],[0,0,1,0,0,0],[0,0,0,1,0,0],[0,0,0,0,1,0]]
P=Matrix(P)

print((P**N).ele[0][0].a)
0