結果

問題 No.3227 Matrix Query
ユーザー Yudai0606Nazo
提出日時 2025-08-08 21:50:20
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,300 ms / 8,000 ms
コード長 4,102 bytes
コンパイル時間 5,158 ms
コンパイル使用メモリ 275,200 KB
実行使用メモリ 9,856 KB
最終ジャッジ日時 2025-08-08 21:50:48
合計ジャッジ時間 26,607 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#include <cassert>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
using mint = modint;
//using mint = modint998244353;
//using mint = modint1000000007;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repu(i, s, t) for (int i = (int)(s); i < (int)(t); i++)
#define repd(i, s, t) for (int i = (int)(s)-1; i >= (int)(t); i--)
#define all(v) v.begin(), v.end()
void _u() { cerr << endl; } template <class H, class... T> void _u(H&& h, T&&... t) { cerr << h << ", "; _u(move(t)...); }
#define U(...) { cerr << #__VA_ARGS__ << ": "; _u(__VA_ARGS__); }
template<typename T> bool chmax(T &a, const T b) { if(a >= b) return false; a = b; return true; }
template<typename T> bool chmin(T &a, const T b) { if(a <= b) return false; a = b; return true; }
template<typename T> istream& operator>>(istream &in, vector<T> &a) { for(T &x: a) in >> x; return in; }
template<typename T> ostream& operator<<(ostream &out, const vector<T> &a) { for(const T &x: a) out << x << ' '; return out; }
const int di[] = {1, 0, -1, 0, 1, 1, -1, -1, 0};
const int dj[] = {0, 1, 0, -1, -1, 1, 1, -1, 0};


template <typename T=double>
struct Matrix {
    vector<vector<T>> mat;
    int m, n;
    Matrix(initializer_list<initializer_list<T>> init) {
        mat.resize(0);
        for(const auto &row : init) {
            mat.emplace_back(row);
        }
        m = mat.size();
        n = mat.empty() ? 0 : mat[0].size();
    }
    Matrix(vector<vector<T>> &mat_): mat(mat_), m(int(mat_.size())), n(mat.empty() ? 0 : int(mat_[0].size())) {}
    Matrix(int x, int y):mat(vector(x, vector<T>(y))), m(x), n(y) {}
    Matrix(int x): m(x), n(x) {
        mat = vector(x, vector<T> (x));
        rep(i, x) mat[i][i] = 1;
    }
    vector<T> &operator[](int x) { return mat[x]; }
    Matrix<T> &operator+=(const Matrix<T> &other) {
        assert(m == other.m && n == other.n);
        rep(i, m) rep(j, n) *this[i][j] += other[i][j];
        return *this;
    }
    Matrix<T> operator+(const Matrix<T> &other) { return Matrix<T>(*this) += other; }
    Matrix<T> &operator*=(const Matrix<T> &other) { return *this = *this * other; }
    Matrix<T> operator*(const Matrix<T> &other) {
        assert(n == other.m);
        Matrix<T> result(m, other.n);
        rep(i, m) rep(j, other.n) rep(k, n) result.mat[i][j] += mat[i][k] * other.mat[k][j];
        return result;
    }
    Matrix<T> pow(ll x) {
        assert(m == n);
        if(x == 0) return Matrix<T>(m);
        Matrix<T> result = pow(x / 2);
        result = result * result;
        return (x % 2 == 0 ? result : result * *this);
    }
    T dat() {
        assert(m == n);
        T result = 1;
        rep(i, m) {
            int pivot = -1;
            repu(j, i, m) if(mat[j][i] != 0) {
                pivot = j;
                break;
            }
            if(pivot == -1) return 0;
            if(pivot != i) {
                swap(mat[i], mat[pivot]);
                result *= -1;
            }
            result *= mat[i][i];
            T inv = 1 / mat[i][i];
            repu(j, i + 1, m) {
                T factor = mat[j][i] * inv;
                repu(k, i, n) mat[j][k] -= mat[i][k] * factor;
            }
        }
        return result;
    }
};

using S = Matrix<mint>;
S op(S l, S r) { return l * r; }
S e() { return S(2); }

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int k, n;
    cin >> k >> n;
    mint::set_mod(k);
    segtree<S, op, e> st(n);
    rep(i, n) {
        int a11, a12, a21, a22;
        cin >> a11 >> a12 >> a21 >> a22;
        st.set(i, Matrix<mint>({{a11, a12}, {a21, a22}}));
    }
    int q;
    cin >> q;
    while(q--) {
        int i, l, r, y11, y12, y21, y22;
        cin >> i >> l >> r >> y11 >> y12 >> y21 >> y22;
        i--, l--;
        st.set(i, Matrix<mint>({{y11, y12}, {y21, y22}}));
        auto res = st.prod(l, r);
        cout << res[0][0].val() << " " << res[0][1].val() << '\n';
        cout << res[1][0].val() << " " << res[1][1].val() << '\n';
    }
    return 0;
}
0