結果
問題 | No.2435 Order All Company |
ユーザー |
![]() |
提出日時 | 2023-08-24 16:50:29 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 43 ms / 2,000 ms |
コード長 | 4,047 bytes |
コンパイル時間 | 2,156 ms |
コンパイル使用メモリ | 208,672 KB |
最終ジャッジ日時 | 2025-02-16 13:05:42 |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 36 |
ソースコード
#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;}