結果

問題 No.2156 ぞい文字列
ユーザー ikomaikoma
提出日時 2022-12-09 23:05:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 55 ms / 2,000 ms
コード長 1,624 bytes
コンパイル時間 733 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 61,696 KB
最終ジャッジ日時 2024-04-22 22:56:53
合計ジャッジ時間 2,224 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
61,056 KB
testcase_01 AC 55 ms
61,056 KB
testcase_02 AC 52 ms
61,184 KB
testcase_03 AC 53 ms
60,800 KB
testcase_04 AC 53 ms
61,184 KB
testcase_05 AC 53 ms
61,568 KB
testcase_06 AC 54 ms
60,800 KB
testcase_07 AC 55 ms
61,184 KB
testcase_08 AC 42 ms
52,736 KB
testcase_09 AC 42 ms
53,248 KB
testcase_10 AC 43 ms
52,608 KB
testcase_11 AC 42 ms
53,120 KB
testcase_12 AC 42 ms
52,864 KB
testcase_13 AC 43 ms
52,608 KB
testcase_14 AC 42 ms
52,608 KB
testcase_15 AC 43 ms
52,480 KB
testcase_16 AC 41 ms
52,096 KB
testcase_17 AC 41 ms
52,480 KB
testcase_18 AC 42 ms
52,864 KB
testcase_19 AC 54 ms
61,696 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N=int(input())
if N==2:exit(print(1))
MOD=998244353
class matrix_mod:
    def __init__(self, data, mod:int=MOD):
        self._mat = data._mat if isinstance(data, self.__class__) else data
        self.row = len(data)
        self.col = len(data[0])
        self.mod = mod

    @staticmethod
    def copy_m(m:list):
        return [[c for c in r] for r in m]

    @staticmethod
    def _mul(m1:list, m2:list, mod:int):
        return [[sum([(z1*z2)%mod for (z1,z2) in zip(x1,y2)])%mod for y2 in zip(*m2)] for x1 in m1]
    def mul(self, m:"matrix_mod"):
        assert self.col==m.row
        assert self.mod==m.mod
        return self.__class__(self.__class__._mul(self._mat, m._mat, self.mod), mod=self.mod)
    def __matmul__(self, other:"matrix_mod"):
        return self.mul(other)

    @classmethod
    def _pow(cls, m:list, n:int, mod:int):
        assert isinstance(n, int)
        assert n > 0
        ret = None
        mm = cls.copy_m(m)
        while n:
            if n&1:
                if ret is None:
                    ret = cls.copy_m(mm)
                else:
                    ret = cls._mul(mm, ret, mod)
            mm = cls._mul(mm, mm, mod)
            n >>= 1
        return ret
    def pow(self, n:int):
        assert self.row==self.col
        return self.__class__(self.__class__._pow(self._mat, n, self.mod), mod=self.mod)
    def __pow__(self, other:"matrix_mod"):
        print(other)
        return self.pow(other)
m = matrix_mod([[1,1,0],[1,0,1],[0,0,1]], mod=MOD)
m = m.pow(N-2)
m2 = matrix_mod([[0],[1],[1]], mod=MOD)
ans = matrix_mod([[1,1,0]], mod=MOD)@m @ m2

print(ans._mat[0][0])
0