結果

問題 No.2435 Order All Company
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-08-24 16:50:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 46 ms / 2,000 ms
コード長 4,047 bytes
コンパイル時間 2,414 ms
コンパイル使用メモリ 212,048 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-24 16:50:34
合計ジャッジ時間 4,832 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,356 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 46 ms
4,384 KB
testcase_06 AC 46 ms
4,380 KB
testcase_07 AC 22 ms
4,380 KB
testcase_08 AC 45 ms
4,380 KB
testcase_09 AC 45 ms
4,384 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,384 KB
testcase_14 AC 42 ms
4,376 KB
testcase_15 AC 43 ms
4,380 KB
testcase_16 AC 35 ms
4,380 KB
testcase_17 AC 46 ms
4,376 KB
testcase_18 AC 30 ms
4,380 KB
testcase_19 AC 29 ms
4,376 KB
testcase_20 AC 31 ms
4,376 KB
testcase_21 AC 34 ms
4,380 KB
testcase_22 AC 41 ms
4,376 KB
testcase_23 AC 21 ms
4,380 KB
testcase_24 AC 34 ms
4,380 KB
testcase_25 AC 42 ms
4,376 KB
testcase_26 AC 35 ms
4,380 KB
testcase_27 AC 41 ms
4,376 KB
testcase_28 AC 31 ms
4,376 KB
testcase_29 AC 37 ms
4,384 KB
testcase_30 AC 35 ms
4,380 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 3 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const ll modc = 998244353;
class mint {
    ll x;
public:
    mint(ll x=0) : x((x%modc+modc)%modc) {}
    mint operator-() const {
      return mint(-x);
    }
    mint& operator+=(const mint& a) {
        if ((x += a.x) >= modc) x -= modc;
        return *this;
    }
    mint& operator-=(const mint& a) {
        if ((x += modc-a.x) >= modc) x -= modc;
        return *this;
    }
    mint& operator*=(const  mint& a) {
        (x *= a.x) %= modc;
        return *this;
    }
    mint operator+(const mint& a) const {
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint& a) const {
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint& a) const {
        mint res(*this);
        return res*=a;
    }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    mint inv() const {
        return pow(modc-2);
    }
    mint& operator/=(const mint& a) {
        return (*this) *= a.inv();
    }
    mint operator/(const mint& a) const {
        mint res(*this);
        return res/=a;
    }
    bool operator == (const mint& a) const{
        return x == a.x;
    }
    bool operator != (const mint& a) const{
        return x != a.x;
    }
    friend ostream& operator<<(ostream& os, const mint& m){
        os << m.x;
        return os;
    }
    friend istream& operator>>(istream& ip, mint &m) {
        ll t;
        ip >> t;
        m = mint(t);
        return ip;
    }
    ll val(){
        return x;
    }
};

template<typename T>
using matrix = vector<vector<T>>;

template<typename T>
void show(matrix<T> &a){
    for (int i=0; i<a.size(); i++){
        for (int j=0; j<a[i].size(); j++) cout << a[i][j] << " ";
        cout << endl;
    }
}

template<typename T>
matrix<T> dot(matrix<T> &a, matrix<T> &b){
    int N = a.size();
    matrix<T> c(N, vector<T>(N));
    for (int i=0; i<N; i++){
        for (int k=0; k<N; k++){
            for (int j=0; j<N; j++) c[i][j] += (a[i][k] * b[k][j]);
        }
    }
    return c;
}

template<typename T>
T det(matrix<T> a){
    int N = a.size();
    T res = 1;
    for (int i=0; i<N; i++){
        if (a[i][i] == 0){
            for (int j=i+1; j<N; j++){
                if (a[i][j] != 0){
                    swap(a[i], a[j]);
                    res *= -1;
                    break;
                }
            }
            if (a[i][i] == 0) return 0;
        }
        res *= a[i][i];
        T iv = (T) 1 / a[i][i];
        for (int k=i; k<N; k++) a[i][k] *= iv;
        for (int j=i+1; j<N; j++){
            T mag = a[j][i];
            for (int k=i; k<N; k++){
                a[j][k] -= mag * a[i][k];
            }
        }
    }

    return res;
}

template<typename T>
matrix<T> pow(matrix<T> a, ll N){
    int M = a.size();
    matrix<T> b(M, vector<T>(M));
    for (int i=0; i<M; i++) b[i][i] = 1;
    while(N){
        if (N % 2 == 1) b = dot(b, a);
        N >>= 1;
        a = dot(a, a);
    }
    return b;
}

int N, K;
vector<vector<pair<int, int>>> e;

mint solve(int x){
     matrix<mint> lap(N-1, vector<mint>(N-1));
    
     for (int i=0; i<K; i++){
         if (1<<i & x){
             for (auto [a, b] : e[i]){
                 if (a < N-1 && b < N-1){
                     lap[a][b] -= 1;
                     lap[b][a] -= 1;
                 }
                 if (a < N-1) lap[a][a] += 1;
                 if (b < N-1) lap[b][b] += 1;
             }
         }
     }

     return det(lap);
}

int main(){

    int t, a, b;
    cin >> N >> K;
    e.resize(K);
    for (int i=0; i<K; i++){
        cin >> t;
        e[i].resize(t);
        for (int j=0; j<t; j++){
            cin >> a >> b;
            a--; b--;
            e[i][j] = {a, b};
        }
    }

    mint ans=0;
    for (int i=1; i<(1<<K); i++){
        ans += solve(i) * ((K-__builtin_popcount(i)) % 2 == 0 ? 1 : -1);
    }

    cout << ans << endl;

    return 0;
}
0