結果

問題 No.1516 simple 門松列 problem Re:MASTER
ユーザー hir355hir355
提出日時 2021-05-21 23:31:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 195 ms / 6,000 ms
コード長 3,853 bytes
コンパイル時間 194 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 76,800 KB
最終ジャッジ日時 2024-04-18 16:57:56
合計ジャッジ時間 2,908 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
58,880 KB
testcase_01 AC 68 ms
66,304 KB
testcase_02 AC 126 ms
76,288 KB
testcase_03 AC 55 ms
61,824 KB
testcase_04 AC 55 ms
61,696 KB
testcase_05 AC 55 ms
62,720 KB
testcase_06 AC 65 ms
64,128 KB
testcase_07 AC 69 ms
66,816 KB
testcase_08 AC 73 ms
68,864 KB
testcase_09 AC 49 ms
59,392 KB
testcase_10 AC 62 ms
64,000 KB
testcase_11 AC 53 ms
61,184 KB
testcase_12 AC 55 ms
62,080 KB
testcase_13 AC 54 ms
60,800 KB
testcase_14 AC 184 ms
76,800 KB
testcase_15 AC 125 ms
76,544 KB
testcase_16 AC 100 ms
76,288 KB
testcase_17 AC 73 ms
71,552 KB
testcase_18 AC 60 ms
64,640 KB
testcase_19 AC 62 ms
66,432 KB
testcase_20 AC 195 ms
76,672 KB
testcase_21 AC 195 ms
76,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 998244353
 
class Matrix():
    def __init__(self, n, m, mat=None):
        self.n = n
        self.m = m
        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] % 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]) % 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]) % 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] %= MOD
            return Matrix(self.n, other.m, res)
        elif other.__class__ == list:
            assert self.m == len(other)
            res = [0] * self.n
            for i in range(self.n):
                self_i = self[i]
                for j in range(self.m):
                    res[i] += self_i[j] * other[j]
                    res[i] %= MOD
            return 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, k = map(int, input().split())
mat = [[0] * (k * k) for _ in range(k * k)]
for i in range(k):
    for j in range(i + 1, k):
        for p in range(j):
            if i == p:
                continue
            mat[j + p * k][i + j * k] = 1
for i in range(k):
    for j in range(i):
        for p in range(j + 1, k):
            if i == p:
                continue
            mat[j + p * k][i + j * k] = 1
f1 = Matrix(k * k, k * k, mat)
a = [0] * (k * k)
for i in range(k):
    for j in range(k):
        if i != j:
            a[i * k + j] = 1
ans1 = sum(f1 ** (n - 2) * a) % MOD
ans2 = ans1 * n * (k - 1) * pow(2, MOD - 2, MOD) % MOD
print(ans1, ans2)
0