結果

問題 No.2062 Sum of Subset mod 999630629
ユーザー shobonvipshobonvip
提出日時 2022-08-27 00:21:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 11,494 bytes
コンパイル時間 5,344 ms
コンパイル使用メモリ 288,072 KB
実行使用メモリ 33,616 KB
最終ジャッジ日時 2024-10-14 01:12:34
合計ジャッジ時間 45,357 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
23,892 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,248 KB
testcase_04 AC 2 ms
5,248 KB
testcase_05 AC 2 ms
5,248 KB
testcase_06 AC 2 ms
5,248 KB
testcase_07 AC 2 ms
5,248 KB
testcase_08 AC 10 ms
5,248 KB
testcase_09 AC 9 ms
5,248 KB
testcase_10 AC 8 ms
5,248 KB
testcase_11 AC 632 ms
10,812 KB
testcase_12 AC 635 ms
11,196 KB
testcase_13 AC 309 ms
7,484 KB
testcase_14 AC 652 ms
11,060 KB
testcase_15 AC 22 ms
5,248 KB
testcase_16 AC 732 ms
10,756 KB
testcase_17 AC 641 ms
10,680 KB
testcase_18 AC 314 ms
7,724 KB
testcase_19 AC 166 ms
5,248 KB
testcase_20 AC 357 ms
5,624 KB
testcase_21 AC 449 ms
7,280 KB
testcase_22 AC 292 ms
5,596 KB
testcase_23 AC 8 ms
5,248 KB
testcase_24 AC 8 ms
5,248 KB
testcase_25 AC 4,968 ms
33,492 KB
testcase_26 AC 4,987 ms
33,520 KB
testcase_27 AC 4,992 ms
33,616 KB
testcase_28 AC 4,992 ms
33,488 KB
testcase_29 AC 4,106 ms
33,364 KB
testcase_30 AC 3,297 ms
18,656 KB
testcase_31 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;

istream &operator>>(istream &is, modint998244353 &a) { long long v; is >> v; a = v; return is; }
ostream &operator<<(ostream &os, const modint998244353 &a) { return os << a.val(); }
istream &operator>>(istream &is, modint1000000007 &a) { long long v; is >> v; a = v; return is; }
ostream &operator<<(ostream &os, const modint1000000007 &a) { return os << a.val(); }
template<int m> istream &operator>>(istream &is, static_modint<m> &a) { long long v; is >> v; a = v; return is; }
template<int m> istream &operator>>(istream &is, dynamic_modint<m> &a) { long long v; is >> v; a = v; return is; }
template<int m> ostream &operator<<(ostream &os, const static_modint<m> &a) { return os << a.val(); }
template<int m> ostream &operator<<(ostream &os, const dynamic_modint<m> &a) { return os << a.val(); }
#define rep2(i, m, n) for (int i = (m); i < (n); ++i)
#define rep(i, n) rep2(i, 0, n)
#define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i)
#define drep(i, n) drep2(i, n, 0)
using ll = long long;
template<class T> istream &operator>>(istream &is, vector<T> &v) { for (auto &e : v) is >> e; return is; }
template<class T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; }
struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;


// verified by:
// https://judge.yosupo.jp/problem/convolution_mod
// https://judge.yosupo.jp/problem/inv_of_formal_power_series
// https://judge.yosupo.jp/problem/log_of_formal_power_series
// https://judge.yosupo.jp/problem/exp_of_formal_power_series
// https://judge.yosupo.jp/problem/pow_of_formal_power_series


template<class T>
struct FormalPowerSeries : vector<T> {
  using vector<T>::vector;
  using vector<T>::operator=;
  using F = FormalPowerSeries;

  F operator-() const {
    F res(*this);
    for (auto &e : res) e = -e;
    return res;
  }
  F &operator*=(const T &g) {
    for (auto &e : *this) e *= g;
    return *this;
  }
  F &operator/=(const T &g) {
    assert(g != T(0));
    *this *= g.inv();
    return *this;
  }
  F &operator+=(const F &g) {
    int n = this->size(), m = g.size();
    rep(i, min(n, m)) (*this)[i] += g[i];
    return *this;
  }
  F &operator-=(const F &g) {
    int n = this->size(), m = g.size();
    rep(i, min(n, m)) (*this)[i] -= g[i];
    return *this;
  }
  F &operator<<=(const int d) {
    int n = this->size();
    this->insert(this->begin(), d, 0);
    this->resize(n);
    return *this;
  }
  F &operator>>=(const int d) {
    int n = this->size();
    this->erase(this->begin(), this->begin() + min(n, d));
    this->resize(n);
    return *this;
  }

  // O(n log n)
  F inv(int d = -1) const {
    int n = this->size();
    assert(n != 0 && (*this)[0] != 0);
    if (d == -1) d = n;
    assert(d >= 0);
    F res{(*this)[0].inv()};
    for (int m = 1; m < d; m *= 2) {
      F f(this->begin(), this->begin() + min(n, 2*m));
      F g(res);
      f.resize(2*m), internal::butterfly(f);
      g.resize(2*m), internal::butterfly(g);
      rep(i, 2*m) f[i] *= g[i];
      internal::butterfly_inv(f);
      f.erase(f.begin(), f.begin() + m);
      f.resize(2*m), internal::butterfly(f);
      rep(i, 2*m) f[i] *= g[i];
      internal::butterfly_inv(f);
      T iz = T(2*m).inv(); iz *= -iz;
      rep(i, m) f[i] *= iz;
      res.insert(res.end(), f.begin(), f.begin() + m);
    }
    res.resize(d);
    return res;
  }

  // fast: FMT-friendly modulus only
  // O(n log n)
  F &multiply_inplace(const F &g, int d = -1) {
    int n = this->size();
    if (d == -1) d = n;
    assert(d >= 0);
    *this = convolution(move(*this), g);
    this->resize(d);
    return *this;
  }
  F multiply(const F &g, const int d = -1) const { return F(*this).multiply_inplace(g, d); }
  // O(n log n)
  F &divide_inplace(const F &g, int d = -1) {
    int n = this->size();
    if (d == -1) d = n;
    assert(d >= 0);
    *this = convolution(move(*this), g.inv(d));
    this->resize(d);
    return *this;
  }
  F divide(const F &g, const int d = -1) const { return F(*this).divide_inplace(g, d); }

  // // naive
  // // O(n^2)
  // F &multiply_inplace(const F &g) {
  //   int n = this->size(), m = g.size();
  //   drep(i, n) {
  //     (*this)[i] *= g[0];
  //     rep2(j, 1, min(i+1, m)) (*this)[i] += (*this)[i-j] * g[j];
  //   }
  //   return *this;
  // }
  // F multiply(const F &g) const { return F(*this).multiply_inplace(g); }
  // // O(n^2)
  // F &divide_inplace(const F &g) {
  //   assert(g[0] != T(0));
  //   T ig0 = g[0].inv();
  //   int n = this->size(), m = g.size();
  //   rep(i, n) {
  //     rep2(j, 1, min(i+1, m)) (*this)[i] -= (*this)[i-j] * g[j];
  //     (*this)[i] *= ig0;
  //   }
  //   return *this;
  // }
  // F divide(const F &g) const { return F(*this).divide_inplace(g); }

  // sparse
  // O(nk)
  F &multiply_inplace(vector<pair<int, T>> g) {
    int n = this->size();
    auto [d, c] = g.front();
    if (d == 0) g.erase(g.begin());
    else c = 0;
    drep(i, n) {
      (*this)[i] *= c;
      for (auto &[j, b] : g) {
        if (j > i) break;
        (*this)[i] += (*this)[i-j] * b;
      }
    }
    return *this;
  }
  F multiply(const vector<pair<int, T>> &g) const { return F(*this).multiply_inplace(g); }
  // O(nk)
  F &divide_inplace(vector<pair<int, T>> g) {
    int n = this->size();
    auto [d, c] = g.front();
    assert(d == 0 && c != T(0));
    T ic = c.inv();
    g.erase(g.begin());
    rep(i, n) {
      for (auto &[j, b] : g) {
        if (j > i) break;
        (*this)[i] -= (*this)[i-j] * b;
      }
      (*this)[i] *= ic;
    }
    return *this;
  }
  F divide(const vector<pair<int, T>> &g) const { return F(*this).divide_inplace(g); }

  // multiply and divide (1 + cz^d)
  // O(n)
  void multiply_inplace(const int d, const T c) { 
    int n = this->size();
    if (c == T(1)) drep(i, n-d) (*this)[i+d] += (*this)[i];
    else if (c == T(-1)) drep(i, n-d) (*this)[i+d] -= (*this)[i];
    else drep(i, n-d) (*this)[i+d] += (*this)[i] * c;
  }
  // O(n)
  void divide_inplace(const int d, const T c) {
    int n = this->size();
    if (c == T(1)) rep(i, n-d) (*this)[i+d] -= (*this)[i];
    else if (c == T(-1)) rep(i, n-d) (*this)[i+d] += (*this)[i];
    else rep(i, n-d) (*this)[i+d] -= (*this)[i] * c;
  }

  // O(n)
  T eval(const T &a) const {
    T x(1), res(0);
    for (auto e : *this) res += e * x, x *= a;
    return res;
  }

  // O(n)
  F &integ_inplace() {
    int n = this->size();
    assert(n > 0);
    if (n == 1) return *this = F{0};
    this->insert(this->begin(), 0);
    this->pop_back();
    vector<T> inv(n);
    inv[1] = 1;
    int p = T::mod();
    rep2(i, 2, n) inv[i] = - inv[p%i] * (p/i);
    rep2(i, 2, n) (*this)[i] *= inv[i];
    return *this;
  }
  F integ() const { return F(*this).integ_inplace(); }

  // O(n)
  F &deriv_inplace() {
    int n = this->size();
    assert(n > 0);
    rep2(i, 2, n) (*this)[i] *= i;
    this->erase(this->begin());
    this->push_back(0);
    return *this;
  }
  F deriv() const { return F(*this).deriv_inplace(); }

  // O(n log n)
  F log(int d = -1) const {
    int n = this->size();
    assert(n > 0 && (*this)[0] == 1);
    if (d == -1) d = n;
    assert(d > 0);
    F res(this->deriv());
    res.divide_inplace(*this, d);
    res.integ_inplace();
    return res;
  }

  // O(n log n)
  // https://arxiv.org/abs/1301.5804 (Figure 2, right)
  F &exp_inplace(int d = -1) {
    int n = this->size();
    assert(n > 0 && (*this)[0] == 0);
    if (d == -1) d = n;
    assert(d >= 0);
    F g{1}, g_fft;
    this->resize(d);
    (*this)[0] = 1;
    F h_drv(this->deriv());
    for (int m = 1; m < d; m *= 2) {
      // prepare
      F f_fft(this->begin(), this->begin() + m);
      f_fft.resize(2*m), internal::butterfly(f_fft);

      // Step 2.a'
      if (m > 1) {
        F _f(m);
        rep(i, m) _f[i] = f_fft[i] * g_fft[i];
        internal::butterfly_inv(_f);
        _f.erase(_f.begin(), _f.begin() + m/2);
        _f.resize(m), internal::butterfly(_f);
        rep(i, m) _f[i] *= g_fft[i];
        internal::butterfly_inv(_f);
        _f.resize(m/2);
        _f /= T(-m) * m;
        g.insert(g.end(), _f.begin(), _f.begin() + m/2);
      }

      // Step 2.b'--d'
      F t(this->begin(), this->begin() + m);
      t.deriv_inplace();
      {
        // Step 2.b'
        F r{h_drv.begin(), h_drv.begin() + m-1};
        // Step 2.c'
        r.resize(m); internal::butterfly(r);
        rep(i, m) r[i] *= f_fft[i];
        internal::butterfly_inv(r);
        r /= -m;
        // Step 2.d'
        t += r;
        t.insert(t.begin(), t.back()); t.pop_back();
      }

      // Step 2.e'
      t.resize(2*m); internal::butterfly(t); 
      g_fft = g; g_fft.resize(2*m); internal::butterfly(g_fft);
      rep(i, 2*m) t[i] *= g_fft[i];
      internal::butterfly_inv(t);
      t.resize(m);
      t /= 2*m;
      
      // Step 2.f'
      F v(this->begin() + m, this->begin() + min(d, 2*m)); v.resize(m);
      t.insert(t.begin(), m-1, 0); t.push_back(0);
      t.integ_inplace();
      rep(i, m) v[i] -= t[m+i];

      // Step 2.g'
      v.resize(2*m); internal::butterfly(v);
      rep(i, 2*m) v[i] *= f_fft[i];
      internal::butterfly_inv(v);
      v.resize(m);
      v /= 2*m;

      // Step 2.h'
      rep(i, min(d-m, m)) (*this)[m+i] = v[i];
    }
    return *this;
  }
  F exp(const int d = -1) const { return F(*this).exp_inplace(d); }

  // O(n log n)
  F &pow_inplace(ll k, int d = -1) {
    int n = this->size();
    if (d == -1) d = n;
    assert(d >= 0);
    int l = 0;
    while ((*this)[l] == 0) ++l;
    if (l > d/k) return *this = F(d);
    T ic = (*this)[l].inv();
    T pc = (*this)[l].pow(k);
    this->erase(this->begin(), this->begin() + l);
    *this *= ic;
    *this = this->log();
    *this *= k;
    this->exp_inplace();
    *this *= pc;
    this->insert(this->begin(), l*k, 0);
    this->resize(d);
    return *this;
  }
  F pow(const ll k, const int d = -1) const { return F(*this).pow_inplace(k, d); }

  F operator*(const T &g) const { return F(*this) *= g; }
  F operator/(const T &g) const { return F(*this) /= g; }
  F operator+(const F &g) const { return F(*this) += g; }
  F operator-(const F &g) const { return F(*this) -= g; }
  F operator<<(const int d) const { return F(*this) <<= d; }
  F operator>>(const int d) const { return F(*this) >>= d; }
  F operator*(const F &g) const { return F(*this) *= g; }
  F operator/(const F &g) const { return F(*this) /= g; }
  F operator*(const vector<pair<int, T>> &g) const { return F(*this) *= g; }
  F operator/(const vector<pair<int, T>> &g) const { return F(*this) /= g; }
};

typedef modint998244353 mint;
typedef FormalPowerSeries<mint> fps;
//--------


typedef long long ll;

int main(){
	// sum(A[i])はかなり小さいので, デカイやつは一周まわるので除外しまくる.
	int N;
	cin >> N;
	vector<int> A(N);
	
	mint ans = 0;
	mint nnv = mint(2).pow(N-1);
	vector<int> Q(10001);
	int asum = 0;

	for (int i=0; i<N; i++){
		cin >> A[i];
		ans += nnv * A[i];
		Q[A[i]] += 1;
		asum += A[i];
	}

	if (asum >= 999630629){
		int l = asum - 999630629 + 1;
		fps F(l);
		F[0] = 1;
		for (int i=0; i<min(l, 10001); i++){
			if (Q[i] > 0){
				fps G(l);
				G[0] = 1;
				G[i] = 1;
				F.multiply_inplace((G.log()*Q[i]).exp());
			}
		}
		mint jogai = 0;
		for (int i=0; i<l; i++){
			jogai += F[i];
		}
		ans -= jogai * mint(999630629);
	}

	cout << ans.val() << endl;
}
0