結果

問題 No.2904 Distinct Multisets in a Way
ユーザー 👑 binapbinap
提出日時 2024-09-27 21:24:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,014 ms / 2,000 ms
コード長 16,821 bytes
コンパイル時間 4,797 ms
コンパイル使用メモリ 288,256 KB
実行使用メモリ 33,248 KB
最終ジャッジ日時 2024-09-27 21:24:42
合計ジャッジ時間 18,403 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 956 ms
33,248 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 950 ms
32,992 KB
testcase_04 AC 3 ms
6,944 KB
testcase_05 AC 3 ms
6,944 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 3 ms
6,944 KB
testcase_09 AC 3 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 3 ms
6,940 KB
testcase_12 AC 3 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 3 ms
6,940 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 3 ms
6,940 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 AC 3 ms
6,940 KB
testcase_19 AC 3 ms
6,944 KB
testcase_20 AC 1 ms
6,940 KB
testcase_21 AC 3 ms
6,944 KB
testcase_22 AC 3 ms
6,940 KB
testcase_23 AC 3 ms
6,940 KB
testcase_24 AC 467 ms
17,888 KB
testcase_25 AC 1,014 ms
33,112 KB
testcase_26 AC 54 ms
6,944 KB
testcase_27 AC 444 ms
16,400 KB
testcase_28 AC 944 ms
31,280 KB
testcase_29 AC 220 ms
10,968 KB
testcase_30 AC 484 ms
17,520 KB
testcase_31 AC 958 ms
32,080 KB
testcase_32 AC 54 ms
6,944 KB
testcase_33 AC 117 ms
6,944 KB
testcase_34 AC 113 ms
6,940 KB
testcase_35 AC 941 ms
29,736 KB
testcase_36 AC 944 ms
29,716 KB
testcase_37 AC 956 ms
33,068 KB
testcase_38 AC 25 ms
6,940 KB
testcase_39 AC 940 ms
30,116 KB
testcase_40 AC 454 ms
16,680 KB
testcase_41 AC 461 ms
17,780 KB
testcase_42 AC 941 ms
29,240 KB
testcase_43 AC 14 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<vector<int>> vvi;
typedef vector<vector<long long>> vvl;
typedef long double ld;
typedef pair<int, int> P;

template <int m> ostream& operator<<(ostream& os, const static_modint<m>& a) {os << a.val(); return os;}
template <int m> ostream& operator<<(ostream& os, const dynamic_modint<m>& a) {os << a.val(); return os;}
template <int m> istream& operator>>(istream& is, static_modint<m>& a) {long long x; is >> x; a = x; return is;}
template <int m> istream& operator>>(istream& is, dynamic_modint<m>& a) {long long x; is >> x; a = x; return is;}
template<typename T> istream& operator>>(istream& is, vector<T>& v){int n = v.size(); assert(n > 0); rep(i, n) is >> v[i]; return is;}
template<typename U, typename T> ostream& operator<<(ostream& os, const pair<U, T>& p){os << p.first << ' ' << p.second; return os;}
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : " "); return os;}
template<typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : ""); return os;}
template<typename T> ostream& operator<<(ostream& os, const set<T>& se){for(T x : se) os << x << " "; os << "\n"; return os;}
template<typename T> ostream& operator<<(ostream& os, const unordered_set<T>& se){for(T x : se) os << x << " "; os << "\n"; return os;}
template<typename S, auto op, auto e> ostream& operator<<(ostream& os, const atcoder::segtree<S, op, e>& seg){int n = seg.max_right(0, [](S){return true;}); rep(i, n) os << seg.get(i) << (i == n - 1 ? "\n" : " "); return os;}
template<typename S, auto op, auto e, typename F, auto mapping, auto composition, auto id> ostream& operator<<(ostream& os, const atcoder::lazy_segtree<S, op, e, F, mapping, composition, id>& seg){int n = seg.max_right(0, [](S){return true;}); rep(i, n) os << seg.get(i) << (i == n - 1 ? "\n" : " "); return os;}

template<typename T> void chmin(T& a, T b){a = min(a, b);}
template<typename T> void chmax(T& a, T b){a = max(a, b);}

using mint = modint998244353;

// combination mod prime
// https://youtu.be/8uowVvQ_-Mo?t=6002
// https://youtu.be/Tgd_zLfRZOQ?t=9928
struct modinv {
  int n; vector<mint> d;
  modinv(): n(2), d({0,1}) {}
  mint operator()(int i) {
    while (n <= i) d.push_back(-d[mint::mod()%n]*(mint::mod()/n)), ++n;
    return d[i];
  }
  mint operator[](int i) const { return d[i];}
} invs;
struct modfact {
  int n; vector<mint> d;
  modfact(): n(2), d({1,1}) {}
  mint operator()(int i) {
    while (n <= i) d.push_back(d.back()*n), ++n;
    return d[i];
  }
  mint operator[](int i) const { return d[i];}
} facts;
struct modfactinv {
  int n; vector<mint> d;
  modfactinv(): n(2), d({1,1}) {}
  mint operator()(int i) {
    while (n <= i) d.push_back(d.back()*invs(n)), ++n;
    return d[i];
  }
  mint operator[](int i) const { return d[i];}
} ifacts;
mint comb(int n, int k) {
  if (n < k || k < 0) return 0;
  return facts(n)*ifacts(k)*ifacts(n-k);
}

template< typename T >
struct FormalPowerSeries : vector< T > {
  using vector< T >::vector;
  using P = FormalPowerSeries;
  
  template<class...Args> FormalPowerSeries(Args...args): vector<T>(args...) {}
  FormalPowerSeries(initializer_list<T> a): vector<T>(a.begin(),a.end()) {}

  using MULT = function< P(P, P) >;

  static MULT &get_mult() {
    static MULT mult = [&](P a, P b){
		P res(convolution(a, b));
		return res;
	};
    return mult;
  }

  static void set_fft(MULT f) {
    get_mult() = f;
  }

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

  P operator+(const P &r) const { return P(*this) += r; }

  P operator+(const T &v) const { return P(*this) += v; }

  P operator-(const P &r) const { return P(*this) -= r; }

  P operator-(const T &v) const { return P(*this) -= v; }

  P operator*(const P &r) const { return P(*this) *= r; }

  P operator*(const T &v) const { return P(*this) *= v; }

  P operator/(const P &r) const { return P(*this) /= r; }

  P operator%(const P &r) const { return P(*this) %= r; }

  P &operator+=(const P &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;
  }

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

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

  P &operator-=(const T &r) {
    if(this->empty()) this->resize(1);
    (*this)[0] -= r;
//    shrink();
    return *this;
  }

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

  P &operator*=(const P &r) {
    if(this->empty() || r.empty()) {
      this->clear();
      return *this;
    }
    assert(get_mult() != nullptr);
    return *this = get_mult()(*this, r);
  }

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

  P operator-() const {
    P ret(this->size());
    for(int i = 0; i < int(this->size()); i++) ret[i] = -(*this)[i];
    return ret;
  }

  P &operator/=(const P &r) {
    if(this->size() < r.size()) {
      this->clear();
      return *this;
    }
    int n = this->size() - r.size() + 1;
    return *this = (rev().pre(n) * r.rev().inv(n)).pre(n).rev(n);
  }

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

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

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

  P rev(int deg = -1) const {
    P ret(*this);
    if(deg != -1) ret.resize(deg, T(0));
    reverse(begin(ret), end(ret));
    return ret;
  }

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

  P integral() const {
    const int n = (int) this->size();
    P ret(n + 1);
    ret[0] = T(0);
    for(int i = 0; i < n; i++) ret[i + 1] = (*this)[i] / T(i + 1);
    return ret;
  }

  // F(0) must not be 0
  P inv(int deg = -1) const {
    assert(((*this)[0]) != T(0));
    const int n = (int) this->size();
    if(deg == -1) deg = n;
    P ret({T(1) / (*this)[0]});
    for(int i = 1; i < deg; i <<= 1) {
      ret = (ret + ret - ret * ret * pre(i << 1)).pre(i << 1);
    }
    return ret.pre(deg);
  }
	/*
	P inv_special(int k, int deg = -1) const {
		// ret = 0 + x/f_1
		P ret({0, T(k)});
		for(int i = 1; (i >> 1) < deg; i <<= 1) {
			// F(G_i(x))
			P fg = (((-ret.pow(k, i << 1) + T(1)) * ret).pre(i << 1) * (-ret + T(1)).inv(i << 1)).pre(i << 1) * T(k).inv();
			// G_(i + 1)(x) = G_i(x) - (F(G_i(x)) - x) / F'(G_i(x))
			// G_(i + 1)(x) = G_i(x) - (F(G_i(x)) - x) / ((d/dx)F(G_i(x)) / (d/dx)G_i(x))
			ret = (ret - ((fg - P{0, 1}) * ret.diff()).pre(i << 1) * (fg.diff()).inv(i << 1)).pre(i << 1);
		}
		return ret.pre(deg);
	}
	*/
  // F(0) must be 1
  P log(int deg = -1) const {
    assert((*this)[0] == T(1));
    const int n = (int) this->size();
    if(deg == -1) deg = n;
    return (this->diff() * this->inv(deg)).pre(deg - 1).integral();
  }

  P sqrt(int deg = -1) const {
    const int n = (int) this->size();
    if(deg == -1) deg = n;

    if((*this)[0] == T(0)) {
      for(int i = 1; i < n; i++) {
        if((*this)[i] != T(0)) {
          if(i & 1) return {};
          if(deg - i / 2 <= 0) break;
          auto ret = (*this >> i).sqrt(deg - i / 2) << (i / 2);
          if(int(ret.size()) < deg) ret.resize(deg, T(0));
          return ret;
        }
      }
      return P(deg, 0);
    }

    P ret({T(1)});
    T inv2 = T(1) / T(2);
    for(int i = 1; i < deg; i <<= 1) {
      ret = (ret + pre(i << 1) * ret.inv(i << 1)) * inv2;
    }
    return ret.pre(deg);
  }

  // F(0) must be 0
  P exp(int deg = -1) const {
    assert((*this)[0] == T(0));
    const int n = (int) this->size();
    if(deg == -1) deg = n;
    P ret({T(1)});
    for(int i = 1; i < deg; i <<= 1) {
      ret = (ret * (pre(i << 1) + T(1) - ret.log(i << 1))).pre(i << 1);
    }
    return ret.pre(deg);
  }

  P pow(int k, int deg = -1) const {
    const int n = (int) this->size();
    if(deg == -1) deg = n;
    for(int i = 0; i < n; i++) {
      if((*this)[i] != T(0)) {
        T rev = T(1) / (*this)[i];
        P C(*this * rev);
        P D(n - i);
        for(int j = i; j < n; j++) D[j - i] = C[j];
        D = (D.log(deg) * T(k)).exp() * (*this)[i].pow(k);
        P E(deg);
        if(i * k > deg) return E;
        auto S = i * k;
        for(int j = 0; j + S < deg && j < D.size(); j++) E[j + S] = D[j];
        return E;
      }
    }
    return *this;
  }
  
	P taylor_shift(T x) const {
		const int n = (int) this->size();
		P p(n), q(n);
		for(int i = 0; i < n; i++) p[i] = facts(i) * (*this)[i];
		for(int i = 0; i < n; i++) q[i] = ifacts(n - 1 - i) * x.pow(n - 1 - i);
		p *= q;
		p = p >> (n - 1);
		for(int i = 0; i < n; i++) p[i] *= ifacts(i);
		return p;
	}

  T get(int idx){
  	assert(idx >= 0);
  	if(idx < int(this->size())) return (*this)[idx];
  	else return T(0);
  }
  
  void set(int idx, T x){
  	assert(idx >= 0);
  	if(idx < int(this->size())) (*this)[idx] = x;
  	else{
  		this->resize(idx + 1);
  		T(0);
  	}
  	return;
  }

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

template<typename T>
T BostanMori(FormalPowerSeries<T> P, FormalPowerSeries<T> Q, ll n){
	assert(P.size() == Q.size());
	if(n == 0) return P[0] / Q[0];
	const int k = P.size();
	FormalPowerSeries<T> R = Q;
	rep(i, k) if(i % 2 == 1) R[i] *= (T)-1;
	P *= R;
	Q *= R;
	FormalPowerSeries<T> U(k), V(k);
	if(n % 2 == 1) rep(i, k - 1) U[i] = P[2 * i + 1];
	else rep(i, k) U[i] = P[2 * i];
	rep(i, k) V[i] = Q[2 * i];
	return BostanMori(U, V, n / 2);
}

// C = A * B in the full_relaxed way
// c_i = \sigma_{j = 0}^{i} a_{j} b_{i - j}
// Postulate: at the point of i, all of the a_j, b_j (0 <= j <= i) are known
// O(N(longN)^2)
// 5e5 * 5e5 -> 3300 ms
// https://judge.yosupo.jp/submission/167521
template<typename T>
void convolution_online(FormalPowerSeries<T>& a, FormalPowerSeries<T>& b, FormalPowerSeries<T>& c, int idx){
	assert(int(c.size()) >= int(a.size()) + int(b.size()) - 1);
	int two = 1;
	rep(_, 30){
		if(idx == 0 and two >= 2) break;
		if(!(idx % two == max(0, two - 2))) break;
		{
			FormalPowerSeries<T> a1(two), b1(two), c1;
			rep(i, two){a1[i] = a[(two - 1) + i]; b1[i] = b[idx - (two - 1) + i];}
			c1 = a1 * b1;
			rep(i, two * 2 - 1) c[idx + i] += c1[i];
		}
		if(idx == (two - 1) * 2) break;
		{
			FormalPowerSeries<T> a2(two), b2(two), c2;
			rep(i, two){a2[i] = a[idx - (two - 1) + i]; b2[i] = b[(two - 1) + i];}
			c2 = a2 * b2;
			rep(i, two * 2 - 1) c[idx + i] += c2[i];
		}
		two *= 2;
	}
}

namespace sparse{
	// f^k (mod x^n)for sparse FPS f
	// O(N * M) (M is for # of terms of f)
	// Requirement : f0 = 1
	vector<mint> pow(vector<pair<int, mint>> f, int N, mint k){
		assert(int(f.size()) > 0);
		assert(f[0].first == 0);
		assert(f[0].second == (mint)1);
		vector<pair<int, mint>> f_prime;
		for(auto [n, c] : f) if(n > 0) f_prime.emplace_back(n - 1, c * n);
		vector<mint> F(N);
		vector<mint> F_prime(N);
		F[0] = 1;
		rep(n, N - 1){
			mint res = 0;
			for(auto [m, c] : f){
				if(m == 0) continue;
				if(m > n) break;
				res -= c * F_prime[n - m];
			}
			for(auto [m, c] : f_prime){
				if(m > n) break;
				res += k * c * F[n - m];
			}
			F_prime[n] = res;
			F[n + 1] = res / (n + 1);
		}
		return F;
	}
	// exp(f) (mod x^n)for sparse FPS f
	// O(N * M) (M is for # of terms of f)
	// Requirement : f0 = 0
	vector<mint> exp(vector<pair<int, mint>> f, int N){
		assert(int(f.size()) > 0);
		assert(f[0].first > 0 or f[0].second == 0);
		vector<pair<int, mint>> f_prime;
		for(auto [n, c] : f) if(n > 0) f_prime.emplace_back(n - 1, c * n);
		vector<mint> F(N);
		vector<mint> F_prime(N);
		F[0] = 1;
		rep(n, N - 1){
			mint res = 0;
			for(auto [m, c] : f_prime){
				if(m > n) break;
				res += c * F[n - m];
			}
			F_prime[n] = res;
			F[n + 1] = res / (n + 1);
		}
		return F;
	}
	// g / f (mod x^n for sparse FPS f and not sparse FPS g
	// O(N * M) (M is for # of terms of f)
	// Requirement : f0 = 1
	vector<mint> quotient(vector<mint> g, vector<pair<int, mint>> f, int N){
		assert(int(g.size()) == N);
		assert(int(f.size()) > 0);
		assert(f[0].first == 0);
		assert(f[0].second == (mint)1);
		vector<mint> F(N);
		F[0] = g[0];
		for(int n = 1; n < N; n++){
			mint res = g[n];
			for(auto [m, c] : f){
				if(m == 0) continue;
				if(m > n) break;
				res -= c * F[n - m];
			}
			F[n] = res;
		}
		return F;
	}
	// log f (mod x^n) for sparse FPS f
	// O(N * M) (M is for # of terms of f)
	// Requirement : f0 = 1
	vector<mint> log(vector<pair<int, mint>> f, int N){
		assert(int(f.size()) > 0);
		assert(f[0].first == 0);
		assert(f[0].second == (mint)1);
		vector<mint> f_prime(N);
		for(auto [n, c] : f){
			if(n == 0) continue;
			f_prime[n - 1] = c * n;
		}
		vector<mint> F_prime = quotient(f_prime, f, N);
		vector<mint> F(N);
		rep(n, N - 1) F[n + 1] = F_prime[n] / (n + 1);
		return F;
	}
}

// https://nyaannyaan.github.io/library/fps/berlekamp-massey.hpp
// find series c such that a_n = c_1 a_(n-1) + .. + c_k a_(n-k)
// need at least 2 * k terms
template <typename T>
vector<T> BerlekampMassey(const vector<T> &s) {
	const int N = (int)s.size();
	vector<T> b, c;
	b.reserve(N + 1);
	c.reserve(N + 1);
	b.push_back(T(1));
	c.push_back(T(1));
	T y = T(1);
	for (int ed = 1; ed <= N; ed++) {
		int l = int(c.size()), m = int(b.size());
		T x = 0;
		for (int i = 0; i < l; i++) x += c[i] * s[ed - l + i];
		b.emplace_back(T(0));
		m++;
		if (x == T(0)) continue;
		T freq = x / y;
		if (l < m) {
			auto tmp = c;
			c.insert(begin(c), m - l, T(0));
			for (int i = 0; i < m; i++) c[m - 1 - i] -= freq * b[m - 1 - i];
			b = tmp;
			y = x;
		} else {
			for (int i = 0; i < m; i++) c[l - 1 - i] -= freq * b[m - 1 - i];
		}
	}
	reverse(begin(c), end(c));
	return c;
}

template <typename T>
T LinearRecurrence(const FormalPowerSeries<T>& s, long long n){
	FormalPowerSeries<T> c = BerlekampMassey(s);
	auto t = c.size();
	FormalPowerSeries<T> P = c * s;
	P = FormalPowerSeries<T>(P.begin(), P.begin() + t);
	P[t - 1] = 0;
	return BostanMori(P, c, n);
};


// Thanks for maspy-san's submission
// https://judge.yosupo.jp/submission/197702
template <typename T = mint>
vector<vector<T>> convolution2d(vector<vector<T>>& f, vector<vector<T>>& g) {
	auto shape = [&](vector<vector<T>>& f) -> pair<int, int> {
		int H = len(f);
		int W = (H == 0 ? 0 : len(f[0]));
		return {H, W};
	};
	auto [H1, W1] = shape(f);
	auto [H2, W2] = shape(g);
	int H = H1 + H2 - 1;
	int W = W1 + W2 - 1;

	FormalPowerSeries<T> ff(H1 * W);
	FormalPowerSeries<T> gg(H2 * W);
	for(int x = 0; x < H1; x++) for(int y = 0; y < W1; y++) ff[W * x + y] = f[x][y];
	for(int x = 0; x < H2; x++) for(int y = 0; y < W2; y++) gg[W * x + y] = g[x][y];
	auto hh = convolution(ff, gg);
	vector<vector<T>> h(H, vector<T>(W));
	for(int x = 0; x < H; x++) for(int y = 0; y < W; y++) h[x][y] = hh[W * x + y];
	return h;
}

template<typename T>
struct Merger{
	int n;
	using P = FormalPowerSeries<T>;
	using Comp = std::function<bool(const P&, const P&)>;
	Comp comp = [](const P& a, const P& b){return a.size() > b.size();};
	priority_queue<P, vector<P>, Comp> pq;
	Merger(int n = -1) : n(n), pq(comp){
		pq.push(P{1});
	}
	void add(P r){
		pq.push(r);
	}
	P get(){
		while(pq.size() > 1){
			auto f = pq.top(); pq.pop();
			auto g = pq.top(); pq.pop();
			f *= g;
			if(n != -1) if(int(f.size()) > n) f.resize(n + 1);
			pq.push(f);
		}
		P res = pq.top();
		return res;
	}
};


void infer(mint a){
	if(a == 0){cout << "0/1 "; return;}
	int p = a.mod();
	long long u0 = 0, v0 = 1, w0 = a.val() * u0 + p * v0;
	long long u1 = 1, v1 = 0, w1 = a.val() * u1 + p * v1;
	while (w0*w0 >= p) {
		long long q = w0 / w1;
		u0 -= q * u1; v0 -= q * v1;	w0 -= q * w1;
		u0 ^= u1; u1 ^= u0; u0 ^= u1;
		v0 ^= v1; v1 ^= v0; v0 ^= v1;
		w0 ^= w1; w1 ^= w0; w0 ^= w1;
	}
	if(u0 < 0){cout << '-'; u0 = abs(u0);}
	cout << w0 << '/' << u0 << ' ';
}

template<typename T>
void infer(vector<T> v){for(auto& a : v) infer(a); cout << "\n";}

using FPS = FormalPowerSeries<mint>;

int main(){
	int n;
	cin >> n;
	FPS f = {1,1,1};
	f = f.pow(n, n + 1);
	cout << (f[n] - 1) / 2 << "\n";
	return 0;
}
0