結果

問題 No.1706 Many Bus Stops (hard)
ユーザー kohei2019kohei2019
提出日時 2023-06-04 20:26:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 47 ms / 2,000 ms
コード長 7,686 bytes
コンパイル時間 297 ms
コンパイル使用メモリ 82,204 KB
実行使用メモリ 65,328 KB
最終ジャッジ日時 2024-06-09 04:03:31
合計ジャッジ時間 2,993 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
55,520 KB
testcase_01 AC 38 ms
56,540 KB
testcase_02 AC 41 ms
64,096 KB
testcase_03 AC 42 ms
64,180 KB
testcase_04 AC 46 ms
63,948 KB
testcase_05 AC 42 ms
64,288 KB
testcase_06 AC 44 ms
63,620 KB
testcase_07 AC 42 ms
63,928 KB
testcase_08 AC 42 ms
65,032 KB
testcase_09 AC 43 ms
65,328 KB
testcase_10 AC 42 ms
63,956 KB
testcase_11 AC 41 ms
64,272 KB
testcase_12 AC 40 ms
64,212 KB
testcase_13 AC 42 ms
64,424 KB
testcase_14 AC 43 ms
64,432 KB
testcase_15 AC 40 ms
64,288 KB
testcase_16 AC 42 ms
63,532 KB
testcase_17 AC 42 ms
64,824 KB
testcase_18 AC 41 ms
65,032 KB
testcase_19 AC 41 ms
64,012 KB
testcase_20 AC 43 ms
64,316 KB
testcase_21 AC 41 ms
63,852 KB
testcase_22 AC 42 ms
64,096 KB
testcase_23 AC 41 ms
64,764 KB
testcase_24 AC 44 ms
64,872 KB
testcase_25 AC 43 ms
64,500 KB
testcase_26 AC 43 ms
63,888 KB
testcase_27 AC 42 ms
63,928 KB
testcase_28 AC 42 ms
63,688 KB
testcase_29 AC 42 ms
64,288 KB
testcase_30 AC 41 ms
63,772 KB
testcase_31 AC 42 ms
64,008 KB
testcase_32 AC 42 ms
64,164 KB
testcase_33 AC 43 ms
63,592 KB
testcase_34 AC 41 ms
63,896 KB
testcase_35 AC 42 ms
64,896 KB
testcase_36 AC 45 ms
63,604 KB
testcase_37 AC 45 ms
64,416 KB
testcase_38 AC 44 ms
64,288 KB
testcase_39 AC 46 ms
64,808 KB
testcase_40 AC 45 ms
64,624 KB
testcase_41 AC 47 ms
64,452 KB
testcase_42 AC 42 ms
63,876 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import copy
class matrix():
    def __init__(self):
        self.mod = 10**9+7

    def multiplication(self,arr1,arr2):
        '''
        例
        arr1
        2 3 4 5
        6 7 8 9
        arr2
        1 2
        3 4
        5 6
        7 8
        '''
        H = len(arr1)
        W = len(arr2[0])
        arr3 = [[0]*W for i in range(H)]
        for i in range(H):
            for j in range(W):
                val = 0
                for k in range(len(arr1[0])):
                    val += arr1[i][k]*arr2[k][j]
                arr3[i][j] = val
        return arr3
    
    def determinant(self,arr):
        '''
        正方行列N*Nの行列式
        計算量O(N**3)
        '''
        arr_calc = copy.deepcopy(arr)
        N = len(arr_calc)
        for i in range(N-1):
            d = arr_calc[i][i]
            for j in range(i+1,N):
                e = arr_calc[j][i]/d
                for k in range(i,N):
                    arr_calc[j][k] -= e*arr_calc[i][k]
        #arr_calc 上△行列
        det = 1
        for i in range(N):
            det *= arr_calc[i][i]
        return det
    
    def invarr(self,arr):
        '''
        正方行列N*Nの逆行列
        det == 0ならreturn False
        計算量O(N**3)
        ''' 
        arr_calc = copy.deepcopy(arr)
        if self.determinant(arr_calc) == 0:
            return False
        N = len(arr_calc)
        for i in range(N):
            v = [0]*(N)
            v[i] = 1
            arr_calc[i].extend(v)
        for i in range(N-1):
            d = arr_calc[i][i]
            for j in range(i+1,N):
                e = arr_calc[j][i]/d
                for k in range(i,2*N):
                    arr_calc[j][k] -= e*arr_calc[i][k]
        for i in range(N-1,-1,-1):
            d = arr_calc[i][i]
            for k in range(i,2*N):
                arr_calc[i][k] /= d
            for j in range(i-1,-1,-1):
                c = arr_calc[j][i]
                for k in range(i,2*N):
                    arr_calc[j][k] -= c*arr_calc[i][k]
        inv = [[0]*(N) for i in range(N)]
        for i in range(N):
            for j in range(N):
                inv[i][j] = arr_calc[i][j+N]
        return inv
    
    def SimultaneousE(self,arr):
        '''
        3x+2y+z = 4
        4x+5y+6z = 3
        7x+8y+9z = 2
        ->
        3 2 1 4
        4 5 6 3
        7 8 9 2
        '''
        N = len(arr)
        arr1 = [[0]*(N) for i in range(N)]
        for i in range(N):
            for j in range(N):
                arr1[i][j] = arr[i][j]
        v = [[0] for i in range(N)]
        for i in range(N):
            v[i][0] = arr[i][-1]
        if self.determinant(arr1) == 0:
            return False
        inva = self.invarr(arr1)
        return self.multiplication(inva,v)


    def invmod(self,a):#mod逆元
        if a == 0:
            return 0
        if a == 1:
            return 1
        return (-self.invmod(self.mod % a) * (self.mod // a)) % self.mod
    
    def multiplication_mod(self,arr1,arr2):
        H = len(arr1)
        W = len(arr2[0])
        arr3 = [[0]*W for i in range(H)]
        for i in range(H):
            for j in range(W):
                val = 0
                for k in range(len(arr1[0])):
                    val += arr1[i][k]*arr2[k][j]
                arr3[i][j] = val%self.mod
        return arr3

    def determinant_mod(self,arr):
        '''
        正方行列N*Nの行列式
        計算量O(N**3)
        '''
        arr_calc = copy.deepcopy(arr)
        N = len(arr_calc)
        for i in range(N-1):
            d = arr_calc[i][i]
            for j in range(i+1,N):
                e = arr_calc[j][i]*self.invmod(d)
                e %= self.mod
                for k in range(i,N):
                    arr_calc[j][k] -= e*arr_calc[i][k]
                    arr_calc[j][k] %= self.mod
        #arr_calc 上△行列
        det = 1
        for i in range(N):
            det *= arr_calc[i][i]
            det %= self.mod
        return det

    def invarr_mod(self,arr):
        '''
        正方行列N*Nの逆行列
        det == 0ならreturn False
        計算量O(N**3)
        '''
        arr_calc = copy.deepcopy(arr)
        det = self.determinant_mod(arr_calc)
        if det == 0:
            return False
        N = len(arr_calc)
        for i in range(N):
            v = [0]*(N)
            v[i] = det
            arr_calc[i].extend(v)
        for i in range(N-1):
            d = arr_calc[i][i]
            for j in range(i+1,N):
                e = arr_calc[j][i]*self.invmod(d)
                for k in range(i,2*N):
                    arr_calc[j][k] -= e*arr_calc[i][k]
                    arr_calc[j][k] %= self.mod
        for i in range(N-1,-1,-1):
            d = arr_calc[i][i]
            for k in range(i,2*N):
                arr_calc[i][k] *= self.invmod(d)
            for j in range(i-1,-1,-1):
                c = arr_calc[j][i]
                for k in range(i,2*N):
                    arr_calc[j][k] -= c*arr_calc[i][k]
                    arr_calc[j][k] %= self.mod
        inv = [[0]*(N) for i in range(N)]
        for i in range(N):
            for j in range(N):
                inv[i][j] = arr_calc[i][j+N]*self.invmod(det)%self.mod
        return inv
    
    def SimultaneousE_mod(self,arr):
        '''
        3x+2y+z = 4
        4x+5y+6z = 3
        7x+8y+9z = 2
        ->
        3 2 1 4
        4 5 6 3
        7 8 9 2
        '''
        N = len(arr)
        arr1 = [[0]*(N) for i in range(N)]
        for i in range(N):
            for j in range(N):
                arr1[i][j] = arr[i][j]
        v = [[0] for i in range(N)]
        for i in range(N):
            v[i][0] = arr[i][-1]
        det = self.determinant_mod(arr1)
        if det == 0:
            return False
        inva = self.invarr_mod(arr1)
        v2 = self.multiplication_mod(inva,v)
        for i in range(N):
            v2[i][0] %= self.mod
        return v2

    def modPow_matrix(self,arr,n):
        '''
        N*Nの正方行列arrをn乗する。
        '''
        N = len(arr)
        if n==0:
            arr1 = [[0]*(N) for i in range(N)]
            for i in range(N):
                arr1[i][i] = 1
            return arr1
        if n==1:
            for i in range(N):
                for j in range(N):
                    arr[i][j] %= self.mod
            return arr
        if n % 2 == 1:
            arr2 = self.multiplication_mod(arr,self.modPow_matrix(arr,n-1))
            return arr2
        arr3 = self.modPow_matrix(arr,n//2)
        return self.multiplication_mod(arr3,arr3)

    def Pow_matrix(self,arr,n):
        '''
        N*Nの正方行列arrをn乗する。
        '''
        N = len(arr)
        if n==0:
            arr1 = [[0]*(N) for i in range(N)]
            for i in range(N):
                arr1[i][i] = 1
            return arr1
        if n==1:
            return arr
        if n % 2 == 1:
            arr2 = self.multiplication(arr,self.Pow_matrix(arr,n-1))
            return arr2
        arr3 = self.Pow_matrix(arr,n//2)
        return self.multiplication(arr3,arr3)

def modPow(a,n,mod):#繰り返し二乗法 a**n % mod
    if n==0:
        return 1
    if n==1:
        return a%mod
    if n & 1:
        return (a*modPow(a,n-1,mod)) % mod
    t = modPow(a,n>>1,mod)
    return (t*t)%mod


C,N,M = map(int,input().split())
mod = 10**9+7
MX = matrix()
one_c = MX.invmod(C)
one_c1 = MX.invmod(C-1)

arr = [[one_c,0,((C-1)*one_c)%mod,0],[0,one_c,0,((C-1)*one_c)%mod],[0,1,0,0],[one_c1,((C-2)*one_c1)%mod,0,0]]
m = MX.modPow_matrix(arr, N)
ans0 = MX.multiplication_mod(m,[[1],[0],[0],[0]])[0][0]
ans = (1-modPow((1-ans0)%mod,M,mod))%mod
print(ans)
0