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

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

#define ll long long
typedef pair<int, int> ii;
typedef pair<ll, int> lli;
#define ar array
#define pb push_back
#define fi first
#define se second
#define sz size
#define mp make_pair
#define nl '\\n'
#define all(x) (x).begin(), (x).end()
#define int_max numeric_limits<int>::max()
#define int_min numeric_limits<int>::min()
#define ll_max numeric_limits<ll>::max()
#define ll_min numeric_limits<ll>::min()

#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define im(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

// custom sort example
// sort(all(a), [ ]( const auto& c, const auto& d) {
// if (c.fi.se != d.fi.se) {
// return c.fi.se < d.fi.se; // lower first
//}
// if (c.fi.fi != d.fi.fi) {
// return c.fi.fi > d.fi.fi; // higher first
//}
// return c.se > d.se; // higher first
//});

struct chash {
  static uint64_t splitmix64(uint64_t x) {
    // http://xorshift.di.unimi.it/splitmix64.c
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};

template <class T> auto lbf(const std::set<std::pair<T, T>> &s, T first) {
  static constexpr T min = std::numeric_limits<T>::min();

  return s.lower_bound({first, min});
}

int MOD = 1000000007;

ll pw(ll n, ll k) {
  ll r = 1;
  for (; k; k >>= 1) {
    if (k & 1)
      r = r * n % MOD;
    n = n * n % MOD;
  }
  return r;
}
ll inv(ll n) { return pw(n, MOD - 2); }

// ll pom(ll a, ll b) {
// ll f = 1, t = 1;
// for (ll i = a; i > (a - b); i--) {
// f = (f * i) % MOD;
//}
// for (ll i = b; i > 0; i--) {
// t = (t * i) % MOD;
//}
// return ();
//}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif
  ll n, res = 0;
  cin >> n;
  if (n < 3) {
    cout << 0 << nl;
  } else {
    ll act = n / 3;
    ll todo = n - (act * 3);
    vector<vector<ll>> dp(act, vector<ll>(n + 1));
    for (int i = 0; i < act; i++) {
      ll pom = 0;
      for (int j = 0; j <= todo; j++) {
        if (i == 0) {
          dp[i][j] = 1;
        } else {
          pom = (pom + dp[i - 1][j]) % MOD;
          dp[i][j] = pom;
          // for (int k = 0; k <= j; k++) {
          // dp[i][j] = (dp[i][j] + dp[i - 1][k]) % MOD;
          //}
        }
      }
    }
    // debug() << im(act) im(todo) im(dp);
    res = (res + dp[act - 1][todo]) % MOD;
    // debug() << im(res);
    act--;
    ll prev = todo;
    todo = n - (act * 3);
    while (act >= 1) {
      // res = (res + pom(todo, act)) % MOD;

      for (int i = 0; i < act; i++) {
        ll pom;
        if (i != 0) {
          pom = dp[i][prev];
        } else {
          pom = 0;
        }
        for (int j = prev + 1; j <= todo; j++) {
          if (i == 0) {
            dp[i][j] = 1;
          } else {
            pom = (pom + dp[i - 1][j]) % MOD;
            dp[i][j] = pom;
            // for (int k = 0; k <= j; k++) {
            // dp[i][j] = (dp[i][j] + dp[i - 1][k]) % MOD;
            //}
          }
        }
      }
      // debug() << im(act) im(todo) im(dp);
      res = (res + dp[act - 1][todo]) % MOD;
      // debug() << im(res);
      act--;
      prev = todo;
      todo = n - (act * 3);
    }
    cout << res << nl;
  }
}


Error: TLE

Fixed submission:

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

#define ll long long
typedef pair<int, int> ii;
typedef pair<ll, int> lli;
#define ar array
#define pb push_back
#define fi first
#define se second
#define sz size
#define mp make_pair
#define nl '\\n'
#define all(x) (x).begin(), (x).end()
#define int_max numeric_limits<int>::max()
#define int_min numeric_limits<int>::min()
#define ll_max numeric_limits<ll>::max()
#define ll_min numeric_limits<ll>::min()

#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define im(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

// custom sort example
// sort(all(a), [ ]( const auto& c, const auto& d) {
// if (c.fi.se != d.fi.se) {
// return c.fi.se < d.fi.se; // lower first
//}
// if (c.fi.fi != d.fi.fi) {
// return c.fi.fi > d.fi.fi; // higher first
//}
// return c.se > d.se; // higher first
//});

struct chash {
  static uint64_t splitmix64(uint64_t x) {
    // http://xorshift.di.unimi.it/splitmix64.c
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};

template <class T> auto lbf(const std::set<std::pair<T, T>> &s, T first) {
  static constexpr T min = std::numeric_limits<T>::min();

  return s.lower_bound({first, min});
}

int MOD = 1000000007;

ll pw(ll n, ll k) {
  ll r = 1;
  for (; k; k >>= 1) {
    if (k & 1)
      r = r * n % MOD;
    n = n * n % MOD;
  }
  return r;
}
ll inv(ll n) { return pw(n, MOD - 2); }

// ll pom(ll a, ll b) {
// ll f = 1, t = 1;
// for (ll i = a; i > (a - b); i--) {
// f = (f * i) % MOD;
//}
// for (ll i = b; i > 0; i--) {
// t = (t * i) % MOD;
//}
// return ();
//}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif
  ll n, res = 0;
  cin >> n;
  if (n < 3) {
    cout << 0 << nl;
  } else {
    ll act = n / 3;
    ll todo = n - (act * 3);
    while (act >= 1) {
      // res = (res + pom(todo, act)) % MOD;
      vector<vector<ll>> dp(act, vector<ll>(todo + 1));
      for (int i = 0; i < act; i++) {
        ll pom = 0;
        for (int j = 0; j <= todo; j++) {
          if (i == 0) {
            dp[i][j] = 1;
          } else {
            pom = (pom + dp[i - 1][j]) % MOD;
            dp[i][j] = pom;
            // for (int k = 0; k <= j; k++) {
            // dp[i][j] = (dp[i][j] + dp[i - 1][k]) % MOD;
            //}
          }
        }
      }
      // debug() << im(act) im(todo) im(dp);
      res = (res + dp[act - 1][todo]) % MOD;
      act--;
      todo = n - (act * 3);
    }
    cout << res << nl;
  }
}