結果

問題 No.2405 Minimal Matrix Decomposition
ユーザー Fu_LFu_L
提出日時 2024-12-26 10:23:07
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 136 ms / 2,000 ms
コード長 9,473 bytes
コンパイル時間 7,384 ms
コンパイル使用メモリ 284,772 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-26 10:23:21
合計ジャッジ時間 14,196 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,248 KB
testcase_02 AC 3 ms
5,248 KB
testcase_03 AC 3 ms
5,248 KB
testcase_04 AC 23 ms
5,248 KB
testcase_05 AC 136 ms
5,248 KB
testcase_06 AC 3 ms
5,248 KB
testcase_07 AC 4 ms
5,248 KB
testcase_08 AC 3 ms
5,248 KB
testcase_09 AC 3 ms
5,248 KB
testcase_10 AC 51 ms
5,248 KB
testcase_11 AC 58 ms
5,248 KB
testcase_12 AC 4 ms
5,248 KB
testcase_13 AC 15 ms
5,248 KB
testcase_14 AC 15 ms
5,248 KB
testcase_15 AC 7 ms
5,248 KB
testcase_16 AC 7 ms
5,248 KB
testcase_17 AC 28 ms
5,248 KB
testcase_18 AC 17 ms
5,248 KB
testcase_19 AC 21 ms
5,248 KB
testcase_20 AC 5 ms
5,248 KB
testcase_21 AC 7 ms
5,248 KB
testcase_22 AC 71 ms
5,248 KB
testcase_23 AC 5 ms
5,248 KB
testcase_24 AC 4 ms
5,248 KB
testcase_25 AC 5 ms
5,248 KB
testcase_26 AC 37 ms
5,248 KB
testcase_27 AC 15 ms
5,248 KB
testcase_28 AC 33 ms
5,248 KB
testcase_29 AC 10 ms
5,248 KB
testcase_30 AC 42 ms
5,248 KB
testcase_31 AC 18 ms
5,248 KB
testcase_32 AC 4 ms
5,248 KB
testcase_33 AC 32 ms
5,248 KB
testcase_34 AC 9 ms
5,248 KB
testcase_35 AC 2 ms
5,248 KB
testcase_36 AC 13 ms
5,248 KB
testcase_37 AC 18 ms
5,248 KB
testcase_38 AC 21 ms
5,248 KB
testcase_39 AC 33 ms
5,248 KB
testcase_40 AC 16 ms
5,248 KB
testcase_41 AC 4 ms
5,248 KB
testcase_42 AC 40 ms
5,248 KB
testcase_43 AC 12 ms
5,248 KB
testcase_44 AC 11 ms
5,248 KB
testcase_45 AC 5 ms
5,248 KB
testcase_46 AC 10 ms
5,248 KB
testcase_47 AC 13 ms
5,248 KB
testcase_48 AC 6 ms
5,248 KB
testcase_49 AC 3 ms
5,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<long long, long long>;
#define rep(i, a, b) for(long long i = (a); i < (b); ++i)
#define rrep(i, a, b) for(long long i = (a); i >= (b); --i)
constexpr long long inf = 4e18;
struct SetupIO {
    SetupIO() {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout << fixed << setprecision(30);
    }
} setup_io;
struct Barrett {
    explicit Barrett(const unsigned int m)
        : _m(m), im((unsigned long long)(-1) / m + 1) {}
    inline unsigned int umod() const {
        return _m;
    }
    inline unsigned int mul(const unsigned int a, const unsigned int b) const {
        unsigned long long z = a;
        z *= b;
        const unsigned long long x = (unsigned long long)(((unsigned __int128)(z)*im) >> 64);
        unsigned int v = (unsigned int)(z - x * _m);
        if(_m <= v) v += _m;
        return v;
    }

   private:
    unsigned int _m;
    unsigned long long im;
};
template <int id>
struct DynamicModint {
    using mint = DynamicModint;
    static int mod() {
        return (int)bt.umod();
    }
    static void set_mod(const int m) {
        assert(1 <= m);
        bt = Barrett(m);
    }
    static mint raw(const int v) {
        mint a;
        a._v = v;
        return a;
    }
    DynamicModint()
        : _v(0) {}
    template <class T>
    DynamicModint(const T& v) {
        static_assert(is_integral_v<T>);
        if(is_signed_v<T>) {
            long long x = (long long)(v % (long long)(umod()));
            if(x < 0) x += umod();
            _v = (unsigned int)(x);
        } else _v = (unsigned int)(v % umod());
    }
    unsigned int val() const {
        return _v;
    }
    mint& operator++() {
        ++_v;
        if(_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if(_v == 0) _v = umod();
        --_v;
        return *this;
    }
    mint operator++(int) {
        mint res = *this;
        ++*this;
        return res;
    }
    mint operator--(int) {
        mint res = *this;
        --*this;
        return res;
    }
    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if(_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v += mod() - rhs._v;
        if(_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        _v = bt.mul(_v, rhs._v);
        return *this;
    }
    mint& operator/=(const mint& rhs) {
        return *this *= rhs.inv();
    }
    mint operator+() const {
        return *this;
    }
    mint operator-() const {
        return mint() - *this;
    }
    mint pow(long long n) const {
        assert(0 <= n);
        if(n == 0) return 1;
        mint x = *this, r = 1;
        while(1) {
            if(n & 1) r *= x;
            n >>= 1;
            if(n == 0) return r;
            x *= x;
        }
    }
    mint inv() const {
        const auto eg = inv_gcd(_v, mod());
        assert(eg.first == 1);
        return eg.second;
    }
    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }
    friend istream& operator>>(istream& in, mint& x) {
        long long a;
        in >> a;
        x = a;
        return in;
    }
    friend ostream& operator<<(ostream& out, const mint& x) {
        return out << x.val();
    }

   private:
    unsigned int _v = 0;
    static Barrett bt;
    inline static unsigned int umod() {
        return bt.umod();
    }
    inline static pair<long long, long long> inv_gcd(const long long a, const long long b) {
        if(a == 0) return {b, 0};
        long long s = b, t = a, m0 = 0, m1 = 1;
        while(t) {
            const long long u = s / t;
            s -= t * u;
            m0 -= m1 * u;
            swap(s, t);
            swap(m0, m1);
        }
        if(m0 < 0) m0 += b / s;
        return {s, m0};
    }
};
template <int id>
Barrett DynamicModint<id>::bt(998244353);
using modint = DynamicModint<-1>;
using mint = modint;
template <typename T>
struct Matrix {
    Matrix(const int h, const int w, const T& val = 0)
        : h(h), w(w), A(h, vector<T>(w, val)) {}
    int H() const {
        return h;
    }
    int W() const {
        return w;
    }
    const vector<T>& operator[](const int i) const {
        assert(0 <= i and i < h);
        return A[i];
    }
    vector<T>& operator[](const int i) {
        assert(0 <= i and i < h);
        return A[i];
    }
    static Matrix I(const int n) {
        Matrix mat(n, n);
        for(int i = 0; i < n; ++i) mat[i][i] = 1;
        return mat;
    }
    Matrix& operator+=(const Matrix& B) {
        assert(h == B.h and w == B.w);
        for(int i = 0; i < h; ++i) {
            for(int j = 0; j < w; ++j) {
                (*this)[i][j] += B[i][j];
            }
        }
        return (*this);
    }
    Matrix& operator-=(const Matrix& B) {
        assert(h == B.h and w == B.w);
        for(int i = 0; i < h; ++i) {
            for(int j = 0; j < w; ++j) {
                (*this)[i][j] -= B[i][j];
            }
        }
        return (*this);
    }
    Matrix& operator*=(const Matrix& B) {
        assert(w == B.h);
        vector<vector<T>> C(h, vector<T>(B.w, 0));
        for(int i = 0; i < h; ++i) {
            for(int k = 0; k < w; ++k) {
                for(int j = 0; j < B.w; ++j) {
                    C[i][j] += (*this)[i][k] * B[k][j];
                }
            }
        }
        A.swap(C);
        return (*this);
    }
    Matrix& pow(long long t) {
        assert(h == w);
        assert(t >= 0);
        Matrix B = Matrix::I(h);
        while(t > 0) {
            if(t & 1ll) B *= (*this);
            (*this) *= (*this);
            t >>= 1ll;
        }
        A.swap(B.A);
        return (*this);
    }
    Matrix operator+(const Matrix& B) const {
        return (Matrix(*this) += B);
    }
    Matrix operator-(const Matrix& B) const {
        return (Matrix(*this) -= B);
    }
    Matrix operator*(const Matrix& B) const {
        return (Matrix(*this) *= B);
    }
    bool operator==(const Matrix& B) const {
        assert(h == B.H() and w == B.W());
        for(int i = 0; i < h; ++i) {
            for(int j = 0; j < w; ++j) {
                if(A[i][j] != B[i][j]) return false;
            }
        }
        return true;
    }
    bool operator!=(const Matrix& B) const {
        assert(h == B.H() and w == B.W());
        for(int i = 0; i < h; ++i) {
            for(int j = 0; j < w; ++j) {
                if(A[i][j] != B[i][j]) return true;
            }
        }
        return false;
    }

   private:
    int h, w;
    vector<vector<T>> A;
};
template <typename T>
pair<int, T> gauss_elimination(Matrix<T>& a, int pivot_end = -1) {
    const int h = a.H(), w = a.W();
    int rank = 0;
    assert(-1 <= pivot_end and pivot_end <= w);
    if(pivot_end == -1) pivot_end = w;
    T det = 1;
    for(int j = 0; j < pivot_end; ++j) {
        int idx = -1;
        for(int i = rank; i < h; ++i) {
            if(a[i][j] != T(0)) {
                idx = i;
                break;
            }
        }
        if(idx == -1) {
            det = 0;
            continue;
        }
        if(rank != idx) det = -det, swap(a[rank], a[idx]);
        det *= a[rank][j];
        if(a[rank][j] != T(1)) {
            const T coeff = T(1) / a[rank][j];
            for(int k = j; k < w; ++k) a[rank][k] *= coeff;
        }
        for(int i = 0; i < h; ++i) {
            if(i == rank) continue;
            if(a[i][j] != T(0)) {
                const T coeff = a[i][j] / a[rank][j];
                for(int k = j; k < w; ++k) a[i][k] -= a[rank][k] * coeff;
            }
        }
        ++rank;
    }
    return {rank, det};
}
int main(void) {
    int p, n, m;
    cin >> p >> n >> m;
    mint::set_mod(p);
    Matrix<mint> a(n, m);
    bool flag = true;
    rep(i, 0, n) {
        rep(j, 0, m) {
            cin >> a[i][j];
            if(a[i][j].val() != 0) {
                flag = false;
            }
        }
    }
    Matrix<mint> b = a;
    int r = gauss_elimination(b).first;
    if(flag) r = 1;
    if(n * m <= (n + m) * r) {
        cout << 1 << '\n';
        cout << n << ' ' << m << '\n';
        rep(i, 0, n) {
            rep(j, 0, m) {
                cout << a[i][j] << " \n"[j + 1 == m];
            }
        }
    } else {
        cout << 2 << '\n';
        cout << n << ' ' << r << '\n';
        vector<int> idx(r);
        rep(i, 0, r) {
            rep(j, 0, m) {
                if(b[i][j].val() != 0) {
                    idx[i] = j;
                    break;
                }
            }
        }
        rep(i, 0, n) {
            rep(j, 0, r) {
                cout << a[i][idx[j]] << " \n"[j + 1 == r];
            }
        }
        cout << r << ' ' << m << '\n';
        rep(i, 0, r) {
            rep(j, 0, m) {
                cout << b[i][j] << " \n"[j + 1 == m];
            }
        }
    }
}
0