結果

問題 No.1750 ラムドスウイルスの感染拡大-hard
ユーザー Navier_BoltzmannNavier_Boltzmann
提出日時 2022-01-03 07:03:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,561 ms / 2,000 ms
コード長 3,292 bytes
コンパイル時間 268 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 77,952 KB
最終ジャッジ日時 2024-04-20 18:21:22
合計ジャッジ時間 19,170 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
53,248 KB
testcase_01 AC 47 ms
52,864 KB
testcase_02 AC 51 ms
59,008 KB
testcase_03 AC 56 ms
61,312 KB
testcase_04 AC 117 ms
73,984 KB
testcase_05 AC 45 ms
52,992 KB
testcase_06 AC 48 ms
53,504 KB
testcase_07 AC 42 ms
52,992 KB
testcase_08 AC 328 ms
77,056 KB
testcase_09 AC 326 ms
76,672 KB
testcase_10 AC 319 ms
76,636 KB
testcase_11 AC 276 ms
76,672 KB
testcase_12 AC 361 ms
77,056 KB
testcase_13 AC 338 ms
76,800 KB
testcase_14 AC 1,449 ms
77,696 KB
testcase_15 AC 1,506 ms
77,696 KB
testcase_16 AC 1,509 ms
77,952 KB
testcase_17 AC 1,505 ms
77,312 KB
testcase_18 AC 1,561 ms
77,440 KB
testcase_19 AC 1,478 ms
77,536 KB
testcase_20 AC 1,045 ms
77,184 KB
testcase_21 AC 1,225 ms
77,440 KB
testcase_22 AC 300 ms
76,288 KB
testcase_23 AC 1,404 ms
77,568 KB
testcase_24 AC 229 ms
76,368 KB
testcase_25 AC 447 ms
76,160 KB
testcase_26 AC 195 ms
75,904 KB
testcase_27 AC 66 ms
67,456 KB
testcase_28 AC 75 ms
73,344 KB
testcase_29 AC 64 ms
67,200 KB
testcase_30 AC 272 ms
76,416 KB
testcase_31 AC 264 ms
76,640 KB
testcase_32 AC 280 ms
76,780 KB
testcase_33 AC 268 ms
76,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Matrix():
    def __init__(self, n, m ,mat=None,mod=998244353):
        self.n = n
        self.m = m
        self.MOD = mod
        self.mat = [[0] * self.m for _ in range(self.n)]
        if mat:
            assert len(mat) == n and len(mat[0]) == m
            for i in range(self.n):
                self.mat[i] = mat[i].copy()

    def is_square(self):
        return self.n == self.m

    def __getitem__(self, key):
        if isinstance(key, slice):
            return self.mat[key]
        else:
            assert key >= 0
            return self.mat[key]

    def id(n):
        res = Matrix(n, n)
        for i in range(n):
            res[i][i] = 1
        return res

    def __len__(self):
        return len(self.mat)

    def __str__(self):
        return '\n'.join(' '.join(map(str, self[i])) for i in range(self.n))

    def times(self, k):
        res = [[0] * self.m for _ in range(self.n)]
        for i in range(self.n):
            res_i, self_i = res[i], self[i]
            for j in range(self.m):
                res_i[j] = k * self_i[j] % self.MOD
        return Matrix(self.n, self.m, res)

    def __pos__(self):
        return self

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

    def __add__(self, other):
        assert self.n == other.n and self.m == other.m
        res = [[0] * self.m for _ in range(self.n)]
        for i in range(self.n):
            res_i, self_i, other_i = res[i], self[i], other[i]
            for j in range(self.m):
                res_i[j] = (self_i[j] + other_i[j]) % self.MOD
        return Matrix(self.n, self.m, res)

    def __sub__(self, other):
        assert self.n == other.n and self.m == other.m
        res = [[0] * self.m for _ in range(self.n)]
        for i in range(self.n):
            res_i, self_i, other_i = res[i], self[i], other[i]
            for j in range(self.m):
                res_i[j] = (self_i[j] - other_i[j]) % self.MOD
        return Matrix(self.n, self.m, res)

    def __mul__(self, other):
        if other.__class__ == Matrix:
            assert self.m == other.n
            res = [[0] * other.m for _ in range(self.n)]
            for i in range(self.n):
                res_i, self_i = res[i], self[i]
                for k in range(self.m):
                    self_ik, other_k = self_i[k], other[k]
                    for j in range(other.m):
                        res_i[j] += self_ik * other_k[j]
                        res_i[j] %= self.MOD
            return Matrix(self.n, other.m, res)
        else:
            return self.times(other)

    def __rmul__(self, other):
        return self.times(other)

    def __pow__(self, k):
        assert self.is_square()
        tmp = Matrix(self.n, self.n, self.mat)
        res = Matrix.id(self.n)
        while k:
            if k & 1:
                res *= tmp
            tmp *= tmp
            k >>= 1
        return res
        

N,M,T = map(int,input().split())
e = [[] for _ in range(N)]
for _ in range(M):
    s,t = map(int,input().split())
    e[s].append(t)
    e[t].append(s)
    
mod = 998244353
    
S = [[0]*N for _ in range(N)]
for i in range(N):
    for j in e[i]:
        S[i][j] = 1
S = Matrix(N,N,S,mod)
tS = S**T
B = [[0] for _ in range(N)]
B[0] = [1]
B = Matrix(N,1,B,mod)
ans = tS*B
print(ans[0][0])
0