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

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

#define mp make_pair
#define pb push_back
#define ll long long int
#define sd(x) scanf("%lld", &x)
#define inf 1e18 + 9
#define pll pair<ll, ll>
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define bits(x) __builtin_popcountll(x)
#define ld long double
#define test()                                                                 \\
  ll test;                                                                     \\
  cin >> test;                                                                 \\
  while (test--)
#define fi first
#define se second

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

inline ll GCD(ll x, ll y) {
  if (x < y)
    swap(x, y);
  if (x == 0)
    return y;
  if (y == 0)
    return x;
  return GCD(x % y, y);
}

ll phi(ll n) {
  ll result = n;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      while (n % i == 0)
        n /= i;
      result -= result / i;
    }
  }
  if (n > 1)
    result -= result / n;
  return result;
}

ll power(ll x, ll n, ll mod) {
  ll res = 1;
  x = (x + mod) % mod;
  while (n) {
    if (n & 1) {
      res = ((res * x) % mod + mod) % mod;
    }
    x = ((x * x) % mod + mod) % mod;
    n >>= 1;
  }

  return res;
}

const ll MOD = 1e9 + 7;
const int MAXN = 2005;

inline ll add(ll x, ll y) {
  x = (x + MOD) % MOD;
  y = (y + MOD) % MOD;
  ll ans = (x + y + MOD) % MOD;
  return ans;
}

ll dp[MAXN][MAXN] = {0};
ll S;

ll dp2[MAXN] = {0};

void clear() {
  for (ll i = 0; i < MAXN; i++) {
    for (ll j = 0; j < MAXN; j++) {
      dp[i][j] = 0;
    }
  }

  for (ll i = 3; i < MAXN; i++) {
    dp[1][i] = 1;
  }
}

ll solve(ll indx, ll sum) {
  if (sum == 0)
    return 1;
  if (sum <= 2 && sum >= 1) {
    return 0;
  }

  if (dp[indx][sum] != -1)
    return dp[indx][sum];

  ll ans = 0;

  for (ll i = 3; i <= S; i++) {
    if (sum - i >= 0) {
      ans = (ans + solve(indx + 1, sum - i)) % MOD;
    } else
      break;
  }

  return dp[indx][sum] = ans;
}

inline ll mul(ll x, ll y) {
  x %= MOD;
  y %= MOD;
  ll ans = ((x * y) % MOD + MOD) % MOD;
  return ans;
}

int main() {
  fastio;

  // ll S;
  cin >> S;
  clear();

  if (S <= 2) {
    cout << 0 << endl;
    return 0;
  }

  dp2[0] = 1;
  dp2[3] = 1;

  for (ll i = 4; i <= S; i++) {
    for (ll k = 3; k <= S; k++) {
      if (i - k >= 0)
        dp2[i] = (dp2[i] + dp2[i - k]) % MOD;
      else
        break;
    }
  }

  cout << dp2[S] << endl;

  // ll ans = solve(1,S);

  // ll ans = 0;

  // for(ll i=2;(i*3)<=S;i++) {
  //     for(ll j=3;j<=S;j++) {
  //         for(ll k=3;k<=S;k++) {
  //             if(j-k>0) {
  //                 dp[i][j] = (dp[i][j]+dp[i-1][j-k])%MOD;
  //             } else break;
  //         }
  //     }
  // }

  // for(ll i=1;i*3<=S;i++) {
  //     ans = (ans+dp[i][S])%MOD;
  // }

  // cout<<ans<<endl;
}


Error: TLE

Fixed submission:

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

#define mp make_pair
#define pb push_back
#define ll long long int
#define sd(x) scanf("%lld", &x)
#define inf 1e18 + 9
#define pll pair<ll, ll>
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define bits(x) __builtin_popcountll(x)
#define ld long double
#define test()                                                                 \\
  ll test;                                                                     \\
  cin >> test;                                                                 \\
  while (test--)
#define fi first
#define se second

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

inline ll GCD(ll x, ll y) {
  if (x < y)
    swap(x, y);
  if (x == 0)
    return y;
  if (y == 0)
    return x;
  return GCD(x % y, y);
}

ll phi(ll n) {
  ll result = n;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      while (n % i == 0)
        n /= i;
      result -= result / i;
    }
  }
  if (n > 1)
    result -= result / n;
  return result;
}

ll power(ll x, ll n, ll mod) {
  ll res = 1;
  x = (x + mod) % mod;
  while (n) {
    if (n & 1) {
      res = ((res * x) % mod + mod) % mod;
    }
    x = ((x * x) % mod + mod) % mod;
    n >>= 1;
  }

  return res;
}

const ll MOD = 1e9 + 7;
const int MAXN = 2005;

inline ll add(ll x, ll y) {
  x = (x + MOD) % MOD;
  y = (y + MOD) % MOD;
  ll ans = (x + y + MOD) % MOD;
  return ans;
}

ll dp[MAXN][MAXN] = {0};
ll S;

void clear() {
  for (ll i = 0; i < MAXN; i++) {
    for (ll j = 0; j < MAXN; j++) {
      dp[i][j] = -1;
    }
  }
}

ll solve(ll indx, ll sum) {
  if (sum == 0)
    return 1;
  if (sum <= 2 && sum >= 1) {
    return 0;
  }

  if (dp[indx][sum] != -1)
    return dp[indx][sum];

  ll ans = 0;

  for (ll i = 3; i <= S; i++) {
    if (sum - i >= 0) {
      ans = (ans + solve(indx + 1, sum - i)) % MOD;
    } else
      break;
  }

  return dp[indx][sum] = ans;
}

int main() {
  fastio;

  // ll S;
  cin >> S;
  clear();

  ll ans = solve(1, S);

  cout << ans << endl;
}