• Wannafly Winter Camp 2020 Day 7D 方阵的行列式


    于是去弄了个板子来

    #include <bits/stdc++.h>
    using namespace std;
    #define int long long
    
    const int mod = 998244353;
    const int N = 505;
    
    int qpow(int p,int q) {return ((q&1)?p:1) * (q?qpow(p*p%mod,q>>1):1) % mod;}
    
    int n, Q;
    
    // Input: a[][],n
    // Method: build() modify(i,j,x)
    // Output: inv[][], ans
    struct matrix {
        int a[N][N], st[N][N], inv[N][N], ans, n;
        void build() {
            ans=1;
            for(int i=1;i<=n;i++) {
                for(int j=1;j<=n;j++) {
                    st[i][j]=a[i][j];
                }
            }
            for (int i = 1; i <= n; i++) inv[i][i] = 1;
            for (int i = 1; i <= n; i++) {
                for (int j = i; j <= n; j++) {
                    if (a[j][i]) {
                        for (int k = 1; k <= n; k++) {
                            swap(a[i][k], a[j][k]);
                            swap(inv[i][k], inv[j][k]);
                        }
                        if (j > i) ans = (mod - ans) % mod;
                        break;
                    }
                }
                int K = qpow(a[i][i], mod-2);
                for (int j = i+1; j <= n; j++) {
                    int tmp = K * a[j][i] % mod;
                    for (int k = 1; k <= n; k++) {
                        a[j][k] = (a[j][k] + mod - tmp * a[i][k] % mod) % mod;
                        inv[j][k] = (inv[j][k] + mod - tmp * inv[i][k] % mod) % mod;
                    }
                }
            }
            for (int i = n; i >= 1; i--) {
                int K = qpow(a[i][i], mod-2);
                ans = ans * a[i][i] % mod;
                for (int j = 1; j <= n; j++) {
                    a[i][j] = a[i][j] * K % mod;
                    inv[i][j] = inv[i][j] * K % mod;
                }
                for (int j = 1; j < i; j++) {
                    int tmp = a[j][i];
                    for (int k = 1; k <= n; k++) {
                        a[j][k] = (a[j][k] + mod - tmp * a[i][k] % mod) % mod;
                        inv[j][k] = (inv[j][k] + mod - tmp * inv[i][k] % mod) % mod;
                    }
                }
            }
        }
        void modify(int x,int y,int z) {
            int t = (z + mod - st[x][y]) % mod;
            st[x][y] = z;
            for (int i = 1; i <= n; i++) {
                a[i][y] = (a[i][y] + t * inv[i][x] % mod) % mod;
            }
            {
                int i = y;
                int K = qpow(a[i][i], mod-2);
                ans = ans * a[i][i] % mod;
                for (int j = 1; j <= n; j++) {
                    a[i][j] = a[i][j] * K % mod;
                    inv[i][j] = inv[i][j] * K % mod;
                }
                for (int j = 1; j <= n; j++) {
                    if (j == i) continue;
                    int tmp = a[j][i];
                    for (int k = 1; k <= n; k++) {
                        a[j][k] = (a[j][k] + mod - tmp * a[i][k] % mod) % mod;
                        inv[j][k] = (inv[j][k] + mod - tmp * inv[i][k] % mod) % mod;
                    }
                }
            }
        }
    } mat;
    
    signed main() {
        scanf("%d%d", &n, &Q);
        mat.n = n;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                scanf("%d", &mat.a[i][j]);
            }
        }
        mat.build();
    
        while(Q--) {
            int t1,t2,t3;
            scanf("%d%d%d", &t1, &t2, &t3);
            mat.modify(t1,t2,t3);
            printf("%d
    ", mat.ans);
        }
    }
    
    /*
    2 3
    0 1
    1 0
    1 1 1
    2 1 2
    2 2 1
    [Output]
    998244352
    998244351
    998244352
    */
    
  • 相关阅读:
    <C> 链表 双向链表 栈 队列
    <C> 结构体
    <C> getchar()函数 如何把getchar()到的字符串存起来的实际应用
    DataSet转换为泛型集合和DataRow 转成 模型类
    对DataSet,DataRow,DateTable转换成相应的模型
    Json对象与Json字符串互转(4种转换方式)
    Android开发 使用HBuilder的缓存方法
    MIT_AI公开课p1p2学习笔记
    LeetCode_02 两数相加【链表】
    leetcode_01两数之和
  • 原文地址:https://www.cnblogs.com/mollnn/p/12376794.html
Copyright © 2020-2023  润新知