結果

問題 No.2825 Sum of Scores of Sets of Specified Sections
ユーザー 👑 Nachia
提出日時 2024-07-26 22:45:44
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 93 ms / 3,000 ms
コード長 5,035 bytes
コンパイル時間 1,231 ms
コンパイル使用メモリ 102,728 KB
最終ジャッジ日時 2025-02-23 18:51:38
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifdef NACHIA
#define _GLIBCXX_DEBUG
#else
#define NDEBUG
#endif
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
#include <array>
#include <cmath>
using i64 = long long;
using u64 = unsigned long long;
#define rep(i,n) for(int i=0; i<int(n); i++)
#define repr(i,n) for(int i=int(n)-1; i>=0; i--)
const i64 INF = 1001001001001001001;
const char* yn(bool x){ return x ? "Yes" : "No"; }
template<typename A> void chmin(A& l, const A& r){ if(r < l) l = r; }
template<typename A> void chmax(A& l, const A& r){ if(l < r) l = r; }
template<typename A> using nega_queue = std::priority_queue<A,std::vector<A>,std::greater<A>>;
template<class R> auto ComparingBy(R f){ return [g=std::move(f)](auto l, auto r) -> bool { return g(l) < g(r); }; }
#include <atcoder/modint>
#include <cassert>

namespace nachia{

template<class Elem>
struct MatrixModulo{
private:
    int h;
    int w;
    std::vector<Elem> elems;
public:
    
    MatrixModulo(int new_h=0, int new_w=0)
        : h(new_h), w(new_w), elems(h*w, Elem(0)){}
    MatrixModulo(const MatrixModulo &) = default;
    int numRow() const { return h; }
    int numColumn() const { return w; }
    int height() const { return numRow(); }
    int width() const { return numColumn(); }
    typename std::vector<Elem>::iterator operator[](int y){ return elems.begin() + (y*w); }
    typename std::vector<Elem>::const_iterator operator[](int y) const { return elems.begin() + (y*w); }
    static MatrixModulo Identity(int n){
        auto res = MatrixModulo(n,n);
        for(int i=0; i<n; i++) res[i][i]=Elem(1);
        return res;
    }
    MatrixModulo transposed() const {
        auto res = MatrixModulo(w,h);
        for(int i=0; i<h; i++) for(int j=0; j<w; j++) res[j][i] = elems[i*w+j];
        return res;
    }
    void swapColumns(int x1, int x2){
        assert(0 <= x1 && x1 < numColumn());
        assert(0 <= x2 && x2 < numColumn());
        for(int y=0; y<numRow(); y++) std::swap((*this)[y][x1], (*this)[y][x2]);
    }
    void swapRows(int y1, int y2){
        assert(0 <= y1 && y1 < numRow());
        assert(0 <= y2 && y2 < numRow());
        for(int x=0; x<numColumn(); x++) std::swap((*this)[y1][x], (*this)[y2][x]);
    }
    MatrixModulo operator*(const MatrixModulo& r) const {
        assert(width() == r.height());
        auto res = MatrixModulo(h, r.w);
        for(int i=0; i<h; i++) for(int j=0; j<w; j++) for(int k=0; k<r.w; k++){
            res[i][k] += (*this)[i][j] * r[j][k];
        }
        return res;
    }
    std::vector<Elem> operator*(const std::vector<Elem>& r) const {
        assert(width() == int(r.size()));
        auto res = std::vector<Elem>(h);
        for(int i=0; i<h; i++) for(int j=0; j<w; j++){
            res[i] += (*this)[i][j] * r[j];
        }
        return res;
    }
    Elem det() const {
        assert(height() == width());
        MatrixModulo g = *this;
        Elem ans = 1;
        for(int i=0; i<h; i++){
            int tg = -1;
            for(int j=i; j<h; j++){ if(g[j][i].val() != 0) tg = j; }
            if(tg == -1) return 0;
            if(tg != i) ans = -ans;
            for(int j=0; j<h; j++) std::swap(g[i][j], g[tg][j]);
            tg = i;
            ans *= g[i][i];
            Elem const_coeff = g[i][i].inv();
            for(int j=0; j<h; j++) g[i][j] *= const_coeff;
            for(int j=i+1; j<h; j++) for(int k=h-1; k>=i; k--) g[j][k] -= g[j][i] * g[i][k];
        }
        return ans;
    }
    int rank() const {
        if(height() == 0 || width() == 0) return 0;
        MatrixModulo g = *this;
        int y = 0;
        for(int d=0; d<w; d++){
            if(y == h) break;
            int tg = -1;
            for(int i=y; i<h; i++){ if(g[i][d].val() != 0){ tg = i; break; } }
            if(tg == -1) continue;
            for(int j=d; j<w; j++) std::swap(g[y][j], g[tg][j]);
            tg = y;
            Elem const_coeff = g[y][d].inv();
            for(int j=d; j<w; j++) g[y][j] *= const_coeff;
            for(int i=y+1; i<h; i++) for(int j=w-1; j>=d; j--) g[i][j] -= g[i][d] * g[y][j];
            y++;
        }
        return y;
    }
    MatrixModulo pow(unsigned long long i){
        auto a = *this;
        auto res = Identity(height());
        while(i){
            if(i%2) res = res * a;
            a = a * a; i /= 2;
        }
        return res;
    }
};


} // namespace nachia
using Modint = atcoder::static_modint<998244353>;
using namespace std;

void testcase(){
    using Matrix = nachia::MatrixModulo<Modint>;
    i64 H, W; cin >> H >> W;
    auto A = Matrix(H, W);
    rep(y,H) rep(x,W){ int a; cin >> a; A[y][x] = Modint::raw(a); }
    auto B = Matrix(H, W);
    rep(y,H) rep(x,W){ int a; cin >> a; B[y][x] = -Modint::raw(a); }
    B = B.transposed();
    auto mat = Matrix::Identity(H);
    rep(y,H) rep(x,W) rep(f,H) mat[y][f] -= A[y][x] * B[x][f];
    cout << (mat.det() - 1).val() << endl;
}

int main(){
    ios::sync_with_stdio(false); cin.tie(nullptr);
    testcase();
    return 0;
}
0