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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vpii = vector<pii>;

#define PI 3.1415926535897932384626433832795

class mod_ll {
public:
  ll m_v;
  static const ll m_m = 998244353; // prime value ex. 1000000009
  mod_ll(ll v = 0) : m_v(v) {}
  mod_ll operator=(ll rhs) {
    m_v = rhs % m_m;
    return *this;
  }
  ll operator()() { return m_v; }
  mod_ll operator=(mod_ll rhs) {
    m_v = rhs.m_v;
    return *this;
  }
  mod_ll operator+=(ll rhs) {
    m_v = (m_v + rhs) % m_m;
    return *this;
  }
  mod_ll operator+=(mod_ll rhs) {
    m_v = (m_v + rhs.m_v) % m_m;
    return *this;
  }
  mod_ll operator+(mod_ll rhs) {
    auto ret = *this;
    return ret += rhs;
  }
  mod_ll operator-=(mod_ll rhs) {
    m_v = (m_v - rhs.m_v + m_m) % m_m;
    return *this;
  }
  mod_ll operator-(mod_ll rhs) {
    auto ret = *this;
    return ret -= rhs;
  }
  mod_ll operator*=(ll rhs) {
    m_v = (m_v * rhs) % m_m;
    return *this;
  }
  mod_ll operator*=(mod_ll rhs) {
    m_v = (m_v * rhs.m_v) % m_m;
    return *this;
  }
  mod_ll operator*(mod_ll rhs) {
    mod_ll ret = *this;
    return ret *= rhs;
  }
  mod_ll operator^(ll rhs) {
    mod_ll t = *this;
    mod_ll ret(1LL);

    for (; rhs > 0; rhs >>= 1) {
      if (rhs & 1)
        ret *= t;
      t *= t;
    }
    return ret;
  }
  mod_ll operator/=(mod_ll rhs) { return *this *= (rhs ^ (m_m - 2)); }
  mod_ll operator/=(ll rhs) {
    mod_ll t(rhs);
    return *this /= t;
  }
  mod_ll operator/(mod_ll rhs) {
    mod_ll ret = *this;
    return ret /= rhs;
  }
};

mod_ll mod_fact(int n) {
  static vector<mod_ll> memo(1);

  if (memo.size() <= n) {
    if (memo.size() == 1) {
      memo[0] = 1;
    }
    for (int i = memo.size(); i <= n; i++) {
      memo.push_back(memo.back() * i);
    }
  }
  return memo[n];
}

mod_ll mod_nCr(int n, int r) {
  mod_ll bunsi = mod_fact(n);
  mod_ll bunbo = mod_fact(n - r) * mod_fact(r);
  return bunsi / bunbo;
}

mod_ll mod_nPr(int n, int r) {
  mod_ll ret(1);

  for (int i = 0; i < r; i++) {
    ret *= (n - i);
  }
  return ret;
}

int main(void) {
  int n, s;
  while (scanf("%d%d", &n, &s) == 2) {
    vi a(n);

    for (auto &m : a)
      scanf("%d", &m);

    vector<mod_ll> dp(s + 1);
    mod_ll u(2);
    u = u ^ n;

    dp[0] = u;
    for (int ma : a) {
      for (int i = s - ma; i >= 0; i--) {
        int ts = i + ma;
        dp[ts] += dp[i] / 2;
      }
    }
    mod_ll ans = dp[s];
    printf("%lld\\n", ans());
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vpii = vector<pii>;

#define PI 3.1415926535897932384626433832795

class mod_ll {
public:
  ll m_v;
  static const ll m_m = 998244353; // prime value ex. 1000000009
  mod_ll(ll v = 0) : m_v(v) {}
  mod_ll operator=(ll rhs) {
    m_v = rhs % m_m;
    return *this;
  }
  ll operator()() { return m_v; }
  mod_ll operator=(mod_ll rhs) {
    m_v = rhs.m_v;
    return *this;
  }
  mod_ll operator+=(ll rhs) {
    m_v = (m_v + rhs) % m_m;
    return *this;
  }
  mod_ll operator+=(mod_ll rhs) {
    m_v = (m_v + rhs.m_v) % m_m;
    return *this;
  }
  mod_ll operator+(mod_ll rhs) {
    auto ret = *this;
    return ret += rhs;
  }
  mod_ll operator-=(mod_ll rhs) {
    m_v = (m_v - rhs.m_v + m_m) % m_m;
    return *this;
  }
  mod_ll operator-(mod_ll rhs) {
    auto ret = *this;
    return ret -= rhs;
  }
  mod_ll operator*=(ll rhs) {
    m_v = (m_v * rhs) % m_m;
    return *this;
  }
  mod_ll operator*=(mod_ll rhs) {
    m_v = (m_v * rhs.m_v) % m_m;
    return *this;
  }
  mod_ll operator*(mod_ll rhs) {
    mod_ll ret = *this;
    return ret *= rhs;
  }
  mod_ll operator^(ll rhs) {
    mod_ll t = *this;
    mod_ll ret(1LL);

    for (; rhs > 0; rhs >>= 1) {
      if (rhs & 1)
        ret *= t;
      t *= t;
    }
    return ret;
  }
  mod_ll operator/=(mod_ll rhs) { return *this *= (rhs ^ (m_m - 2)); }
  mod_ll operator/=(ll rhs) {
    mod_ll t(rhs);
    return *this /= t;
  }
  mod_ll operator/(mod_ll rhs) {
    mod_ll ret = *this;
    return ret /= rhs;
  }
};

mod_ll mod_fact(int n) {
  static vector<mod_ll> memo(1);

  if (memo.size() <= n) {
    if (memo.size() == 1) {
      memo[0] = 1;
    }
    for (int i = memo.size(); i <= n; i++) {
      memo.push_back(memo.back() * i);
    }
  }
  return memo[n];
}

mod_ll mod_nCr(int n, int r) {
  mod_ll bunsi = mod_fact(n);
  mod_ll bunbo = mod_fact(n - r) * mod_fact(r);
  return bunsi / bunbo;
}

mod_ll mod_nPr(int n, int r) {
  mod_ll ret(1);

  for (int i = 0; i < r; i++) {
    ret *= (n - i);
  }
  return ret;
}

int main(void) {
  int n, s;
  while (scanf("%d%d", &n, &s) == 2) {
    vi a(n);

    for (auto &m : a)
      scanf("%d", &m);

    vector<vector<mod_ll>> dp(s + 1);
    vector<bool> f(s + 1, false);

    for (auto &m : dp)
      m.resize(n + 1);

    dp[0][0] = 1;
    f[0] = 1;
    for (int ma : a) {
      for (int i = s - ma; i >= 0; i--) {
        if (!f[i])
          continue;
        int ts = i + ma;
        for (int j = 0; j < n; j++) {
          if (dp[i][j].m_v) {
            dp[ts][j + 1] += dp[i][j];
            f[ts] = true;
          }
        }
      }
    }
    mod_ll ans;
    mod_ll u(1);
    for (int i = n; i >= 1; i--) {
      ans += u * dp[s][i];
      u *= 2;
    }
    printf("%lld\\n", ans());
  }
  return 0;
}