Problem p02539 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <complex>
#include <cstring>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

using namespace std;
// using namespace atcoder;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define RFOR(i, a, n) for (int i = (int)(n)-1; i >= (a); --i)

#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()

#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl;

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[";
  REP(i, SZ(v)) {
    if (i)
      os << ", ";
    os << v[i];
  }
  return os << "]";
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << "(" << p.first << " " << p.second << ")";
}

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;

const ll MOD = 1e9 + 7;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
const ld eps = 1e-9;

// edit

template <ll mod = 998244353, ll primitive = 3> struct NTT {

  /**
   * @brief 畳み込みの実行. O(nlog n)
   * @require n <= 2^23
   * @param[in] a, b: 畳み込みたい配列
   * @return 畳み込みした結果
   * @usage
   *   vector<ll> a(2*n+1), b(2*n+1);
   *   vector<ll> c = NTT<>::multiply(a, b);
   */
  static vector<ll> multiply(const vector<ll> &a, const vector<ll> &b) {
    auto A = NTT(a).solve();
    auto B = NTT(b).solve();

    vector<ll> C(A.size());
    REP(i, C.size()) C[i] = (A[i] * B[i]) % 998244353;

    return NTT(C).solve(true);
  }

private:
  int n;
  vector<ll> a;
  ll inv_n, root, inv_root;

  NTT(const vector<ll> &a) : n(1), a(a) {
    while (n < a.size())
      n <<= 1;
    this->a.resize(n);
    inv_n = modpow(n, mod - 2);
    root = modpow(primitive, (mod - 1) / n);
    inv_root = modpow(root, mod - 2);
  }

  vector<ll> solve(bool inverse = false) {
    return ntt(0, 0, inverse, inverse ? inv_root : root);
  }

  vector<ll> ntt(int d, int bit, bool inverse, ll base) {
    int sz = n >> d;
    if (sz == 1)
      return {(a[bit] * (inverse ? inv_n : 1)) % mod};

    auto f0 = ntt(d + 1, bit, inverse, (base * base) % mod);
    auto f1 = ntt(d + 1, bit | 1 << d, inverse, (base * base) % mod);
    vector<ll> f(sz);
    ll tmp = 1;
    for (int i = 0; i < sz; ++i) {
      f[i] = (f0[i % (sz / 2)] + tmp * f1[i % (sz / 2)]) % mod;
      (tmp *= base) %= mod;
    }
    return f;
  }

  ll modpow(ll a, ll p) {
    ll ret = 1;
    while (p) {
      if (p & 1)
        (ret *= a) %= mod;
      (a *= a) %= mod;
      p >>= 1;
    }
    return ret;
  }
};

template <int64_t mod> class modint {
public:
  int64_t x;

  modint(int64_t x = 0) : x(x < 0 ? ((x % mod) + mod) % mod : x % mod) {}

  const modint operator-() const { return x ? mod - x : 0; }

  modint &operator+=(const modint &rhs) {
    if ((x += rhs.x) >= mod)
      x -= mod;
    return *this;
  }

  modint &operator-=(const modint &rhs) { return *this += -rhs; }

  modint &operator*=(const modint &rhs) {
    (x *= rhs.x) %= mod;
    return *this;
  }

  modint &operator/=(const modint &rhs) { return *this *= rhs.pow(mod - 2); }

  friend const modint operator+(modint lhs, const modint &rhs) {
    return lhs += rhs;
  }

  friend const modint operator-(modint lhs, const modint &rhs) {
    return lhs -= rhs;
  }

  friend const modint operator*(modint lhs, const modint &rhs) {
    return lhs *= rhs;
  }

  friend const modint operator/(modint lhs, const modint &rhs) {
    return lhs /= rhs;
  }

  const modint pow(int64_t n) const {
    modint ret = 1, tmp = *this;
    while (n) {
      if (n & 1)
        ret *= tmp;
      tmp *= tmp;
      n >>= 1;
    }
    return ret;
  }

  friend bool operator==(const modint &lhs, const modint &rhs) {
    return lhs.x == rhs.x;
  }

  friend bool operator!=(const modint &lhs, const modint &rhs) {
    return !(lhs == rhs);
  }

  friend ostream &operator<<(ostream &os, const modint &a) { return os << a.x; }

  friend istream &operator>>(istream &is, modint &a) {
    int64_t tmp;
    is >> tmp;
    a = tmp;
    return is;
  }
};

using mint = modint<998244353>;
vector<mint> fact_v{1};
vector<mint> inv_p2_v{mint(1), mint(1) / mint(2)};
vector<mint> inv_fact_v;

mint fact(int x) {
  for (int i = SZ(fact_v); i <= x; ++i) {
    fact_v.push_back(fact_v.at(i - 1) * i);
  }
  return fact_v.at(x);
}

mint inv_p2(int p) {
  for (int i = SZ(inv_p2_v); i <= p; ++i) {
    //        fact_v.push_back(fact_v.at(i - 1) * i);
    inv_p2_v.push_back(inv_p2_v.back() * inv_p2_v.at(1));
  }
  return inv_p2_v.at(p);
}

mint inv_fact(int x) {
  for (int i = SZ(inv_fact_v); i <= x; ++i) {
    inv_fact_v.push_back(mint(1) / fact(i));
  }
  return inv_fact_v[x];
}

void solve() {
  int N;
  cin >> N;
  int lim = 100000;

  vector<int> cnt(lim + 1);
  REP(i, 2 * N) {
    int tmp;
    cin >> tmp;
    cnt[tmp]++;
  }

  vector<vector<ll>> g; // g[i][j] = 身長A[i]がjペア違反する方法

  for (int i = 0; i <= lim; ++i) {
    if (cnt[i] == 0)
      continue;
    vector<ll> v;
    for (int j = 0; j * 2 <= cnt[i]; ++j) {
      //            mint tmp = fact(cnt[i]) / (mint(2).pow(j) * fact(j) *
      //            fact(cnt[i] - 2 * j));
      mint tmp =
          fact(cnt[i]) * inv_p2(j) * inv_fact(j) * inv_fact(cnt[i] - 2 * j);
      v.push_back(tmp.x);
    }
    g.push_back(v);
  }

  priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
  for (int i = 0; i < g.size(); ++i) {
    pq.push(make_pair((int)g[i].size(), i));
  }

  while (pq.size() > 1) {
    int i1 = pq.top().second;
    pq.pop();
    int i2 = pq.top().second;
    pq.pop();

    int s1 = g[i1].size();
    int s2 = g[i2].size();
    //        int sn = s1 + s2 + 1;
    int sn = s1 + s2 + 1;
    //        cerr << s1 << " " << s2 << " " << sn << endl;

    vector<ll> v1(sn), v2(sn);
    for (int i = 0; i < s1; ++i) {
      v1[i] = g[i1][i];
    }

    for (int i = 0; i < s2; ++i) {
      v2[i] = g[i2][i];
    }

    vector<ll> vn = NTT<>::multiply(v1, v2);
    vn.resize(sn);
    g[i1] = vn;
    pq.emplace(sn, i1);
  }

  auto f = g[pq.top().second];

  mint ans = 0;
  for (int i = 0; i <= N; ++i) {
    if (f.size() <= i) {
      break;
    }

    int sign = 1 - 2 * (i % 2);
    //        mint tmp = mint(f[i]) * fact(2 * N - 2 * i) / (fact(N - i) *
    //        mint(2).pow(N - i));
    mint tmp =
        mint(f[i]) * fact(2 * N - 2 * i) * inv_fact(N - i) * inv_p2(N - i);
    ans += tmp * sign;
  }

  cout << ans << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  //    std::ifstream in("input.txt");
  //    std::cin.rdbuf(in.rdbuf());

  solve();

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <complex>
#include <cstring>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

using namespace std;
// using namespace atcoder;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define RFOR(i, a, n) for (int i = (int)(n)-1; i >= (a); --i)

#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()

#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl;

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[";
  REP(i, SZ(v)) {
    if (i)
      os << ", ";
    os << v[i];
  }
  return os << "]";
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << "(" << p.first << " " << p.second << ")";
}

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;

const ll MOD = 1e9 + 7;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
const ld eps = 1e-9;

// edit

template <ll mod = 998244353, ll primitive = 3> struct NTT {

  /**
   * @brief 畳み込みの実行. O(nlog n)
   * @require n <= 2^23
   * @param[in] a, b: 畳み込みたい配列
   * @return 畳み込みした結果
   * @usage
   *   vector<ll> a(2*n+1), b(2*n+1);
   *   vector<ll> c = NTT<>::multiply(a, b);
   */
  static vector<ll> multiply(const vector<ll> &a, const vector<ll> &b) {
    auto A = NTT(a).solve();
    auto B = NTT(b).solve();

    vector<ll> C(A.size());
    REP(i, C.size()) C[i] = (A[i] * B[i]) % 998244353;

    return NTT(C).solve(true);
  }

private:
  int n;
  vector<ll> a;
  ll inv_n, root, inv_root;

  NTT(const vector<ll> &a) : n(1), a(a) {
    while (n < a.size())
      n <<= 1;
    this->a.resize(n);
    inv_n = modpow(n, mod - 2);
    root = modpow(primitive, (mod - 1) / n);
    inv_root = modpow(root, mod - 2);
  }

  vector<ll> solve(bool inverse = false) {
    return ntt(0, 0, inverse, inverse ? inv_root : root);
  }

  vector<ll> ntt(int d, int bit, bool inverse, ll base) {
    int sz = n >> d;
    if (sz == 1)
      return {(a[bit] * (inverse ? inv_n : 1)) % mod};

    auto f0 = ntt(d + 1, bit, inverse, (base * base) % mod);
    auto f1 = ntt(d + 1, bit | 1 << d, inverse, (base * base) % mod);
    vector<ll> f(sz);
    ll tmp = 1;
    for (int i = 0; i < sz; ++i) {
      f[i] = (f0[i % (sz / 2)] + tmp * f1[i % (sz / 2)]) % mod;
      (tmp *= base) %= mod;
    }
    return f;
  }

  ll modpow(ll a, ll p) {
    ll ret = 1;
    while (p) {
      if (p & 1)
        (ret *= a) %= mod;
      (a *= a) %= mod;
      p >>= 1;
    }
    return ret;
  }
};

template <int64_t mod> class modint {
public:
  int64_t x;

  modint(int64_t x = 0) : x(x < 0 ? ((x % mod) + mod) % mod : x % mod) {}

  const modint operator-() const { return x ? mod - x : 0; }

  modint &operator+=(const modint &rhs) {
    if ((x += rhs.x) >= mod)
      x -= mod;
    return *this;
  }

  modint &operator-=(const modint &rhs) { return *this += -rhs; }

  modint &operator*=(const modint &rhs) {
    (x *= rhs.x) %= mod;
    return *this;
  }

  modint &operator/=(const modint &rhs) { return *this *= rhs.pow(mod - 2); }

  friend const modint operator+(modint lhs, const modint &rhs) {
    return lhs += rhs;
  }

  friend const modint operator-(modint lhs, const modint &rhs) {
    return lhs -= rhs;
  }

  friend const modint operator*(modint lhs, const modint &rhs) {
    return lhs *= rhs;
  }

  friend const modint operator/(modint lhs, const modint &rhs) {
    return lhs /= rhs;
  }

  const modint pow(int64_t n) const {
    modint ret = 1, tmp = *this;
    while (n) {
      if (n & 1)
        ret *= tmp;
      tmp *= tmp;
      n >>= 1;
    }
    return ret;
  }

  friend bool operator==(const modint &lhs, const modint &rhs) {
    return lhs.x == rhs.x;
  }

  friend bool operator!=(const modint &lhs, const modint &rhs) {
    return !(lhs == rhs);
  }

  friend ostream &operator<<(ostream &os, const modint &a) { return os << a.x; }

  friend istream &operator>>(istream &is, modint &a) {
    int64_t tmp;
    is >> tmp;
    a = tmp;
    return is;
  }
};

using mint = modint<998244353>;
vector<mint> fact_v{1};
vector<mint> inv_p2_v{mint(1), mint(1) / mint(2)};
vector<mint> inv_fact_v;

mint fact(int x) {
  for (int i = SZ(fact_v); i <= x; ++i) {
    fact_v.push_back(fact_v.at(i - 1) * i);
  }
  return fact_v.at(x);
}

mint inv_p2(int p) {
  for (int i = SZ(inv_p2_v); i <= p; ++i) {
    //        fact_v.push_back(fact_v.at(i - 1) * i);
    inv_p2_v.push_back(inv_p2_v.back() * inv_p2_v.at(1));
  }
  return inv_p2_v.at(p);
}

mint inv_fact(int x) {
  for (int i = SZ(inv_fact_v); i <= x; ++i) {
    inv_fact_v.push_back(mint(1) / fact(i));
  }
  return inv_fact_v[x];
}

void solve() {
  int N;
  cin >> N;
  int lim = 100000;

  vector<int> cnt(lim + 1);
  REP(i, 2 * N) {
    int tmp;
    cin >> tmp;
    cnt[tmp]++;
  }

  vector<vector<ll>> g; // g[i][j] = 身長A[i]がjペア違反する方法

  for (int i = 0; i <= lim; ++i) {
    if (cnt[i] == 0)
      continue;
    vector<ll> v;
    for (int j = 0; j * 2 <= cnt[i]; ++j) {
      //            mint tmp = fact(cnt[i]) / (mint(2).pow(j) * fact(j) *
      //            fact(cnt[i] - 2 * j));
      mint tmp =
          fact(cnt[i]) * inv_p2(j) * inv_fact(j) * inv_fact(cnt[i] - 2 * j);
      v.push_back(tmp.x);
    }
    g.push_back(v);
  }

  priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
  for (int i = 0; i < g.size(); ++i) {
    pq.push(make_pair((int)g[i].size(), i));
  }

  while (pq.size() > 1) {
    int i1 = pq.top().second;
    pq.pop();
    int i2 = pq.top().second;
    pq.pop();

    int s1 = g[i1].size();
    int s2 = g[i2].size();
    //        int sn = s1 + s2 + 1;
    int sn = s1 + s2 + 1;

    vector<ll> v1(sn), v2(sn);
    for (int i = 0; i < s1; ++i) {
      v1[i] = g[i1][i];
    }

    for (int i = 0; i < s2; ++i) {
      v2[i] = g[i2][i];
    }

    vector<ll> vn = NTT<>::multiply(v1, v2);
    g[i1] = vn;
    pq.emplace(sn, i1);
  }

  auto f = g[pq.top().second];

  mint ans = 0;
  for (int i = 0; i <= N; ++i) {
    if (f.size() <= i) {
      break;
    }

    int sign = 1 - 2 * (i % 2);
    //        mint tmp = mint(f[i]) * fact(2 * N - 2 * i) / (fact(N - i) *
    //        mint(2).pow(N - i));
    mint tmp =
        mint(f[i]) * fact(2 * N - 2 * i) * inv_fact(N - i) * inv_p2(N - i);
    ans += tmp * sign;
  }

  cout << ans << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  //    std::ifstream in("input.txt");
  //    std::cin.rdbuf(in.rdbuf());

  solve();

  return 0;
}