結果

問題 No.1907 DETERMINATION
ユーザー qwewe
提出日時 2025-05-14 12:51:37
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 3,284 bytes
コンパイル時間 561 ms
コンパイル使用メモリ 82,732 KB
実行使用メモリ 140,488 KB
最終ジャッジ日時 2025-05-14 12:52:17
合計ジャッジ時間 8,089 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 3 TLE * 1 -- * 59
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 998244353

def main():
    import sys
    input = sys.stdin.read().split()
    ptr = 0
    N = int(input[ptr])
    ptr += 1
    
    M0 = []
    for _ in range(N):
        row = list(map(int, input[ptr:ptr+N]))
        ptr += N
        M0.append(row)
    
    M1 = []
    for _ in range(N):
        row = list(map(int, input[ptr:ptr+N]))
        ptr += N
        M1.append(row)
    
    # Evaluation points are x = 0, 1, ..., N
    xs = list(range(N + 1))
    ys = []
    
    for x in xs:
        # Compute M = M0 + x * M1
        M = []
        for i in range(N):
            row = []
            for j in range(N):
                val = (M0[i][j] + x * M1[i][j]) % MOD
                row.append(val)
            M.append(row)
        
        # Compute determinant using Gaussian elimination
        det = 1
        sign = 1
        mat = [row.copy() for row in M]
        for col in range(N):
            # Find pivot row
            pivot_row = None
            for r in range(col, N):
                if mat[r][col] != 0:
                    pivot_row = r
                    break
            if pivot_row is None:
                det = 0
                break
            # Swap with current row
            if pivot_row != col:
                mat[col], mat[pivot_row] = mat[pivot_row], mat[col]
                sign *= -1
            # Multiply determinant by the pivot element
            pivot_val = mat[col][col]
            det = (det * pivot_val) % MOD
            # Compute inverse of pivot
            inv_pivot = pow(pivot_val, MOD - 2, MOD)
            # Eliminate lower rows
            for r in range(col + 1, N):
                factor = (mat[r][col] * inv_pivot) % MOD
                for c in range(col, N):
                    mat[r][c] = (mat[r][c] - factor * mat[col][c]) % MOD
        if det != 0:
            det = (det * sign) % MOD
        ys.append(det)
    
    # Precompute d_i = product_{j != i} (x_i - x_j)
    d = []
    for i in range(N + 1):
        xi = xs[i]
        di = 1
        for j in range(N + 1):
            if j == i:
                continue
            term = (xi - xs[j]) % MOD
            di = (di * term) % MOD
        d.append(di)
    
    # Function to multiply a polynomial by (x - c)
    def multiply_poly(poly, c):
        # poly is a list of coefficients, returns new polynomial
        new_poly = [0] * (len(poly) + 1)
        for i in range(len(poly)):
            new_poly[i] = (new_poly[i] - poly[i] * c) % MOD
            new_poly[i + 1] = (new_poly[i + 1] + poly[i]) % MOD
        return new_poly
    
    # Compute the coefficients a_0 ... a_N
    a = [0] * (N + 1)
    for i in range(N + 1):
        # Compute numerator polynomial N_i(x) = product_{j != i} (x - x_j)
        numerator = [1]
        for j in range(N + 1):
            if j == i:
                continue
            numerator = multiply_poly(numerator, xs[j])
        
        # Compute inv_di
        inv_di = pow(d[i], MOD - 2, MOD)
        # Multiply numerator by inv_di and y_i
        term = [ (coeff * inv_di % MOD) * ys[i] % MOD for coeff in numerator ]
        # Add to a
        for k in range(len(term)):
            a[k] = (a[k] + term[k]) % MOD
    
    for coeff in a:
        print(coeff)

if __name__ == '__main__':
    main()
0