結果

問題 No.2507 Yet Another Subgraph Counting
ユーザー 👑 hos.lyrichos.lyric
提出日時 2023-10-27 16:33:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 11,280 bytes
コンパイル時間 1,495 ms
コンパイル使用メモリ 121,924 KB
実行使用メモリ 5,016 KB
最終ジャッジ日時 2023-10-27 16:33:37
合計ジャッジ時間 4,834 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 5 ms
4,368 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 5 ms
4,368 KB
testcase_12 AC 3 ms
4,348 KB
testcase_13 AC 3 ms
4,348 KB
testcase_14 AC 5 ms
4,368 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 5 ms
4,368 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 3 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 3 ms
4,348 KB
testcase_23 AC 5 ms
4,368 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 9 ms
4,452 KB
testcase_26 WA -
testcase_27 AC 5 ms
4,368 KB
testcase_28 AC 9 ms
4,452 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 WA -
testcase_31 AC 47 ms
5,012 KB
testcase_32 AC 47 ms
5,012 KB
testcase_33 AC 5 ms
4,368 KB
testcase_34 AC 3 ms
4,348 KB
testcase_35 AC 5 ms
4,368 KB
testcase_36 AC 3 ms
4,348 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 49 ms
5,016 KB
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 AC 20 ms
4,636 KB
testcase_44 AC 47 ms
5,012 KB
testcase_45 AC 5 ms
4,368 KB
testcase_46 AC 4 ms
4,368 KB
testcase_47 AC 48 ms
5,012 KB
testcase_48 AC 2 ms
4,348 KB
testcase_49 WA -
testcase_50 AC 3 ms
4,348 KB
testcase_51 AC 20 ms
4,636 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")

////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
  static constexpr unsigned M = M_;
  unsigned x;
  constexpr ModInt() : x(0U) {}
  constexpr ModInt(unsigned x_) : x(x_ % M) {}
  constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
  constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
  constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
  ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
  ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
  ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
  ModInt pow(long long e) const {
    if (e < 0) return inv().pow(-e);
    ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
  }
  ModInt inv() const {
    unsigned a = M, b = x; int y = 0, z = 1;
    for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
    assert(a == 1U); return ModInt(y);
  }
  ModInt operator+() const { return *this; }
  ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
  ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
  ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
  ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
  ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
  template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
  template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
  template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
  template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
  explicit operator bool() const { return x; }
  bool operator==(const ModInt &a) const { return (x == a.x); }
  bool operator!=(const ModInt &a) const { return (x != a.x); }
  friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////

constexpr unsigned MO = 998244353;
using Mint = ModInt<MO>;


// as * bs
// ZT: T[2^n][n+1]
template <class T, class ZT>
vector<T> setMul(int n, const vector<T> &as, const vector<T> &bs, ZT zas, ZT zbs) {
  assert(static_cast<int>(as.size()) == 1 << n);
  assert(static_cast<int>(bs.size()) == 1 << n);
  // ranked
  for (int h = 0; h < 1 << n; ++h) {
    memset(zas[h], 0, (n + 1) * sizeof(T));
    zas[h][__builtin_popcount(h)] = as[h];
  }
  for (int h = 0; h < 1 << n; ++h) {
    memset(zbs[h], 0, (n + 1) * sizeof(T));
    zbs[h][__builtin_popcount(h)] = bs[h];
  }
  // zeta
  for (int w = 1; w < 1 << n; w <<= 1) {
    for (int h0 = 0; h0 < 1 << n; h0 += w << 1) for (int h = h0; h < h0 + w; ++h) {
      for (int k = 0; k <= n; ++k) zas[h + w][k] += zas[h][k];
    }
  }
  for (int w = 1; w < 1 << n; w <<= 1) {
    for (int h0 = 0; h0 < 1 << n; h0 += w << 1) for (int h = h0; h < h0 + w; ++h) {
      for (int k = 0; k <= n; ++k) zbs[h + w][k] += zbs[h][k];
    }
  }
  // product
  for (int h = 0; h < 1 << n; ++h) {
    for (int k = n; k >= 0; --k) {
      T t = 0;
      for (int l = 0; l <= k; ++l) t += zas[h][l] * zbs[h][k - l];
      zas[h][k] = t;
    }
  }
  // moebius
  for (int w = 1; w < 1 << n; w <<= 1) {
    for (int h0 = 0; h0 < 1 << n; h0 += w << 1) for (int h = h0; h < h0 + w; ++h) {
      for (int k = 0; k <= n; ++k) zas[h + w][k] -= zas[h][k];
    }
  }
  // unrank
  vector<T> cs(1 << n);
  for (int h = 0; h < 1 << n; ++h) cs[h] = zas[h][__builtin_popcount(h)];
  return cs;
}

// exp(as)
//   assume as[0] == 0
//   exp(a0 + a1 X) = exp(a0) + exp(a0) a1 X
// ZT1: T[2^(n-1)][n]
// ZT: T[2^n][n+1]
template <class T, class ZT1, class ZT>
vector<T> setExp(int n, const vector<T> &as, ZT1 zas, ZT zbs) {
  assert(static_cast<int>(as.size()) == 1 << n);
  assert(as[0] == 0);
  zbs[0][0] = 1;
  for (int m = 0; m < n; ++m) {
    // ranked a[2^m, 2^(m+1))
    for (int h = 0; h < 1 << m; ++h) {
      memset(zas[h], 0, (m + 1) * sizeof(T));
      zas[h][__builtin_popcount(h)] = as[(1 << m) + h];
    }
    // zeta
    for (int w = 1; w < 1 << m; w <<= 1) {
      for (int h0 = 0; h0 < 1 << m; h0 += w << 1) for (int h = h0; h < h0 + w; ++h) {
        for (int k = 0; k <= m; ++k) zas[h + w][k] += zas[h][k];
      }
    }
    for (int h = 0; h < 1 << m; ++h) {
      // zeta
      zbs[h][m + 1] = 0;
      memcpy(zbs[(1 << m) + h], zbs[h], ((m + 1) + 1) * sizeof(T));
      // product
      for (int k = 0; k <= m; ++k) for (int l = 0; l <= m - k; ++l) {
        zbs[(1 << m) + h][k + l + 1] += zbs[h][k] * zas[h][l];
      }
    }
  }
  // moebius
  for (int w = 1; w < 1 << n; w <<= 1) {
    for (int h0 = 0; h0 < 1 << n; h0 += w << 1) for (int h = h0; h < h0 + w; ++h) {
      for (int k = 0; k <= n; ++k) zbs[h + w][k] -= zbs[h][k];
    }
  }
  // unrank
  vector<T> bs(1 << n);
  for (int h = 0; h < 1 << n; ++h) bs[h] = zbs[h][__builtin_popcount(h)];
  return bs;
}

// \sum[0<=i<=n] fs[i] as^i/i!
//   assume as[0] == 0
//   f(a0 + a1 X) + f(a0) + f'(a0) a1 X
// ZT1: T[2^(n-1)][n]
// ZT: T[2^(n+1)][n+1]
template <class T, class ZT1, class ZT>
vector<T> setCom(int n, const vector<T> &fs, const vector<T> &as, ZT1 zas, ZT zbs) {
  assert(static_cast<int>(fs.size()) == n + 1);
  assert(static_cast<int>(as.size()) == 1 << n);
  assert(as[0] == 0);
  // zbs[2^(n-i), 2^(n-i+1)): composite f^(i)
  for (int i = 0; i <= n; ++i) zbs[1<<(n-i)][0] = fs[i];
  for (int m = 0; m < n; ++m) {
    // ranked a[2^m, 2^(m+1))
    for (int h = 0; h < 1 << m; ++h) {
      memset(zas[h], 0, (m + 1) * sizeof(T));
      zas[h][__builtin_popcount(h)] = as[(1 << m) + h];
    }
    // zeta
    for (int w = 1; w < 1 << m; w <<= 1) {
      for (int h0 = 0; h0 < 1 << m; h0 += w << 1) for (int h = h0; h < h0 + w; ++h) {
        for (int k = 0; k <= m; ++k) zas[h + w][k] += zas[h][k];
      }
    }
    for (int i = 0; i < n - m; ++i) {
      for (int h = 0; h < 1 << m; ++h) {
        // zeta
        zbs[(1<<(n-i)) + h][m + 1] = 0;
        memcpy(zbs[(1<<(n-i)) + (1 << m) + h], zbs[(1<<(n-i)) + h], ((m + 1) + 1) * sizeof(T));
        // product
        for (int k = 0; k <= m; ++k) for (int l = 0; l <= m - k; ++l) {
          zbs[(1<<(n-i)) + (1 << m) + h][k + l + 1] += zbs[(1<<(n-(i+1))) + h][k] * zas[h][l];
        }
      }
    }
  }
  // moebius
  for (int w = 1; w < 1 << n; w <<= 1) {
    for (int h0 = 0; h0 < 1 << n; h0 += w << 1) for (int h = h0; h < h0 + w; ++h) {
      for (int k = 0; k <= n; ++k) zbs[(1<<n) + h + w][k] -= zbs[(1<<n) + h][k];
    }
  }
  // unrank
  vector<T> bs(1 << n);
  for (int h = 0; h < 1 << n; ++h) bs[h] = zbs[(1<<n) + h][__builtin_popcount(h)];
  return bs;
}

// \sum[i] fs[i] as^i
//   not necessarily as[0] == 0
// ZT1: T[2^(n-1)][n]
// ZT: T[2^(n+1)][n+1]
template <class T, class ZT1, class ZT>
vector<T> setComPoly(int n, vector<T> fs, vector<T> as, ZT1 zas, ZT zbs) {
  assert(static_cast<int>(as.size()) == 1 << n);
  const int fsLen = fs.size();
  if (fsLen == 0) return vector<T>(1 << n, 0);
  vector<T> gs(n + 1);
  for (int i = 0; i <= n; ++i) {
    T t = 0;
    for (int j = fsLen; --j >= 0; ) (t *= as[0]) += fs[j];
    gs[i] = t;
    for (int j = 1; j < fsLen; ++j) fs[j - 1] = j * fs[j];
    fs[fsLen - 1] = 0;
  }
  as[0] = 0;
  return setCom(n, gs, as, zas, zbs);
}

////////////////////////////////////////////////////////////////////////////////

constexpr int MAX_N = 13;
Mint zas[1 << (MAX_N + 1)][MAX_N + 1];
Mint zbs[1 << (MAX_N + 1)][MAX_N + 1];


int N, M;
vector<int> A, B;

Mint dp[1 << MAX_N][MAX_N];

int main() {
  for (; ~scanf("%d%d", &N, &M); ) {
    A.resize(M);
    B.resize(M);
    for (int i = 0; i < M; ++i) {
      scanf("%d%d", &A[i], &B[i]);
      --A[i];
      --B[i];
      if (A[i] > B[i]) {
        swap(A[i], B[i]);
      }
    }
    
    vector<int> adj(N, 0);
    for (int i = 0; i < M; ++i) {
      adj[A[i]] |= 1 << B[i];
      adj[B[i]] |= 1 << A[i];
    }
    
    // cycle
    vector<Mint> cs(1 << N, 0);
    memset(dp, 0, sizeof(dp));
    for (int r = 0; r < N; ++r) {
      dp[1 << r][r] = 1;
      for (int p = 1 << r; p < 1 << (r + 1); ++p) {
        for (int u = 0; u <= r; ++u) if (p & 1 << u) {
          for (int v = 0; v < r; ++v) if (!(p & 1 << v)) {
            if (adj[u] >> v & 1) {
              dp[p | 1 << v][v] += dp[p][u];
            }
          }
          if (adj[u] >> r & 1) {
            cs[p] += dp[p][u];
          }
        }
      }
    }
    const Mint INV2 = Mint(2).inv();
    for (int i = 0; i < M; ++i) cs[1 << A[i] | 1 << B[i]] -= 1;
    for (int p = 0; p < 1 << N; ++p) cs[p] *= INV2;
// cerr<<"cs = "<<cs<<endl;
    for (int u = 0; u < N; ++u) cs[1 << u] += 1;
    
    fill(adj.begin(), adj.end(), 0);
    for (int i = 0; i < M; ++i) {
      adj[B[i]] |= 1 << A[i];
    }
    for (int r = 1; r < N; ++r) {
      /*
        cs[p]:
          before: connected graph on p, allow bridge (u, v) s.t. u < v < r
          after : connected graph on p, allow bridge (u, v) s.t. u < v <= r
      */
      vector<Mint> ds(1 << (N - 1)), es(1 << (N - 1));
      for (int p = 0; p < 1 << r; ++p) for (int q = 0; q < 1 << (N-1-r); ++q) {
        ds[p | q << r] = cs[p | 1 << r | q << (r + 1)];
        es[p | q << r] = __builtin_popcount(p & adj[r]) * cs[p | q << (r + 1)];
      }
      es = setExp(N - 1, es, zas, zbs);
      ds = setMul(N - 1, ds, es, zas, zbs);
      for (int p = 0; p < 1 << r; ++p) for (int q = 0; q < 1 << (N-1-r); ++q) {
        cs[p | 1 << r | q << (r + 1)] = ds[p | q << r];
      }
// cerr<<"cs = "<<cs<<endl;
    }
    
    cs = setExp(N, cs, zas, zbs);
    printf("%u\n", cs.back().x);
  }
  return 0;
}
0