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

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <algorithm>
#include <assert.h>
#include <cstdint>
#include <cstdlib>
#include <ctime>
#include <ctype.h>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <unordered_map>
#include <vector>
#define lld long long int
#define newline cout << endl
#define pb push_back
#define mp make_pair
lld shouldendl = 1;
#define se second
#define fi first
#define sfind(s, ele) (s).find((ele));
#define SPEED                                                                  \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define starttime                                                              \\
  chrono::time_point<chrono::system_clock> start, end;                         \\
  start = chrono::system_clock::now()
#define showtime                                                               \\
  end = chrono::system_clock::now();                                           \\
  chrono::duration<double> elapsed_seconds = end - start;                      \\
  time_t end_time = chrono::system_clock::to_time_t(end);                      \\
  cout << "elapsed time: " << elapsed_seconds.count() << "s\\n"
lld INF = (lld)(1e9 + 7);
lld MOD = (lld)(1e9 + 7);
#define fileopen                                                               \\
  freopen("input.txt", "rt", stdin);                                           \\
  freopen("output.txt", "wt", stdout);
using namespace std;
lld modInverse(lld a) {
  lld m = MOD, m0 = m, y = 0, x = 1;
  if (m == 1)
    return 0;
  while (a > 1) {
    lld q = a / m, t = m;
    m = a % m, a = t, t = y;
    y = x - q * y, x = t;
  }
  if (x < 0)
    x += m0;
  return x;
}
lld add(long long a, long long b) { return ((a % MOD) + (b % MOD)) % MOD; }
lld mul(long long a, long long b) { return ((a % MOD) * (b % MOD)) % MOD; }
lld sub(long long a, long long b) {
  lld ans = ((a % MOD - b % MOD)) % MOD;
  if (ans < 0)
    ans += MOD;
  return ans;
}
lld gcd(lld a, lld b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
void MAIN(lld i, lld t);
int get() {
  lld t;
  cin >> t;
  ;
  return t;
}
template <class T> int logd(T name) {
  cout << name << endl;
  return 0;
}
void test(int t) {
  for (lld i = 1; i <= t; i++) {
    MAIN(i, t);
    if (shouldendl)
      cout << endl;
  }
}
lld power(lld x, lld y) {
  lld res = 1;
  x = x % MOD;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % MOD;
    y = y >> 1;
    x = (x * x) % MOD;
  }
  return res;
}
template <typename T, typename U>
std::pair<T, U> operator+(const std::pair<T, U> &l, const std::pair<T, U> &r) {
  return {l.first + r.first, l.second + r.second};
}
#define all(v) (v).begin(), (v).end()
lld popcount(lld x) { return __builtin_popcountll(x); }
lld lcm(lld a, lld b) { return (a * b) / gcd(a, b); }
template <class T> bool input(vector<T> &v, lld n = -1) {
  assert(v.size() != 0);
  if (n == -1)
    n = v.size();
  for (auto &i : v)
    cin >> i;
  return 0;
}
template <class T> bool print(vector<T> v, lld l = 0, lld r = -1) {
  if (r == -1)
    r = v.size();
  for (int i = l; i < r; i++)
    cout << v[i] << " ";
  return 0;
}
// Observation, binary_search, dfs on strings, simplify the problem
// Observe and ELIMINATE
lld sum;
lld n;
lld dp[2001];
lld solve(lld temp) {
  if (temp > sum)
    return 0;
  if (temp == sum)
    return 1;
  if (dp[temp] != -1)
    return dp[temp];
  lld p = 0;
  for (int j = 3; j <= sum; j++) {
    p = add(p, solve(temp + j));
  }
  return dp[temp] = p;
}
void MAIN(lld current_test_case, lld total_test_cases) {

  cin >> sum;
  lld ans = 0;
  memset(dp, (lld)-1, sizeof(dp));
  ans = solve(0LL);

  cout << ans;
}
int main() {

  SPEED;
#ifndef ONLINE_JUDGE
  fileopen
#endif
      test(1);
  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <algorithm>
#include <assert.h>
#include <cstdint>
#include <cstdlib>
#include <ctime>
#include <ctype.h>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <unordered_map>
#include <vector>
#define lld long long int
#define newline cout << endl
#define pb push_back
#define mp make_pair
lld shouldendl = 1;
#define se second
#define fi first
#define sfind(s, ele) (s).find((ele));
#define SPEED                                                                  \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define starttime                                                              \\
  chrono::time_point<chrono::system_clock> start, end;                         \\
  start = chrono::system_clock::now()
#define showtime                                                               \\
  end = chrono::system_clock::now();                                           \\
  chrono::duration<double> elapsed_seconds = end - start;                      \\
  time_t end_time = chrono::system_clock::to_time_t(end);                      \\
  cout << "elapsed time: " << elapsed_seconds.count() << "s\\n"
lld INF = (lld)(1e9 + 7);
lld MOD = (lld)(1e9 + 7);
#define fileopen                                                               \\
  freopen("input.txt", "rt", stdin);                                           \\
  freopen("output.txt", "wt", stdout);
using namespace std;
lld modInverse(lld a) {
  lld m = MOD, m0 = m, y = 0, x = 1;
  if (m == 1)
    return 0;
  while (a > 1) {
    lld q = a / m, t = m;
    m = a % m, a = t, t = y;
    y = x - q * y, x = t;
  }
  if (x < 0)
    x += m0;
  return x;
}
lld add(long long a, long long b) { return ((a % MOD) + (b % MOD)) % MOD; }
lld mul(long long a, long long b) { return ((a % MOD) * (b % MOD)) % MOD; }
lld sub(long long a, long long b) {
  lld ans = ((a % MOD - b % MOD)) % MOD;
  if (ans < 0)
    ans += MOD;
  return ans;
}
lld gcd(lld a, lld b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
void MAIN(lld i, lld t);
int get() {
  lld t;
  cin >> t;
  ;
  return t;
}
template <class T> int logd(T name) {
  cout << name << endl;
  return 0;
}
void test(int t) {
  for (lld i = 1; i <= t; i++) {
    MAIN(i, t);
    if (shouldendl)
      cout << endl;
  }
}
lld power(lld x, lld y) {
  lld res = 1;
  x = x % MOD;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % MOD;
    y = y >> 1;
    x = (x * x) % MOD;
  }
  return res;
}
template <typename T, typename U>
std::pair<T, U> operator+(const std::pair<T, U> &l, const std::pair<T, U> &r) {
  return {l.first + r.first, l.second + r.second};
}
#define all(v) (v).begin(), (v).end()
lld popcount(lld x) { return __builtin_popcountll(x); }
lld lcm(lld a, lld b) { return (a * b) / gcd(a, b); }
template <class T> bool input(vector<T> &v, lld n = -1) {
  assert(v.size() != 0);
  if (n == -1)
    n = v.size();
  for (auto &i : v)
    cin >> i;
  return 0;
}
template <class T> bool print(vector<T> v, lld l = 0, lld r = -1) {
  if (r == -1)
    r = v.size();
  for (int i = l; i < r; i++)
    cout << v[i] << " ";
  return 0;
}
// Observation, binary_search, dfs on strings, simplify the problem
// Observe and ELIMINATE
lld sum;
lld n;
lld dp[2001][2001];
lld solve(lld j, lld temp) {
  if (temp > sum)
    return 0;
  if (j >= sum)
    return 0;
  if (temp == sum)
    return 1;

  if (dp[j][temp] != -1)
    return dp[j][temp];

  lld p = 0;
  for (lld i = 3; i <= sum; i++) {
    p = add(p, solve(j + 1, temp + i));
  }
  return dp[j][temp] = p;
}
void MAIN(lld current_test_case, lld total_test_cases) {

  cin >> sum;
  lld ans = 0;
  memset(dp, (lld)-1, sizeof(dp));
  ans += solve(0, 0);

  cout << ans;
}
int main() {

  SPEED;
#ifndef ONLINE_JUDGE
  fileopen
#endif
      test(1);
  return 0;
}