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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define ocut cout
#define ouct cout
#define itn int

struct Union {
  vector<ll> par;
  Union(ll a) { par = vector<ll>(a, -1); }
  ll find(ll a) {
    if (par[a] < 0) {
      return a;
    } else {
      return par[a] = find(par[a]);
    }
  }
  bool same(ll a, ll b) { return (find(a) == find(b)); }
  ll size(ll a) { return -par[find(a)]; }
  void unite(ll a, ll b) {
    ll c = find(a), d = find(b);
    if (c == d)
      return;
    if (size(c) < size(d)) {
      swap(c, d);
    }
    par[c] += par[d];
    par[d] = c;
  }
};
ll euclidean_gcd(ll a, ll b) {
  if (a < b)
    return euclidean_gcd(b, a);
  ll r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}
ll euclidean_lcm(ll a, ll b) { return a / euclidean_gcd(a, b) * b; }

ll binary(ll bina) {
  ll ans = 0;
  for (ll i = 0; bina > 0; i++) {
    ans += bina % 2;
    bina = bina / 2;
  }
  return ans;
}
void revStr(char *str) {
  int size = strlen(str);
  int i, j;
  char tmp = {0};

  for (i = 0, j = size - 1; i < size / 2; i++, j--) {
    tmp = str[i];
    str[i] = str[j];
    str[j] = tmp;
  }
  return;
}

const int MAX = 510000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
int main(void) {
  COMinit();
  ll s;
  cin >> s;
  ll d, ans = 0;
  for (ll i = 1; i < s / 3 + 1; i++) {
    d = s - i * 3;
    ans = (ans + COM(i + d - 1, d) % 1000000007) % 1000000007;
  }
  cout << ans;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define ocut cout
#define ouct cout
#define itn int

struct Union {
  vector<ll> par;
  Union(ll a) { par = vector<ll>(a, -1); }
  ll find(ll a) {
    if (par[a] < 0) {
      return a;
    } else {
      return par[a] = find(par[a]);
    }
  }
  bool same(ll a, ll b) { return (find(a) == find(b)); }
  ll size(ll a) { return -par[find(a)]; }
  void unite(ll a, ll b) {
    ll c = find(a), d = find(b);
    if (c == d)
      return;
    if (size(c) < size(d)) {
      swap(c, d);
    }
    par[c] += par[d];
    par[d] = c;
  }
};
ll euclidean_gcd(ll a, ll b) {
  if (a < b)
    return euclidean_gcd(b, a);
  ll r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}
ll euclidean_lcm(ll a, ll b) { return a / euclidean_gcd(a, b) * b; }

ll binary(ll bina) {
  ll ans = 0;
  for (ll i = 0; bina > 0; i++) {
    ans += bina % 2;
    bina = bina / 2;
  }
  return ans;
}
void revStr(char *str) {
  int size = strlen(str);
  int i, j;
  char tmp = {0};

  for (i = 0, j = size - 1; i < size / 2; i++, j--) {
    tmp = str[i];
    str[i] = str[j];
    str[j] = tmp;
  }
  return;
}
ll comb(ll n, ll r) {
  std::vector<std::vector<long long>> v(n + 1,
                                        std::vector<long long>(n + 1, 0));
  for (ll i = 0; i < v.size(); i++) {
    v[i][0] = 1;
    v[i][i] = 1;
  }
  for (ll j = 1; j < v.size(); j++) {
    for (ll k = 1; k < j; k++) {
      v[j][k] = (v[j - 1][k - 1] + v[j - 1][k]) % 1000000007;
    }
  }
  return v[n][r];
}
int main(void) {
  ll s;
  cin >> s;
  ll d, ans = 0;
  for (ll i = 1; i < s / 3 + 1; i++) {
    d = s - i * 3;
    ans = (ans + comb(i + d - 1, d) % 1000000007) % 1000000007;
  }
  cout << ans;
}