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

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

typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define FOR1(i, n) for (int(i) = 1; (i) <= (n); (i)++)
#define FORI(i, n) for (int(i) = n - 1; (i) >= 0; (i)--)

const ll MOD = 998244353;
template <class T> T powmod(T x, ll n, ll m) {
  T r = 1;
  T a = x % m;
  while (n > 0) {
    if (n & 1)
      r = (r * a) % m;
    a = (a * a) % m;
    n = n >> 1;
  }
  return r;
}

const int M = 3002;
ll dp[M][M];

int main(int argc, char **argv) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << setprecision(15);
  if (argc == 2 && atoi(argv[1]) == 123456789)
    freopen("d:\\\\code\\\\cpp\\\\contests\\\\stdin", "r", stdin);

  int n, s;
#if 1
  cin >> n >> s;
  vector<int> A(n);
  FOR(i, n) cin >> A[i];
#else
  n = 3000;
  s = n;
  vector<int> A(n);
  FOR(i, n) A[i] = i + 1; // rand() % 3000;
#endif
  sort(A.begin(), A.end());

  // dp[s][k] - sum of contributions for each subset which sums to s
  vector<ll> v(s + 1);
  v[0] = powmod(2ll, n, MOD);
  const ll i2 = (MOD + 1) / 2;
  FOR(i, n) {
    vector<ll> v2 = v;
    for (int x = s - A[i]; x >= 0; x--) {
      v2[x + A[i]] += v[x] * i2 % MOD;
      v2[x + A[i]] -= MOD * (v2[x + A[i]] >= MOD);
    }
    v = v2;
  }
  cout << v[s] << endl;

  if (argc == 2 && atoi(argv[1]) == 123456789)
    cout << clock() * 1.0 / CLOCKS_PER_SEC << " sec\\n";
  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define FOR1(i, n) for (int(i) = 1; (i) <= (n); (i)++)
#define FORI(i, n) for (int(i) = n - 1; (i) >= 0; (i)--)

const ll MOD = 998244353;
template <class T> T powmod(T x, ll n, ll m) {
  T r = 1;
  T a = x % m;
  while (n > 0) {
    if (n & 1)
      r = (r * a) % m;
    a = (a * a) % m;
    n = n >> 1;
  }
  return r;
}

const int M = 3002;
ll dp[M][M];

int main(int argc, char **argv) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << setprecision(15);
  if (argc == 2 && atoi(argv[1]) == 123456789)
    freopen("d:\\\\code\\\\cpp\\\\contests\\\\stdin", "r", stdin);

  int n, s;
#if 1
  cin >> n >> s;
  vector<int> A(n);
  FOR(i, n) cin >> A[i];
#else
  n = 3000;
  s = n;
  vector<int> A(n);
  FOR(i, n) A[i] = i + 1; // rand() % 3000;
#endif
  sort(A.begin(), A.end());

  // dp[s][k] - how many subsets with k elems sum to s
  dp[0][0] = 1;
  map<pii, ll> H;
  H[{0, 0}] = 1;
  FOR(i, n) {

// #define TEST
#ifdef TEST
    map<pii, ll> H2 = H;
    for (auto it : H) {
      int x = it.first.first;
      int k = it.first.second;
      if (x + A[i] <= s) {
        H2[{x + A[i], k + 1}] += it.second;
        H2[{x + A[i], k + 1}] -= MOD * (H2[{x + A[i], k + 1}] >= MOD);
      }
    }
    H = H2;
#else
    for (int x = s - A[i]; x >= 0; x--) {
      for (int k = n - 1; k >= 0; k--)
        if (dp[x][k]) {
          dp[x + A[i]][k + 1] += dp[x][k];
          dp[x + A[i]][k + 1] -= MOD * (dp[x + A[i]][k + 1] >= MOD);
        }
    }
#endif
    //        cout << i << endl;
  }

  ll sol = 0;
  FOR1(k, n) {
#ifdef TEST
    sol += H[{s, k}] * powmod(2ll, n - k, MOD) % MOD;
#else
    sol += dp[s][k] * powmod(2ll, n - k, MOD) % MOD;
#endif
    sol -= MOD * (sol >= MOD);
  }
  cout << sol << endl;

  //    ll cnt = 0;
  //    FOR1(x, s) FOR1(k, n) cnt += dp[x][k] >0;
  //    cout << cnt << endl;

  if (argc == 2 && atoi(argv[1]) == 123456789)
    cout << clock() * 1.0 / CLOCKS_PER_SEC << " sec\\n";
  return 0;
}