結果

問題 No.2435 Order All Company
ユーザー woodywoodywoodywoody
提出日時 2023-08-18 23:55:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 25 ms / 2,000 ms
コード長 18,040 bytes
コンパイル時間 3,962 ms
コンパイル使用メモリ 239,620 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-06 06:47:24
合計ジャッジ時間 5,244 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 23 ms
5,376 KB
testcase_06 AC 24 ms
5,376 KB
testcase_07 AC 13 ms
5,376 KB
testcase_08 AC 25 ms
5,376 KB
testcase_09 AC 24 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 21 ms
5,376 KB
testcase_15 AC 22 ms
5,376 KB
testcase_16 AC 16 ms
5,376 KB
testcase_17 AC 23 ms
5,376 KB
testcase_18 AC 14 ms
5,376 KB
testcase_19 AC 12 ms
5,376 KB
testcase_20 AC 15 ms
5,376 KB
testcase_21 AC 16 ms
5,376 KB
testcase_22 AC 20 ms
5,376 KB
testcase_23 AC 9 ms
5,376 KB
testcase_24 AC 15 ms
5,376 KB
testcase_25 AC 21 ms
5,376 KB
testcase_26 AC 17 ms
5,376 KB
testcase_27 AC 21 ms
5,376 KB
testcase_28 AC 15 ms
5,376 KB
testcase_29 AC 17 ms
5,376 KB
testcase_30 AC 16 ms
5,376 KB
testcase_31 AC 1 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 3 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
#define rep(i,b) for(int i=0;i<b;i++)
#define rrep(i,b) for(int i=b-1;i>=0;i--)
#define rep1(i,b) for(int i=1;i<b;i++)
#define repx(i,x,b) for(int i=x;i<b;i++)
#define rrepx(i,x,b) for(int i=b-1;i>=x;i--)
#define fore(i,a) for(auto& i:a)
#define rng(x) (x).begin(), (x).end()
#define rrng(x) (x).rbegin(), (x).rend()
#define sz(x) ((int)(x).size())
#define pb push_back
#define fi first
#define se second
#define pcnt __builtin_popcountll

using namespace std;
using namespace atcoder;

using ll = long long;
using ld = long double;
template<typename T> using mpq = priority_queue<T, vector<T>, greater<T>>;
template<typename T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<typename T> bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template<typename T> ll sumv(const vector<T>&a){ll res(0);for(auto&&x:a)res+=x;return res;}
bool yn(bool a) { if(a) {cout << "Yes" << endl; return true;} else {cout << "No" << endl; return false;}}
#define retval(x) {cout << #x << endl; return;}
#define cout2(x,y) cout << x << " " << y << endl;
#define coutp(p) cout << p.fi << " " << p.se << endl;
#define out cout << ans << endl;
#define outd cout << fixed << setprecision(20) << ans << endl;
#define outm cout << ans.val() << endl;
#define outv fore(yans , ans) cout << yans << "\n";
#define outdv fore(yans , ans) cout << yans.val() << "\n";
#define coutv(v) {fore(vy , v) {cout << vy << " ";} cout << endl;}
#define coutv2(v) fore(vy , v) cout << vy << "\n";
#define coutvm(v) {fore(vy , v) {cout << vy.val() << " ";} cout << endl;}
#define coutvm2(v) fore(vy , v) cout << vy.val() << "\n";
using pll = pair<ll,ll>;using pil = pair<int,ll>;using pli = pair<ll,int>;using pii = pair<int,int>;using pdd = pair<ld,ld>;
using vi = vector<int>;using vd = vector<ld>;using vl = vector<ll>;using vs = vector<string>;using vb = vector<bool>;
using vpii = vector<pii>;using vpli = vector<pli>;using vpll = vector<pll>;using vpil = vector<pil>;
using vvi = vector<vector<int>>;using vvl = vector<vector<ll>>;using vvs = vector<vector<string>>;using vvb = vector<vector<bool>>;
using vvpii = vector<vector<pii>>;using vvpli = vector<vector<pli>>;using vvpll = vector<vpll>;using vvpil = vector<vpil>;
using mint = modint998244353;
//using mint = modint1000000007;
//using mint = dynamic_modint<0>;
using vm = vector<mint>;
using vvm = vector<vector<mint>>;
vector<int> dx={1,0,-1,0,1,1,-1,-1},dy={0,1,0,-1,1,-1,1,-1};
ll gcd(ll a, ll b) { return a?gcd(b%a,a):b;}
ll lcm(ll a, ll b) { return a/gcd(a,b)*b;}
#define yes {cout <<"Yes"<<endl;}
#define no {cout <<"No"<<endl;}
const double eps = 1e-10;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#ifdef MY_LOCAL_DEBUG
#define show(x) cerr<<#x<<" = "<<x<<endl
#define showp(p) cerr<<#p<<" = "<<p.fi<<" : "<<p.se<<endl
#define show2(x,y) cerr<<#x<<" = "<<x<<" : "<<#y<<" = "<<y<<endl
#define show3(x,y,z) cerr<<#x<<" = "<<x<<" : "<<#y<<" = "<<y<<" : "<<#z<<" = "<<z<<endl
#define show4(x,y,z,x2) cerr<<#x<<" = "<<x<<" : "<<#y<<" = "<<y<<" : "<<#z<<" = "<<z<<" : "<<#x2<<" = "<<x2<<endl
#define test(x) cout << "test" << x << endl
#define showv(v) {fore(vy , v) {cout << vy << " ";} cout << endl;}
#define showv2(v) fore(vy , v) cout << vy << "\n";
#define showvm(v) {fore(vy , v) {cout << vy.val() << " ";} cout << endl;}
#define showvm2(v) fore(vy , v) cout << vy.val() << "\n";
#else
#define show(x)
#define showp(p)
#define show2(x,y)
#define show3(x,y,z)
#define show4(x,y,z,x2)
#define test(x)
#define showv(v)
#define showv2(v)
#define showvm(v)
#define showvm2(v)
#endif

template <typename mint>
struct FormalPowerSeries : vector<mint> {
  using vector<mint>::vector;
  using FPS = FormalPowerSeries;

  FPS &operator+=(const FPS &r) {
    if (r.size() > this->size()) this->resize(r.size());
    for (int i = 0; i < (int)r.size(); i++) (*this)[i] += r[i];
    return *this;
  }

  FPS &operator+=(const mint &r) {
    if (this->empty()) this->resize(1);
    (*this)[0] += r;
    return *this;
  }

  FPS &operator-=(const FPS &r) {
    if (r.size() > this->size()) this->resize(r.size());
    for (int i = 0; i < (int)r.size(); i++) (*this)[i] -= r[i];
    return *this;
  }

  FPS &operator-=(const mint &r) {
    if (this->empty()) this->resize(1);
    (*this)[0] -= r;
    return *this;
  }

  FPS &operator*=(const mint &v) {
    for (int k = 0; k < (int)this->size(); k++) (*this)[k] *= v;
    return *this;
  }

  FPS &operator/=(const FPS &r) {
    if (this->size() < r.size()) {
      this->clear();
      return *this;
    }
    int n = this->size() - r.size() + 1;
    if ((int)r.size() <= 64) {
      FPS f(*this), g(r);
      g.shrink();
      mint coeff = g.back().inverse();
      for (auto &x : g) x *= coeff;
      int deg = (int)f.size() - (int)g.size() + 1;
      int gs = g.size();
      FPS quo(deg);
      for (int i = deg - 1; i >= 0; i--) {
        quo[i] = f[i + gs - 1];
        for (int j = 0; j < gs; j++) f[i + j] -= quo[i] * g[j];
      }
      *this = quo * coeff;
      this->resize(n, mint(0));
      return *this;
    }
    return *this = ((*this).rev().pre(n) * r.rev().inv(n)).pre(n).rev();
  }

  FPS &operator%=(const FPS &r) {
    *this -= *this / r * r;
    shrink();
    return *this;
  }

  FPS operator+(const FPS &r) const { return FPS(*this) += r; }
  FPS operator+(const mint &v) const { return FPS(*this) += v; }
  FPS operator-(const FPS &r) const { return FPS(*this) -= r; }
  FPS operator-(const mint &v) const { return FPS(*this) -= v; }
  FPS operator*(const FPS &r) const { return FPS(*this) *= r; }
  FPS operator*(const mint &v) const { return FPS(*this) *= v; }
  FPS operator/(const FPS &r) const { return FPS(*this) /= r; }
  FPS operator%(const FPS &r) const { return FPS(*this) %= r; }
  FPS operator-() const {
    FPS ret(this->size());
    for (int i = 0; i < (int)this->size(); i++) ret[i] = -(*this)[i];
    return ret;
  }

  void shrink() {
    while (this->size() && this->back() == mint(0)) this->pop_back();
  }

  FPS rev() const {
    FPS ret(*this);
    reverse(begin(ret), end(ret));
    return ret;
  }

  FPS dot(FPS r) const {
    FPS ret(min(this->size(), r.size()));
    for (int i = 0; i < (int)ret.size(); i++) ret[i] = (*this)[i] * r[i];
    return ret;
  }

  FPS pre(int sz) const {
    return FPS(begin(*this), begin(*this) + min((int)this->size(), sz));
  }

  FPS operator>>(int sz) const {
    if ((int)this->size() <= sz) return {};
    FPS ret(*this);
    ret.erase(ret.begin(), ret.begin() + sz);
    return ret;
  }

  FPS operator<<(int sz) const {
    FPS ret(*this);
    ret.insert(ret.begin(), sz, mint(0));
    return ret;
  }

  FPS diff() const {
    const int n = (int)this->size();
    FPS ret(max(0, n - 1));
    mint one(1), coeff(1);
    for (int i = 1; i < n; i++) {
      ret[i - 1] = (*this)[i] * coeff;
      coeff += one;
    }
    return ret;
  }

  FPS integral() const {
    const int n = (int)this->size();
    FPS ret(n + 1);
    ret[0] = mint(0);
    if (n > 0) ret[1] = mint(1);
    auto mod = mint::get_mod();
    for (int i = 2; i <= n; i++) ret[i] = (-ret[mod % i]) * (mod / i);
    for (int i = 0; i < n; i++) ret[i + 1] *= (*this)[i];
    return ret;
  }

  mint eval(mint x) const {
    mint r = 0, w = 1;
    for (auto &v : *this) r += w * v, w *= x;
    return r;
  }

  FPS log(int deg = -1) const {
    assert((*this)[0] == mint(1));
    if (deg == -1) deg = (int)this->size();
    return (this->diff() * this->inv(deg)).pre(deg - 1).integral();
  }

  FPS pow(int64_t k, int deg = -1) const {
    const int n = (int)this->size();
    if (deg == -1) deg = n;
    if (k == 0) {
      FPS ret(deg);
      if (deg) ret[0] = 1;
      return ret;
    }
    for (int i = 0; i < n; i++) {
      if ((*this)[i] != mint(0)) {
        mint rev = mint(1) / (*this)[i];
        FPS ret = (((*this * rev) >> i).log(deg) * k).exp(deg);
        ret *= (*this)[i].pow(k);
        ret = (ret << (i * k)).pre(deg);
        if ((int)ret.size() < deg) ret.resize(deg, mint(0));
        return ret;
      }
      if (__int128_t(i + 1) * k >= deg) return FPS(deg, mint(0));
    }
    return FPS(deg, mint(0));
  }

  static void *ntt_ptr;
  static void set_fft();
  FPS &operator*=(const FPS &r);
  void ntt();
  void intt();
  void ntt_doubling();
  static int ntt_pr();
  FPS inv(int deg = -1) const;
  FPS exp(int deg = -1) const;
};
template <typename mint>
void *FormalPowerSeries<mint>::ntt_ptr = nullptr;

template <typename mint>
struct ProductTree {
  using fps = FormalPowerSeries<mint>;
  const vector<mint> &xs;
  vector<fps> buf;
  int N, xsz;
  vector<int> l, r;
  ProductTree(const vector<mint> &xs_) : xs(xs_), xsz(xs.size()) {
    N = 1;
    while (N < (int)xs.size()) N *= 2;
    buf.resize(2 * N);
    l.resize(2 * N, xs.size());
    r.resize(2 * N, xs.size());
    fps::set_fft();
    if (fps::ntt_ptr == nullptr)
      build();
    else
      build_ntt();
  }

  void build() {
    for (int i = 0; i < xsz; i++) {
      l[i + N] = i;
      r[i + N] = i + 1;
      buf[i + N] = {-xs[i], 1};
    }
    for (int i = N - 1; i > 0; i--) {
      l[i] = l[(i << 1) | 0];
      r[i] = r[(i << 1) | 1];
      if (buf[(i << 1) | 0].empty())
        continue;
      else if (buf[(i << 1) | 1].empty())
        buf[i] = buf[(i << 1) | 0];
      else
        buf[i] = buf[(i << 1) | 0] * buf[(i << 1) | 1];
    }
  }

  void build_ntt() {
    fps f;
    f.reserve(N * 2);
    for (int i = 0; i < xsz; i++) {
      l[i + N] = i;
      r[i + N] = i + 1;
      buf[i + N] = {-xs[i] + 1, -xs[i] - 1};
    }
    for (int i = N - 1; i > 0; i--) {
      l[i] = l[(i << 1) | 0];
      r[i] = r[(i << 1) | 1];
      if (buf[(i << 1) | 0].empty())
        continue;
      else if (buf[(i << 1) | 1].empty())
        buf[i] = buf[(i << 1) | 0];
      else if (buf[(i << 1) | 0].size() == buf[(i << 1) | 1].size()) {
        buf[i] = buf[(i << 1) | 0];
        f.clear();
        copy(begin(buf[(i << 1) | 1]), end(buf[(i << 1) | 1]),
             back_inserter(f));
        buf[i].ntt_doubling();
        f.ntt_doubling();
        for (int j = 0; j < (int)buf[i].size(); j++) buf[i][j] *= f[j];
      } else {
        buf[i] = buf[(i << 1) | 0];
        f.clear();
        copy(begin(buf[(i << 1) | 1]), end(buf[(i << 1) | 1]),
             back_inserter(f));
        buf[i].ntt_doubling();
        f.intt();
        f.resize(buf[i].size(), mint(0));
        f.ntt();
        for (int j = 0; j < (int)buf[i].size(); j++) buf[i][j] *= f[j];
      }
    }
    for (int i = 0; i < 2 * N; i++) {
      buf[i].intt();
      buf[i].shrink();
    }
  }
};

template <typename mint>
vector<mint> InnerMultipointEvaluation(const FormalPowerSeries<mint> &f,
                                       const vector<mint> &xs,
                                       const ProductTree<mint> &ptree) {
  using fps = FormalPowerSeries<mint>;
  vector<mint> ret;
  ret.reserve(xs.size());
  auto rec = [&](auto self, fps a, int idx) {
    if (ptree.l[idx] == ptree.r[idx]) return;
    a %= ptree.buf[idx];
    if ((int)a.size() <= 64) {
      for (int i = ptree.l[idx]; i < ptree.r[idx]; i++)
        ret.push_back(a.eval(xs[i]));
      return;
    }
    self(self, a, (idx << 1) | 0);
    self(self, a, (idx << 1) | 1);
  };
  rec(rec, f, 1);
  return ret;
}

template <typename mint>
vector<mint> MultipointEvaluation(const FormalPowerSeries<mint> &f,
                                  const vector<mint> &xs) {
  if(f.empty() || xs.empty()) return vector<mint>(xs.size(), mint(0));
  return InnerMultipointEvaluation(f, xs, ProductTree<mint>(xs));
}

template <class T>
struct Matrix {
  vector<vector<T> > A;

  Matrix() = default;
  Matrix(int n, int m) : A(n, vector<T>(m, T())) {}
  Matrix(int n) : A(n, vector<T>(n, T())){};

  int H() const { return A.size(); }

  int W() const { return A[0].size(); }

  int size() const { return A.size(); }

  inline const vector<T> &operator[](int k) const { return A[k]; }

  inline vector<T> &operator[](int k) { return A[k]; }

  static Matrix I(int n) {
    Matrix mat(n);
    for (int i = 0; i < n; i++) mat[i][i] = 1;
    return (mat);
  }

  Matrix &operator+=(const Matrix &B) {
    int n = H(), m = W();
    assert(n == B.H() && m == B.W());
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) (*this)[i][j] += B[i][j];
    return (*this);
  }

  Matrix &operator-=(const Matrix &B) {
    int n = H(), m = W();
    assert(n == B.H() && m == B.W());
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) (*this)[i][j] -= B[i][j];
    return (*this);
  }

  Matrix &operator*=(const Matrix &B) {
    int n = H(), m = B.W(), p = W();
    assert(p == B.H());
    vector<vector<T> > C(n, vector<T>(m, T{}));
    for (int i = 0; i < n; i++)
      for (int k = 0; k < p; k++)
        for (int j = 0; j < m; j++) C[i][j] += (*this)[i][k] * B[k][j];
    A.swap(C);
    return (*this);
  }

  Matrix &operator^=(long long k) {
    Matrix B = Matrix::I(H());
    while (k > 0) {
      if (k & 1) B *= *this;
      *this *= *this;
      k >>= 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); }

  Matrix operator^(const long long k) const { return (Matrix(*this) ^= k); }

  bool operator==(const Matrix &B) const {
    assert(H() == B.H() && 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() && 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;
  }

  friend ostream &operator<<(ostream &os, const Matrix &p) {
    int n = p.H(), m = p.W();
    for (int i = 0; i < n; i++) {
      os << (i ? "   " : "") << "[";
      for (int j = 0; j < m; j++) {
        os << p[i][j] << (j + 1 == m ? "]\n" : ",");
      }
    }
    return (os);
  }

  T determinant() const {
    Matrix B(*this);
    assert(H() == W());
    T ret = 1;
    for (int i = 0; i < H(); i++) {
      int idx = -1;
      for (int j = i; j < W(); j++) {
        if (B[j][i] != 0) {
          idx = j;
          break;
        }
      }
      if (idx == -1) return 0;
      if (i != idx) {
        ret *= T(-1);
        swap(B[i], B[idx]);
      }
      ret *= B[i][i];
      T inv = T(1) / B[i][i];
      for (int j = 0; j < W(); j++) {
        B[i][j] *= inv;
      }
      for (int j = i + 1; j < H(); j++) {
        T a = B[j][i];
        if (a == 0) continue;
        for (int k = i; k < W(); k++) {
          B[j][k] -= B[i][k] * a;
        }
      }
    }
    return ret;
  }
};

template <class mint>
FormalPowerSeries<mint> PolynomialInterpolation(const vector<mint> &xs,
                                                const vector<mint> &ys) {
  using fps = FormalPowerSeries<mint>;
  assert(xs.size() == ys.size());
  ProductTree<mint> ptree(xs);
  fps w = ptree.buf[1].diff();
  vector<mint> vs = InnerMultipointEvaluation<mint>(w, xs, ptree);
  auto rec = [&](auto self, int idx) -> fps {
    if (idx >= ptree.N) {
      if (idx - ptree.N < (int)xs.size())
        return {ys[idx - ptree.N] / vs[idx - ptree.N]};
      else
        return {mint(1)};
    }
    if (ptree.buf[idx << 1 | 0].empty())
      return {};
    else if (ptree.buf[idx << 1 | 1].empty())
      return self(self, idx << 1 | 0);
    return self(self, idx << 1 | 0) * ptree.buf[idx << 1 | 1] +
           self(self, idx << 1 | 1) * ptree.buf[idx << 1 | 0];
  };
  return rec(rec, 1);
}

template <typename mint>
FormalPowerSeries<mint> PolynomialMatrixDeterminant(
    const Matrix<FormalPowerSeries<mint>> &m) {
  int N = m.size();
  int deg = 0;
  for (int i = 0; i < N; ++i) deg += max<int>(1, m[i][i].size()) - 1;
  vector<mint> xs(deg + 1);
  vector<mint> ys(deg + 1);
  Matrix<mint> M(N);
  for (int x = 0; x <= deg; x++) {
    xs[x] = x;
    for (int i = 0; i < N; ++i)
      for (int j = 0; j < N; ++j) M[i][j] = m[i][j].eval(x);
    ys[x] = M.determinant();
  }
  return PolynomialInterpolation<mint>(xs, ys);
}

template <typename T>
struct MatrixTree {
  int n;
  Matrix<T> m;
  MatrixTree(int _n) : n(_n), m(_n) { assert(n > 0); }

  void add(int i, int j, const T& x) {
    if (i < n) m[i][i] += x;
    if (j < n) m[j][j] += x;
    if (i < n and j < n) {
      m[i][j] -= x;
      m[j][i] -= x;
    }
  }

  Matrix<T> get() const { return m; }

  template <typename U, typename = void>
  struct has_value_type : false_type {};
  template <typename U>
  struct has_value_type<
      U, typename conditional<false, typename U::value_type, void>::type>
      : true_type {};

  template <typename U = T,
            enable_if_t<has_value_type<U>::value, nullptr_t> = nullptr>
  T calc() {
    return PolynomialMatrixDeterminant(m);
  }
  template <typename U = T,
            enable_if_t<!has_value_type<U>::value, nullptr_t> = nullptr>
  T calc() {
    return m.determinant();
  }
};

void solve(){
    int n,k; cin>>n>>k;
    vector<vpii> e(k);
    rep(i,k){
        int t; cin>>t;
        rep(j,t){
            int a,b; cin>>a>>b;
            a--; b--;
            e[i].pb({a,b});
        }
    }

    mint ans = 0;
    int l = 1 << k;
    rep1(i,l){
        int p = pcnt(i);
        Matrix<mint> v(n-1);
        rep(j,k) if ((i>>j&1) == 1){
            fore(y , e[j]){
                if (y.fi && y.se) v[y.fi-1][y.se-1]--;
                if (y.fi && y.se) v[y.se-1][y.fi-1]--;
                if (y.fi) v[y.fi-1][y.fi-1]++;
                if (y.se) v[y.se-1][y.se-1]++;
            }
        }
        mint tmp = v.determinant();
        
        if ((k-p)%2==1) tmp *= -1;
        ans += tmp;
    }
    outm;

    return;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    //cin>>t;

    rep(i,t){
        solve();
    }

    return 0;
}
0