結果

問題 No.2443 特殊線形群の標準表現
ユーザー MasKoaTSMasKoaTS
提出日時 2023-08-09 23:19:25
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,856 ms / 3,000 ms
コード長 5,650 bytes
コンパイル時間 522 ms
コンパイル使用メモリ 87,188 KB
実行使用メモリ 286,772 KB
最終ジャッジ日時 2023-08-16 22:28:29
合計ジャッジ時間 17,752 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 202 ms
83,008 KB
testcase_01 AC 202 ms
83,100 KB
testcase_02 AC 202 ms
83,012 KB
testcase_03 AC 198 ms
83,028 KB
testcase_04 AC 201 ms
83,004 KB
testcase_05 AC 200 ms
82,900 KB
testcase_06 AC 201 ms
83,032 KB
testcase_07 AC 202 ms
83,024 KB
testcase_08 AC 215 ms
83,048 KB
testcase_09 AC 201 ms
82,940 KB
testcase_10 AC 200 ms
83,028 KB
testcase_11 AC 201 ms
82,944 KB
testcase_12 AC 226 ms
83,896 KB
testcase_13 AC 403 ms
89,216 KB
testcase_14 AC 635 ms
108,752 KB
testcase_15 AC 1,845 ms
285,568 KB
testcase_16 AC 1,803 ms
285,656 KB
testcase_17 AC 1,794 ms
285,616 KB
testcase_18 AC 1,856 ms
285,676 KB
testcase_19 AC 1,844 ms
284,932 KB
testcase_20 AC 1,654 ms
286,772 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import itertools as iter
import collections as coll
import heapq as hq
import bisect as bis
from decimal import Decimal as dec
from functools import cmp_to_key
import math
import sys
#import pypyjit
#pypyjit.set_param('max_unroll_recursion=-1')
sys.setrecursionlimit(10 ** 6)
inp = sys.stdin.readline
input = lambda : inp()[:-1]
getN = lambda : int(inp())
getNs = lambda : map(int, inp().split())
getList = lambda :list(map(int, inp().split()))
getStrs = lambda n : [input() for _ in [0] * n]
def yexit(): print("Yes"); exit(0)
def nexit(): print("No"); exit(0)
pi = 3.141592653589793
mod = 1000000007
MOD = 998244353
INF = 4611686018427387903
dx = [1, 0, -1, 0];  dy = [0, 1, 0, -1]
#di = coll.defaultdict(int)

class ModInt:
    mod = 998244353

    def __init__(self, x):
        self.x = x % ModInt.mod

    @classmethod
    def set_mod(cls, mod: int) -> None:
        ModInt.mod = mod
        
    def __str__(self):
        return str(self.x)

    __repr__ = __str__

    def __add__(self, other):
        return (ModInt(self.x + other.x) if isinstance(other, ModInt) else ModInt(self.x + other))

    def __sub__(self, other):
        return (ModInt(self.x - other.x) if isinstance(other, ModInt) else ModInt(self.x - other))

    def __mul__(self, other):
        return (ModInt(self.x * other.x) if isinstance(other, ModInt) else ModInt(self.x * other))

    def __truediv__(self, other):
        return (ModInt(self.x * self.inv_mod(other.x, self.mod)) if isinstance(other, ModInt)
          else ModInt(self.x * self.inv_mod(other, self.mod)))

    def __pow__(self, other):
        return ModInt(pow(self.x, other.x, ModInt.mod)) if isinstance(other, ModInt) else ModInt(pow(self.x, other, ModInt.mod))

    __radd__ = __add__

    def __rsub__(self, other):
        return (ModInt(other.x - self.x) if isinstance(other, ModInt) else ModInt(other - self.x))

    __rmul__ = __mul__

    def __rtruediv__(self, other):
        return (ModInt(other.x * self.inv_mod(self.x, self.mod)) if isinstance(other, ModInt)
          else ModInt(other * self.inv_mod(self.x, self.mod)))

    def __rpow__(self, other):
        return ModInt(pow(other.x, self.x, ModInt.mod)) if isinstance(other, ModInt) else ModInt(pow(other, self.x, ModInt.mod))

    def __iadd__(self, other):
        self = self + other
        return self

    def __isub__(self, other):
        self = self - other
        return self

    def __imul__(self, other):
        self = self * other
        return self

    def __itruediv__(self, other):
        self = self / other
        return self

    @classmethod
    def inv_gcd(cls, n, m):
        n %= m
        if(n == 0):
            return m, 0
        s, t, m0, m1 = m, n, 0, 1
        while(t):
            u = s // t
            s -= t * u
            m0 -= m1 * u
            m0, m1, s, t = m1, m0, t, s
        if(m0 < 0):
            m0 += m // s
        return s, m0

    @classmethod
    def inv_mod(cls, n, m):
        _, im = cls.inv_gcd(n, m)
        return im

    @classmethod
    def prod(cls, A):
        ret = ModInt(1)
        for k in A:
            ret *= k
        return ret

    @classmethod
    def nPk(cls, n, k):
        if(isinstance(n, ModInt)):
            n = n.x
        if(isinstance(k, ModInt)):
            k = k.x
        return cls.prod([*range(n - k + 1, n + 1)])
    
    @classmethod
    def nCk(cls, n, k):
        if(isinstance(n, ModInt)):
            n = n.x
        if(isinstance(k, ModInt)):
            k = k.x
        r = min(n - k, k)
        return cls.nPk(n, r) / cls.nPk(r, r)

class Matrix:
    def __init__(self, A: list):
        if not(A and isinstance(A[0], list)):
            A = [A]
        self.A = A
        self.row = len(A)
        self.col = len(A[0])
        self.I = None
        if(self.row == self.col):
            self.I = self.calc_I()

    def set_A(self, A: list) -> None:
        self.A = A

    def calc_I(self) -> list:
        ret = [[int(i == j) for j in range(self.col)] for i in range(self.row)]
        return ret

    def __str__(self):
        return str(self.A)

    __repr__ = __str__

    def __add__(self, other):
        ret = Matrix(self)
        for i in range(self.row):
            for j in range(self.col):
                ret.A[i][j] = self.A[i][j] + other.A[i][j]
        return ret

    def __sub__(self, other):
        ret = Matrix(self)
        for i in range(self.row):
            for j in range(self.col):
                ret.A[i][j] = self.A[i][j] - other.A[i][j]
        return ret

    def __mul__(self, other):
        assert(self.col == other.row)
        ret = Matrix([[0]*other.col for _ in [0]*self.row])
        for i in range(self.row):
            for j in range(other.col):
                ret.A[i][j] = sum(self.A[i][k] * other.A[k][j] for k in range(self.col))
        return ret

    def __pow__(self, other: int):
        mat = self
        ret = Matrix(self.I)
        n = other
        while(n):
            if(n & 1):
                ret = mat * ret
            mat = mat * mat
            n >>= 1
        return ret
 
 
"""
Main Code
"""

n, b, q = getNs()
ModInt.set_mod(b)
A_lis = [[[ModInt(x) for x in getNs()] for _ in [0] * 2] for _ in [0] * n]
query = [getList() for _ in [0] * q]

As = [Matrix(A) for A in A_lis]
A_prod = Matrix(As[0].calc_I())
Bs = [A_prod]
for A in As:
    A_prod = A * A_prod
    Bs.append(A_prod)

# print(Bs)

def inverse(A):
    return Matrix([[A.A[1][1], ModInt(0)-A.A[0][1]], [ModInt(0)-A.A[1][0], A.A[0][0]]])

for l, r, x, y in query:
    vec = Matrix([[ModInt(x)], [ModInt(y)]])
    ans = Bs[r] * inverse(Bs[max(0, l)]) * vec
    print(ans.A[0][0], ans.A[1][0])
0