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

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

#ifndef ONLINE_JUDGE
#define debarr(a, x, y)                                                        \\
  cerr << #a << ":";                                                           \\
  for (int i = x; i <= y; i++)                                                 \\
    cerr << a[i] << " ";                                                       \\
  cerr << endl;
#define debmat(mat, row, col)                                                  \\
  cerr << #mat << ":\\n";                                                       \\
  for (int i = 0; i < row; i++) {                                              \\
    for (int j = 0; j < col; j++)                                              \\
      cerr << mat[i][j] << " ";                                                \\
    cerr << endl;                                                              \\
  }
#define deb(...) dbs(#__VA_ARGS__, __VA_ARGS__)
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << "(" << p.first << "," << p.second << ")";
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &p) {
  os << "[";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class T> ostream &operator<<(ostream &os, const set<T> &p) {
  os << "[";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class T> ostream &operator<<(ostream &os, const multiset<T> &p) {
  os << "[";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class S, class T>
ostream &operator<<(ostream &os, const map<S, T> &p) {
  os << "[";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class T> void dbs(string str, T t) {
  cerr << str << ":" << t << "\\n";
}
template <class T, class... S> void dbs(string str, T t, S... s) {
  int idx = str.find(',');
  cerr << str.substr(0, idx) << ":" << t << ",";
  dbs(str.substr(idx + 1), s...);
}
#else
#define deb(...)                                                               \\
  {}
#define debarr(a, x, y)                                                        \\
  {}
#define debmat(mat, row, col)                                                  \\
  {}
#endif
#define pr(x)                                                                  \\
  {                                                                            \\
    cout << x << "\\n";                                                         \\
    return;                                                                    \\
  }
template <class T> bool umin(T &a, T b) {
  return a > b ? (a = b, true) : false;
}
template <class T> bool umax(T &a, T b) {
  return a < b ? (a = b, true) : false;
}

#define int long long
#define ld long double
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, n) for (int i = 0; i < n; i++)
#define re(i, n) for (int i = 1; i <= n; i++)
#define fr(i, a, b) for (int i = a; i <= b; i++)
#define fb(i, a, b) for (int i = a; i >= b; i--)
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define ini(x, y) memset(x, y, sizeof(x))
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef map<int, int> mii;
typedef set<int> si;
const int N = 2e5 + 10;
const int mod = 1000 * 1000 * 1000 + 7;
// const int mod = 998244353;
int mpow(int a, int l) {
  int res = 1;
  while (l) {
    if (l & 1)
      res = res * 1LL * a % mod;
    l /= 2;
    a = a * 1LL * a % mod;
  }
  return res;
}
int minv(int a) { return mpow(a, mod - 2); }
void add(int &a, int b) { a = ((a % mod) + (b % mod)) % mod; }
void sub(int &a, int b) { a = (a - b + mod) % mod; }
int mul(int a, int b) { return ((a % mod) * 1LL * (b % mod)) % mod; }
void division(int &a, int b) { a = mul(a, mpow(b, mod - 2)); }
void solve(int tc) {
  int s;
  cin >> s;
  if (s < 3)
    pr(0);
  vi dp(s + 1, 0);
  dp[0] = 1;
  for (int i = 3; i <= s; i++) {
    for (int j = 3; j <= i; j++) {
      add(dp[i], dp[i - j]);
    }
  }
  cout << dp[s] << endl;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin >> t;
  for (int i = 1; i <= t; i++)
    solve(i);
  return 0;
}

Error: TLE

Fixed submission:

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

#ifndef ONLINE_JUDGE
#define debarr(a, x, y)                                                        \\
  cerr << #a << ":";                                                           \\
  for (int i = x; i <= y; i++)                                                 \\
    cerr << a[i] << " ";                                                       \\
  cerr << endl;
#define debmat(mat, row, col)                                                  \\
  cerr << #mat << ":\\n";                                                       \\
  for (int i = 0; i < row; i++) {                                              \\
    for (int j = 0; j < col; j++)                                              \\
      cerr << mat[i][j] << " ";                                                \\
    cerr << endl;                                                              \\
  }
#define deb(...) dbs(#__VA_ARGS__, __VA_ARGS__)
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << "(" << p.first << "," << p.second << ")";
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &p) {
  os << "[";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class T> ostream &operator<<(ostream &os, const set<T> &p) {
  os << "[";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class T> ostream &operator<<(ostream &os, const multiset<T> &p) {
  os << "[";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class S, class T>
ostream &operator<<(ostream &os, const map<S, T> &p) {
  os << "[";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class T> void dbs(string str, T t) {
  cerr << str << ":" << t << "\\n";
}
template <class T, class... S> void dbs(string str, T t, S... s) {
  int idx = str.find(',');
  cerr << str.substr(0, idx) << ":" << t << ",";
  dbs(str.substr(idx + 1), s...);
}
#else
#define deb(...)                                                               \\
  {}
#define debarr(a, x, y)                                                        \\
  {}
#define debmat(mat, row, col)                                                  \\
  {}
#endif
#define pr(x)                                                                  \\
  {                                                                            \\
    cout << x << "\\n";                                                         \\
    return;                                                                    \\
  }
template <class T> bool umin(T &a, T b) {
  return a > b ? (a = b, true) : false;
}
template <class T> bool umax(T &a, T b) {
  return a < b ? (a = b, true) : false;
}

#define int long long
#define ld long double
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, n) for (int i = 0; i < n; i++)
#define re(i, n) for (int i = 1; i <= n; i++)
#define fr(i, a, b) for (int i = a; i <= b; i++)
#define fb(i, a, b) for (int i = a; i >= b; i--)
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define ini(x, y) memset(x, y, sizeof(x))
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef map<int, int> mii;
typedef set<int> si;
const int N = 2e5 + 10;
const int mod = 1000 * 1000 * 1000 + 7;
// const int mod = 998244353;
int mpow(int a, int l) {
  int res = 1;
  while (l) {
    if (l & 1)
      res = res * 1LL * a % mod;
    l /= 2;
    a = a * 1LL * a % mod;
  }
  return res;
}
int minv(int a) { return mpow(a, mod - 2); }
void add(int &a, int b) { a = ((a % mod) + (b % mod)) % mod; }
void sub(int &a, int b) { a = (a - b + mod) % mod; }
int mul(int a, int b) { return ((a % mod) * 1LL * (b % mod)) % mod; }
int cache[700][2001];
int dp(int len, int sum) {
  if (sum == 0)
    return 1;
  if (sum < 3)
    return 0;
  int &ans = cache[len][sum];
  if (ans != -1)
    return ans;
  ans = 0;
  for (int i = 3; i <= sum; i++)
    add(ans, dp(len + 1, sum - i));
  return ans;
}
void solve(int tc) {
  ini(cache, -1);
  int n;
  cin >> n;
  cout << dp(1, n) << endl;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin >> t;
  for (int i = 1; i <= t; i++)
    solve(i);
  return 0;
}