結果

問題 No.2062 Sum of Subset mod 999630629
ユーザー akuaakua
提出日時 2022-09-12 19:40:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 586 ms / 5,000 ms
コード長 6,943 bytes
コンパイル時間 3,320 ms
コンパイル使用メモリ 186,716 KB
実行使用メモリ 84,724 KB
最終ジャッジ日時 2023-08-19 23:00:53
合計ジャッジ時間 9,840 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 113 ms
80,936 KB
testcase_01 AC 114 ms
81,148 KB
testcase_02 AC 113 ms
81,212 KB
testcase_03 AC 112 ms
81,160 KB
testcase_04 AC 113 ms
81,040 KB
testcase_05 AC 113 ms
81,084 KB
testcase_06 AC 114 ms
81,204 KB
testcase_07 AC 112 ms
81,100 KB
testcase_08 AC 120 ms
81,600 KB
testcase_09 AC 120 ms
81,696 KB
testcase_10 AC 118 ms
81,436 KB
testcase_11 AC 132 ms
82,376 KB
testcase_12 AC 135 ms
82,532 KB
testcase_13 AC 125 ms
82,348 KB
testcase_14 AC 136 ms
82,416 KB
testcase_15 AC 123 ms
82,104 KB
testcase_16 AC 130 ms
82,340 KB
testcase_17 AC 131 ms
82,424 KB
testcase_18 AC 126 ms
82,260 KB
testcase_19 AC 123 ms
82,076 KB
testcase_20 AC 125 ms
82,140 KB
testcase_21 AC 124 ms
82,176 KB
testcase_22 AC 124 ms
82,212 KB
testcase_23 AC 119 ms
81,696 KB
testcase_24 AC 118 ms
81,564 KB
testcase_25 AC 182 ms
84,680 KB
testcase_26 AC 224 ms
84,600 KB
testcase_27 AC 198 ms
84,724 KB
testcase_28 AC 198 ms
84,660 KB
testcase_29 AC 160 ms
84,684 KB
testcase_30 AC 148 ms
84,096 KB
testcase_31 AC 586 ms
84,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <atcoder/all>
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <math.h>
#include <iomanip>
using namespace std;  
using namespace atcoder;

typedef long long ll;
typedef pair<int,int> pii;
//形式的冪級数
 
#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)
 
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;
  }
  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()};
    while (res.size() < d) {
      int m = size(res);
      F f(begin(*this), begin(*this) + min(n, 2*m));
      F r(res);
      f.resize(2*m), internal::butterfly(f);
      r.resize(2*m), internal::butterfly(r);
      rep(i, 2*m) f[i] *= r[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] *= r[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);
    }
    return {res.begin(), res.begin() + d};
  }
 
  // fast: FMT-friendly modulus only
  F &operator*=(const F &g) {
    int n = (*this).size();
    *this = convolution(*this, g);
    (*this).resize(n);
    return *this;
  }
  F &operator/=(const F &g) {
    int n = (*this).size();
    *this = convolution(*this, g.inv(n));
    (*this).resize(n);
    return *this;
  }
 
  // // naive
  // F &operator*=(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 &operator/=(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;
  // }
 
  // sparse
  F &operator*=(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 &operator/=(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;
  }
 
  // multiply and divide (1 + cz^d)
  void multiply(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;
  }
  void divide(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;
  }
 
  T eval(const T &a) const {
    T x(1), res(0);
    for (auto e : *this) res += e * x, x *= a;
    return res;
  }
 
  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*(vector<pair<int, T>> g) const { return F(*this) *= g; }
  F operator/(vector<pair<int, T>> g) const { return F(*this) /= g; }
};
 
using mint = modint998244353;
using fps = FormalPowerSeries<mint>;
using sfps = vector<pair<int, mint>>;
const int mod=998244353;
const int mod2=999630629;
const int N=10005;

ll pow_pow(ll x,ll n,ll mod){
    if(n==0) return 1; 
    x%=mod;
    ll res=pow_pow(x*x%mod,n/2,mod);
    if(n&1)res=res*x%mod;
    return res;
}

struct combination {
  vector<mint> fact, ifact;
  combination(int n):fact(n+1),ifact(n+1) {
    //assert(n < mod);
    fact[0] = 1;
    for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n) return 0;
    return fact[n]*ifact[k]*ifact[n-k];
  }
  mint p(int n, int k) {
    return fact[n]*ifact[n-k];
  }
} c(10000050);

int main(){cin.tie(0); ios::sync_with_stdio(false);
  int n; cin >> n;
  vector<int> a(n);rep(i,n)cin >> a[i];
  vector<int>  cnt(N+1);
  rep(i,n)cnt[a[i]]++;
  ll sum=0;rep(i,n)sum+=a[i];
  mint ans=pow_pow(2,n-1,mod);
  ans*=sum;
  int k=sum-mod2;
  if(k<0){
    cout << ans.val() << endl;
    return 0;
  }
  fps f={1};
  f.resize(k+1);
  rep(i,N+1){
    if(cnt[i]==0)continue;
    sfps sf;
    rep(j,cnt[i]+1)sf.push_back({j*i,c(cnt[i],j)});
    f*=sf;
  }
  rep(i,k+1){
    ans-=f[i]*mod2;
  }
  cout << ans.val() << endl; 
   
}
0