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

#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ll long long
#define MOD 1000000007
#define ull unsigned long long int
#define db double
#define fi first
#define se second
#define pb push_back
#define vec vector<ll>
#define pr pair<ll, ll>
#define pq priority_queue<ll, vector<ll>, compare_queue>
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define repn(i, n) for (ll i = 0; i < n; i++)
#define all(a) (a).begin(), (a).end()
#define sz(x) (ll) x.size()
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define GS(s)                                                                  \\
  string s;                                                                    \\
  getline(cin, s);
#define lll __int128_t
#define endl "\\n"
#define Next cout.flush()

struct compare_queue {
  bool operator()(const ll &a, const ll &b) { return a > b; }
};

ll gcd(ll u, ll v) {
  if (v == 0)
    return u;

  else
    return gcd(v, u % v);
}

ll power(ll x, ll y) {
  if (y == 0)
    return 1;
  ll p = power(x, y / 2) % MOD;
  p = ((p % MOD) * (p % MOD)) % MOD;

  return (y % 2 == 0) ? p % MOD : ((x % MOD) * (p % MOD)) % MOD;
}

// ll power(ll x,ll y){
//     ll i;
//     ll ans=1;
//     for(i=0;i<y;i++){
//         ans=ans*x;
//     }
//     return ans;
// }

// ll modInverse(int a, int m)
// {
//     if(a==1){return 1;}
//     return power(a, m-2, m);
// }

// // // #define MAX 1000000
// int primes[100009],cnt=0;
// // vector<int> factors[1000009];
// char str[1000009];
// void pre()
// {
//     // calcualting primes
//     int n=100000;
//     for(int i=2; i*i<=n; i++)
//         if(str[i]==0)
//             for(int j=i; j*i<=n; j++)
//                 str[i*j]=1;
//     for(int i=2; i<=n; i++)
//         if(str[i]==0)primes[cnt]=i,cnt++;
// }

// A O(log n) function returning primefactorization
// by dividing by smallest prime factor at every step
// vector<ll> getFactorization(ll x)
// {
//     vector<ll> ret;
//     while (x != 1)
//     {
//         ret.push_back(spf[x]);
//         x = x / spf[x];
//     }
//     return ret;
// }

// C:\\Users\\A\\AppData\\Local\\Packages\\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\\LocalState\\rootfs\\home\\lambda

// ll getPow(ll n,ll k){
//     double f=log((double)n);
//     double g=log((double)k);
//     f=f/g;
//     return (ll)f;
// }

vec f;

void fro() {
  vec g(2001, 1);
  for (ll i = 1; i <= 2000; i++) {
    g[i] = ((g[i - 1] % MOD) * (i % MOD)) % MOD;
  }
  f = g;
}

ll po(ll n, ll i) {
  if (i == 1) {
    return 1;
  }
  ll hi = f[n - 2 * i - 1] % MOD;
  ll lo = f[i - 1] % MOD;
  lo = ((lo % MOD) * (f[n - 3 * i] % MOD)) % MOD;
  lo = power(lo, MOD - 2) % MOD;

  hi = ((hi % MOD) * (lo % MOD)) % MOD;
  return hi;
}

void Function() {
  ll n;
  cin >> n;
  ll ans = 0, i;
  fro();
  for (i = 1; 3 * i <= n; i++) {
    ans = ((ans % MOD) + (po(n, i) % MOD)) % MOD;
  }

  cout << ans << endl;
}

int main() {
  fast;
  ll t = 1, i = 0, j = 0, k, h;
  // pre();
  // cin >> t;
  for (h = 1; h <= t; h++) {

    Function();
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ll long long
#define MOD 1000000007
#define ull unsigned long long int
#define db double
#define fi first
#define se second
#define pb push_back
#define vec vector<ll>
#define pr pair<ll, ll>
#define pq priority_queue<ll, vector<ll>, compare_queue>
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define repn(i, n) for (ll i = 0; i < n; i++)
#define all(a) (a).begin(), (a).end()
#define sz(x) (ll) x.size()
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define GS(s)                                                                  \\
  string s;                                                                    \\
  getline(cin, s);
#define lll __int128_t
#define endl "\\n"
#define Next cout.flush()

struct compare_queue {
  bool operator()(const ll &a, const ll &b) { return a > b; }
};

ll gcd(ll u, ll v) {
  if (v == 0)
    return u;

  else
    return gcd(v, u % v);
}

ll power(ll x, ll y) {
  if (y == 0)
    return 1;
  ll p = power(x, y / 2) % MOD;
  p = ((p % MOD) * (p % MOD)) % MOD;

  return (y % 2 == 0) ? p % MOD : ((x % MOD) * (p % MOD)) % MOD;
}

// ll power(ll x,ll y){
//     ll i;
//     ll ans=1;
//     for(i=0;i<y;i++){
//         ans=ans*x;
//     }
//     return ans;
// }

// ll modInverse(int a, int m)
// {
//     if(a==1){return 1;}
//     return power(a, m-2, m);
// }

// // // #define MAX 1000000
// int primes[100009],cnt=0;
// // vector<int> factors[1000009];
// char str[1000009];
// void pre()
// {
//     // calcualting primes
//     int n=100000;
//     for(int i=2; i*i<=n; i++)
//         if(str[i]==0)
//             for(int j=i; j*i<=n; j++)
//                 str[i*j]=1;
//     for(int i=2; i<=n; i++)
//         if(str[i]==0)primes[cnt]=i,cnt++;
// }

// A O(log n) function returning primefactorization
// by dividing by smallest prime factor at every step
// vector<ll> getFactorization(ll x)
// {
//     vector<ll> ret;
//     while (x != 1)
//     {
//         ret.push_back(spf[x]);
//         x = x / spf[x];
//     }
//     return ret;
// }

// C:\\Users\\A\\AppData\\Local\\Packages\\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\\LocalState\\rootfs\\home\\lambda

// ll getPow(ll n,ll k){
//     double f=log((double)n);
//     double g=log((double)k);
//     f=f/g;
//     return (ll)f;
// }

ll dp[1000][2001];

ll f(ll n, ll i) {

  if (n == 0) {
    return 1;
  }
  if (n == 1 || n == 2) {
    return 0;
  }

  if (dp[i][n] != -1) {
    return dp[i][n];
  }

  ll j, ans = 0;
  for (j = 3; j <= n; j++) {
    ans = ((ans % MOD) + ((f(n - j, i + 1)) % MOD)) % MOD;
  }

  return dp[i][n] = ans % MOD;
}

void Function() {
  ll n;
  cin >> n;
  memset(dp, -1, sizeof(dp));

  cout << f(n, 0) << endl;
}

int main() {
  fast;
  ll t = 1, i = 0, j = 0, k, h;
  // pre();
  // cin >> t;
  for (h = 1; h <= t; h++) {

    Function();
  }

  return 0;
}