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

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
typedef long double ld;
#define S 8005
#define M 1000000007
#define nl cout << "\\n";
#define er cout << "\\n***********\\n";
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define test                                                                   \\
  lli t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define all(x) (x).begin(), (x).end()
#define sz(x) (lli)(x.size())
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define Y cout << "YES\\n";
#define N cout << "NO\\n";
#define print(x) cout << #x << ": " << x << endl;
#define print2(x, y)                                                           \\
  cout << #x << ": " << x << " | " << #y << ": " << y << endl;
#define print3(x, y, z)                                                        \\
  cout << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \\
       << z << endl;
#define print4(a, b, c, d)                                                     \\
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << endl;
#define print5(a, b, c, d, e)                                                  \\
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define print6(a, b, c, d, e, f)                                               \\
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \\
       << #f << ": " << f << endl;

lli s;
lli dp[S][S];
void solve() {
  cin >> s;
  if (s <= 2) {
    cout << 0;
    return;
  }
  lli len = (s / 3);

  for (lli j = 3; j <= s; j++) {
    dp[1][j] = 1;
  }
  for (lli i = 2; i <= len; i++) {
    for (lli j = 3; j <= s; j++) {
      dp[i][j] = (dp[i - 1][j - 3]) % M;
    }
    for (lli j = 3; j <= s; j++) {
      dp[i][j] = (dp[i][j] % M + dp[i][j - 1] % M) % M;
    }
  }

  lli ans = 0;

  for (lli i = 1; i <= len; i++) {
    ans = ans % M + (dp[i][s]) % M;
    ans = ans % M;
  }

  cout << ans;
}

int main() {
  fast
#ifndef ONLINE_JUDGE
      freopen("inputa.txt", "r", stdin);
  freopen("outputa.txt", "w", stdout);
#endif
  // test
  {
    solve();
    nl
  }
}
// a+b=a?b+2?(a&b)
//__builtin_popcnt(number)
// cout<<fixed<<setprecision(8)<<output<<"\\n";

Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
typedef long double ld;
#define S 8005
#define M 1000000007
#define nl cout << "\\n";
#define er cout << "\\n***********\\n";
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define test                                                                   \\
  lli t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define all(x) (x).begin(), (x).end()
#define sz(x) (lli)(x.size())
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define Y cout << "YES\\n";
#define N cout << "NO\\n";
#define print(x) cout << #x << ": " << x << endl;
#define print2(x, y)                                                           \\
  cout << #x << ": " << x << " | " << #y << ": " << y << endl;
#define print3(x, y, z)                                                        \\
  cout << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \\
       << z << endl;
#define print4(a, b, c, d)                                                     \\
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << endl;
#define print5(a, b, c, d, e)                                                  \\
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define print6(a, b, c, d, e, f)                                               \\
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \\
       << #f << ": " << f << endl;

lli s;
lli dp[S][S];
void solve() {
  cin >> s;
  if (s <= 2) {
    cout << 0;
    return;
  }
  lli len = (s / 3);

  for (lli j = 3; j <= s; j++) {
    dp[1][j] = 1;
  }
  for (lli i = 2; i <= len; i++) {
    for (lli j = 3; j <= s; j++) {
      lli tp = 0;
      for (lli k = 3; (j - k) >= 0; k++) {
        tp = tp % M + (dp[i - 1][j - k]) % M;
        tp = tp % M;
      }
      dp[i][j] = tp;
    }
  }

  lli ans = 0;

  for (lli i = 1; i <= len; i++) {
    ans = ans % M + (dp[i][s]) % M;
    ans = ans % M;
  }

  cout << ans;
}

int main() {
  fast
#ifndef ONLINE_JUDGE
      freopen("inputa.txt", "r", stdin);
  freopen("outputa.txt", "w", stdout);
#endif
  // test
  {
    solve();
    nl
  }
}
// a+b=a?b+2?(a&b)
//__builtin_popcnt(number)
// cout<<fixed<<setprecision(8)<<output<<"\\n";