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

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

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define INF 10000000000
#define MOD 1000000007
using ll = long long;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
using Graph = vector<vector<int>>;

int s;
vector<ll> dp;

ll f(int a) {
  if (dp[a] != -1)
    return dp[a];
  if (a < 3)
    return dp[a] = 0;
  if (a < 6)
    return dp[a] = 1;
  ll res = 0;
  res += f(a - 1);
  res %= MOD;
  res += f(a - 3);
  res %= MOD;
  return dp[a] = res;
}

int main() {
  cin >> s;
  dp.resize(s + 10);
  rep(i, s + 10) dp[i] = -1;
  cout << f(s) << endl;
}


Error: TLE

Fixed submission:

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

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define INF 10000000000
using ll = long long;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
using Graph = vector<vector<int>>;

const int MOD = 1000000007;

// modint: mod 計算を int を扱うように扱える構造体
template <int MOD> struct Fp {
  long long val;
  constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
    if (val < 0)
      val += MOD;
  }
  constexpr int getmod() { return MOD; }
  constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; }
  constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; }
  constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; }
  constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; }
  constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; }
  constexpr Fp &operator+=(const Fp &r) noexcept {
    val += r.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }
  constexpr Fp &operator-=(const Fp &r) noexcept {
    val -= r.val;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr Fp &operator*=(const Fp &r) noexcept {
    val = val * r.val % MOD;
    return *this;
  }
  constexpr Fp &operator/=(const Fp &r) noexcept {
    long long a = r.val, b = MOD, u = 1, v = 0;
    while (b) {
      long long t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr bool operator==(const Fp &r) const noexcept {
    return this->val == r.val;
  }
  constexpr bool operator!=(const Fp &r) const noexcept {
    return this->val != r.val;
  }
  friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept {
    return os << x.val;
  }
  friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
    if (n == 0)
      return 1;
    auto t = modpow(a, n / 2);
    t = t * t;
    if (n & 1)
      t = t * a;
    return t;
  }
};

using mint = Fp<MOD>;
const int MAX = 510000;

mint dp[2005][680];
int s;

mint f(int a, int l) {
  if (dp[a][l] != -1)
    return dp[a][l];
  if (l * 3 > a)
    return dp[a][l] = 0;
  if (l * 3 == a)
    return dp[a][l] = 1;
  if (l == 1)
    return dp[a][l] = 1;
  mint res = 0;
  for (int i = 3; a - i >= 3; i++) {
    res += f(a - i, l - 1);
  }
  return dp[a][l] = res;
}

int main() {
  cin >> s;
  rep(i, 2005) rep(j, 680) dp[i][j] = -1;
  mint res = 0;
  // rep1(j,s/3)
  for (int j = s / 3; j > 0; j--) {
    mint tmp = f(s, j);
    res += tmp;
    if (tmp == 0)
      break;
  }
  cout << res << endl;
}