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


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define ff first
#define ss second
const ll mod = 998244353;
ll multiply(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
ll add(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
ll sub(ll a, ll b) { return ((a % mod) - (b % mod) + mod) % mod; }
////////////////////////////////////////////////////////////////
ll power(ll x, ll n) {
  if (n == 0)
    return 1;
  ll y = power(x, n / 2);
  if (n & 1)
    return multiply(y, multiply(y, x));
  return multiply(y, y);
}
///////////////////////////////////////////////////////////////
void solve() {
  int n, s;
  cin >> n >> s;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  //    vector < vector < map < int , int >  > > dp(n,vector<map < int , int >
  //    >(s+1)); dp[0][0][0] = 1 ; if(a[0]<=s)
  //        dp[0][a[0]][1] = 1 ;
  //    for( int i = 1 ; i < n ; i++){
  //        for( int j = 0 ; j <= s ; j++){
  //            if(j>=a[i]){
  //                for(auto x : dp[i-1][j-a[i]]){
  //                    dp[i][j][x.ff+1] = x.ss ;
  //                }
  //            }
  //            for(auto x : dp[i-1][j]){
  //                dp[i][j][x.ff] = add(dp[i][j][x.ff],x.ss) ;
  //            }
  //        }
  //    }
  vector<vector<int>> dp(n, vector<int>(s + 1));
  ll x = power(2, mod - 2);
  dp[0][0] = 1;
  if (a[0] <= s)
    dp[0][a[0]] = x;
  for (int i = 1; i < n; i++) {
    for (int j = 0; j <= s; j++) {
      if (j >= a[i]) {
        dp[i][j] = add(dp[i - 1][j], multiply(dp[i - 1][j - a[i]], x));
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }
  //    for( int i = 0 ; i < n ; i++){
  //        for( int j = 0 ; j <= s ; j++){
  //            cout << dp[i][j] << " " ;
  //        }
  //        cout << "\\n";
  //    }
  ll ans = 0;
  //    for(auto x : dp[n-1][s]){
  //        ans = add(ans,multiply(x.ss , power(2,n-x.ff)));
  //    }
  // cout << multiply(3,power(4,mod-2)) << " " ;
  cout << multiply(power(2, n), dp[n - 1][s]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  // cin >> t ;
  while (t--) {
    solve();
  }
}


Error: TLE

Fixed submission:


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define ff first
#define ss second
const ll mod = 998244353;
ll multiply(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
ll add(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
ll sub(ll a, ll b) { return ((a % mod) - (b % mod) + mod) % mod; }
////////////////////////////////////////////////////////////////
ll power(ll x, ll n) {
  if (n == 0)
    return 1;
  ll y = power(x, n / 2);
  if (n & 1)
    return multiply(y, multiply(y, x));
  return multiply(y, y);
}
///////////////////////////////////////////////////////////////
void solve() {
  int n, s;
  cin >> n >> s;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  //    vector < vector < map < int , int >  > > dp(n,vector<map < int , int >
  //    >(s+1)); dp[0][0][0] = 1 ; if(a[0]<=s)
  //        dp[0][a[0]][1] = 1 ;
  //    for( int i = 1 ; i < n ; i++){
  //        for( int j = 0 ; j <= s ; j++){
  //            if(j>=a[i]){
  //                for(auto x : dp[i-1][j-a[i]]){
  //                    dp[i][j][x.ff+1] = x.ss ;
  //                }
  //            }
  //            for(auto x : dp[i-1][j]){
  //                dp[i][j][x.ff] = add(dp[i][j][x.ff],x.ss) ;
  //            }
  //        }
  //    }
  vector<vector<int>> dp(n, vector<int>(s + 1));
  dp[0][0] = 1;
  if (a[0] <= s)
    dp[0][a[0]] = power(2, mod - 2);
  for (int i = 1; i < n; i++) {
    for (int j = 0; j <= s; j++) {
      if (j >= a[i]) {
        dp[i][j] =
            add(dp[i - 1][j], multiply(dp[i - 1][j - a[i]], power(2, mod - 2)));
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }
  //    for( int i = 0 ; i < n ; i++){
  //        for( int j = 0 ; j <= s ; j++){
  //            cout << dp[i][j] << " " ;
  //        }
  //        cout << "\\n";
  //    }
  ll ans = 0;
  //    for(auto x : dp[n-1][s]){
  //        ans = add(ans,multiply(x.ss , power(2,n-x.ff)));
  //    }
  // cout << multiply(3,power(4,mod-2)) << " " ;
  cout << multiply(power(2, n), dp[n - 1][s]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  // cin >> t ;
  while (t--) {
    solve();
  }
}