alexjercan/bugnet huggingface dataset:
Problem p02555 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll M = 1e9 + 7;
vector<ll> dp;
int main() {
  ll n;
  cin >> n;
  dp.resize(n + 1);
  for (ll i = 3; i <= n; i++) {
    dp[i] += 1;
    for (ll j = 3; j <= i - 3; j++) {
      dp[i] += dp[j];
      dp[i] %= M;
    }
  }
  cout << dp[n] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll M = 1e9 + 7;

unordered_map<ll, ll> f;
unordered_map<string, ll> dp;

ll fact(ll n) {
  if (n == 0)
    return 1;
  if (f.find(n) != f.end())
    return f[n];

  return f[n] = (n * fact(n - 1)) % M;
}

ll mmi(ll n, ll b = M - 2) {
  if (b == 0)
    return 1;
  ll ans = mmi(n, b / 2);
  ans = (ans * ans) % M;
  if (b & 1)
    ans = (ans * n) % M;
  return ans;
}

ll find(ll n, unordered_map<ll, ll> &map, ll dig = 1) {
  if (n < 3)
    return 0;
  map[n]++;
  ll ans = 1, count = 0;
  string state;
  state += to_string(n) + " " + to_string(dig);
  for (auto p : map) {
    if (p.second)
      ans *= fact(p.second);
    ans %= M;
  }
  count += fact(dig) * mmi(ans);
  state += " " + to_string(ans);
  if (dp.find(state) != dp.end())
    return dp[state];

  count %= M;
  ll half = n / 2;
  for (ll i = 3; i <= half; i++) {
    map[i]++;
    count += find(n - i, map, dig + 1);
    count %= M;
    map[i]--;
  }
  map[n]--;
  return dp[state] = count;
}

int main() {
  ll n;
  cin >> n;
  ll ans = 0;
  unordered_map<ll, ll> map;
  ans += find(n, map);
  cout << ans << endl;
  return 0;
}
Problem p02555 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;
const int MOD = 1e9 + 7;
// DP
vector<long long> memo;

long long dp(int S) {
  if (memo[S] != -1)
    return memo[S];
  if (S == 0 || S == 3)
    return memo[S] = 1;
  if (S < 3)
    return memo[S] = 0;

  long long res = 0;
  for (long long i = 3; i <= S; i++) {
    res = (res + dp(S - i)) % MOD;
  }
  return memo[S] = res;
}
int main() {
  int S;
  cin >> S;
  memo.resize(S + 1, -1LL);
  cout << dp(S) << '\\n';
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;
const int MOD = 1e9 + 7;
// DP
vector<long long> memo;

long long dp(int S) {
  if (memo[S] != -1)
    return memo[S];
  if (S < 3)
    return memo[S] = 0;
  if (S == 0)
    return memo[S] = 1;

  long long res = 0;
  for (long long i = 3; i <= S; i++) {
    res = (res + dp(S - i)) % MOD;
  }
  return memo[S] = res;
}
int main() {
  int S;
  cin >> S;
  memo.resize(S + 1, -1);
  memo[0] = memo[3] = 1;
  memo[2] = memo[1] = 0;
  cout << dp(S) << '\\n';
  return 0;
}
Problem p02555 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int mod = 1e9 + 7;
ll dp[2007];
int N;
ll go(int n) {
  if (n == 1 || n == 2)
    return 0;
  if (dp[n] != -1)
    return dp[n];
  return dp[n] = (go(n - 1) + go(n - 3)) % mod;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0);

  cin >> N;
  memset(dp, -1, sizeof dp);
  dp[0] = 1;
  cout << go(N) % mod << " ";
}


Error: TLE

Fixed submission:

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

const int mod = 1e9 + 7;
ll go(int n) {
  if (n == 0)
    return 1;
  if (n == 1 || n == 2)
    return 0;
  return go(n - 1) + go(n - 3) % mod;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0);

  int n;
  cin >> n;
  cout << go(n) << " ";
}
Problem p02555 (C++) — buggy submission, verdict: Runtime Error


#include <algorithm>
#include <iostream>

int main() {
  int64_t s[2001];
  for (int64_t n = 0; n < 2001; ++n) {
    s[n] = 1;
  }

  s[0] = s[1] = s[2] = 0;

  int64_t S;
  std::cin >> S;

  for (int64_t i = 4; i < 2001; ++i) {
    for (int64_t j = 0; j <= i - 3; ++j) {
      if (i == j) {
        s[i] += 1;
      } else {
        s[i] += s[j];
      }
      s[i] %= 1000000007LL;
    }
  }

  std::cout << s[S] << std::endl;

  return 0;
}


Error: -6
*** stack smashing detected ***: terminated


Fixed submission:


#include <algorithm>
#include <iostream>

int main() {
  int64_t s[2001];
  for (int64_t n = 0; n < 2001; ++n) {
    s[n] = 1;
  }

  s[0] = s[1] = s[2] = 0;

  int64_t S;
  std::cin >> S;

  for (int64_t i = 4; i <= 2001; ++i) {
    for (int64_t j = 3; j < i; ++j) {
      s[i] += s[j] * s[i - j];
      s[i] %= 1000000007;
    }
  }

  int64_t ans = s[S];
  if (ans < 0) {
    ans += 1000000007;
  }
  std::cout << ans << std::endl;

  return 0;
}
Problem p02555 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define ALL(x) (x).begin(), (x).end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))

typedef pair<int, int> PI;
typedef pair<int, pair<int, int>> PII;
static const int INF = 1010000000000000017LL;
static const double eps = 1e-12;
static const double pi = 3.14159265358979323846;
static const int dx[4] = {1, -1, 0, 0};
static const int dy[4] = {0, 0, 1, -1};
static const int ddx[8] = {1, -1, 0, 0, 1, 1, -1, -1};
static const int ddy[8] = {0, 0, 1, -1, 1, -1, 1, -1};

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

int mod = 1000000007;

int S;
int dp[5005];

signed main() {
  cin >> S;
  for (int i = 3; i <= 2000; ++i)
    dp[i] = 1;
  for (int s = 3; s <= S; ++s) {
    for (int i = 3; i <= 2000; ++i) {
      dp[s + i] += dp[s];
      dp[s + i] %= mod;
    }
  }
  cout << dp[S] % mod << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define ALL(x) (x).begin(), (x).end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))

typedef pair<int, int> PI;
typedef pair<int, pair<int, int>> PII;
static const int INF = 1010000000000000017LL;
static const double eps = 1e-12;
static const double pi = 3.14159265358979323846;
static const int dx[4] = {1, -1, 0, 0};
static const int dy[4] = {0, 0, 1, -1};
static const int ddx[8] = {1, -1, 0, 0, 1, 1, -1, -1};
static const int ddy[8] = {0, 0, 1, -1, 1, -1, 1, -1};

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

int mod = 1000000007;

int S;
int mem[2005][2005];

void init() {
  for (int i = 0; i < 2005; ++i)
    for (int j = 0; j < 2005; ++j)
      mem[i][j] = -1;
}

int calc(int n, int s) {
  if (mem[n][s] != -1)
    return mem[n][s];
  // cout<<s<<endl;
  if (s >= S) {
    if (s == S)
      return 1;
    else
      return 0;
  }
  int ret = 0;
  for (int i = 3; i <= S - s; ++i) {
    ret += calc(n + 1, s + i);
    ret %= mod;
  }
  return mem[n][s] = ret % mod;
}

signed main() {
  init();
  cin >> S;
  cout << calc(0, 0) << endl;
}
Problem p02555 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
// #include <bits/stdc++.h>
using namespace std;

const long int INF = 1000000007;

int main() {
  int S;
  cin >> S;
  long long a[S + 1], sum[S + 1];
  if (S <= 2) {
    cout << 0 << endl;
    return 0;
  }
  if (S <= 5) {
    cout << 1 << endl;
    return 0;
  }
  a[0] = 0;
  a[1] = 0;
  a[2] = 0;
  a[3] = 1;
  a[4] = 1;
  a[5] = 1;
  sum[0] = 0;
  sum[1] = 0;
  sum[2] = 0;
  for (int i = 3; i <= S - 3; i++) {
    sum[i] = sum[i - 1] + a[i];
    sum[i] = sum[i] % INF;
    a[i + 3] = 1 + sum[i];
    a[i + 3] = a[i + 3] % INF;
  }
  cout << a[S] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
// #include <bits/stdc++.h>
using namespace std;

const long int INF = 1000000007;

int main() {
  int S;
  cin >> S;
  long long a[S + 1], sum[S + 1];
  if (S <= 2) {
    cout << 0 << endl;
    return 0;
  }
  a[0] = 0;
  a[1] = 0;
  a[2] = 0;
  a[3] = 1;
  a[4] = 1;
  a[5] = 1;
  sum[2] = 0;
  for (int i = 3; i <= S - 3; i++) {
    sum[i] = sum[i - 1] + a[i];
    sum[i] = sum[i] % INF;
    a[i + 3] = 1 + sum[i];
    a[i + 3] = a[i + 3] % INF;
  }
  cout << a[S] << endl;
  return 0;
}
Problem p02555 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
typedef long long ll;
#define ALL(l) (l).begin(), (l).end()
#define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
using namespace std;

// const ll mod =  1000000007;
// const ll maxn = 1000000000;

//---------------------------------------------------------------------------------------------------
using vi = vector<int>; // intの1次元の型に vi という別名をつける
using vll = vector<ll>; // long longの1次元の型に vll という別名をつける
using vvll = vector<vll>; // long longの2次元の型に vvll という別名をつける
using vs = vector<string>; // stringの1次元の型に vs という別名をつける
using pll =
    pair<ll, ll>; // これ以降 pll という型名はpair<ll, ll> と同じ意味で使える
//---------------------------------------------------------------------------------------------------

int main() {
  vvll dp(2001, vll(2001, 0)); // 桁数、合計
  ll s;
  cin >> s;

  dp.at(0).at(0) = 1;
  rep2(i, 1, s / 3 + 1) {
    rep2(j, 0, s - 2) {
      if (dp.at(i - 1).at(j) == 0)
        continue;
      rep2(k, 3, s + 1) {
        if (j + k < 2001) {
          dp.at(i).at(j + k) += dp.at(i - 1).at(j);
          dp.at(i).at(j + k) %= 1000000007;
        }
      }
    }
  }

  ll ans = 0;
  rep2(i, 1, 2001) {
    ans += dp.at(i).at(s);
    // cout<<i<<" "<<ans<<endl;
    ans %= 1000000007;
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
#define ALL(l) (l).begin(), (l).end()
#define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
using namespace std;

// const ll mod =  1000000007;
// const ll maxn = 1000000000;

//---------------------------------------------------------------------------------------------------
using vi = vector<int>; // intの1次元の型に vi という別名をつける
using vll = vector<ll>; // long longの1次元の型に vll という別名をつける
using vvll = vector<vll>; // long longの2次元の型に vvll という別名をつける
using vs = vector<string>; // stringの1次元の型に vs という別名をつける
using pll =
    pair<ll, ll>; // これ以降 pll という型名はpair<ll, ll> と同じ意味で使える
//---------------------------------------------------------------------------------------------------

int main() {

  ll s;
  cin >> s;
  vvll dp(2001, vll(2001, 0));

  dp.at(0).at(0) = 1;
  rep2(i, 1, s / 3 + 1) {
    rep2(j, 0, s - 2) {
      rep2(k, 3, s + 1) {
        if (j + k < 2001) {
          dp.at(i).at(j + k) += dp.at(i - 1).at(j);
          dp.at(i).at(j + k) %= 1000000007;
        }
      }
    }
  }

  ll ans = 0;
  rep2(i, 1, 2001) {
    ans += dp.at(i).at(s);

    ans %= 1000000007;
  }

  cout << ans << endl;
}
Problem p02555 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
#define fi first
#define se second
#define pb push_back
#define wzh(x) cerr << #x << '=' << x << endl;
int s;
const int mod = 1e9 + 7;
int add(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
}
int dp[N];
int main() {
  ios::sync_with_stdio(false);
  cin >> s;
  dp[0] = 1;
  for (int i = 0; i <= s; i++) {
    for (int j = 3; j + i <= s; j++) {
      (dp[j + i] += dp[i]) >= mod && (dp[j + i] -= mod);
      //      add(dp[j+i],dp[i]);
    }
  }
  cout << dp[s] << '\\n';
  return 0;
}

Error: -11

Fixed submission:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
#define fi first
#define se second
#define pb push_back
#define wzh(x) cerr << #x << '=' << x << endl;
int s;

const int mod = 1e9 + 7;
int add(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
}
int dp[N];
int main() {
  ios::sync_with_stdio(false);
  cin >> s;
  dp[0] = 1;
  for (int i = 0; i <= s; i++) {
    for (int j = 3; j + i <= s; j++) {
      add(dp[j + i], dp[i]);
    }
  }
  cout << dp[s] << '\\n';
  return 0;
}
Problem p02555 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<long long> S(N);
  if (N >= 3) {
    S.at(2) = 1;
    if (N >= 4) {
      for (int i = 3; i < N; i++) {
        S.at(i) = S.at(i - 1) + S.at(i - 3);
        S.at(i) %= 1000000007;
      }
    }
  }
  cout << S.at(N - 1) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<long long> S(N);
  S.at(2) = 1;
  if (N >= 4) {
    for (int i = 3; i < N; i++) {
      S.at(i) = S.at(i - 1) + S.at(i - 3);
      S.at(i) %= 1000000007;
    }
  }
  cout << S.at(N - 1) << endl;
  return 0;
}
Problem p02555 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using ll = long long;
const ll MOD = 1000000007;

int main() {
  ll s;
  std::cin >> s;
  if (s <= 2) {
    std::cout << 0 << std::endl;
    return 0;
  }
  std::vector<ll> dp(s + 1, 0);
  dp[0] = 1;
  for (int i = 1; i <= s; i++) {
    for (int j = i - 3; j >= 0; j--) {
      dp[i] += dp[j];
      dp[i] %= MOD;
    }
  }
  std::cout << dp[s] << std::endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using ll = long long;
const ll MOD = 1000000007;
std::vector<std::vector<ll>>
    memo(1000, std::vector<ll>(3000, -1)); // memo[n][s] : n個で和がs

ll calc(ll i, ll s) {
  if (i == 0) {
    return s == 0;
  }
  if (i == 1) {
    return s >= 3;
  }
  if (s < 3) {
    return 0;
  }
  if (memo[i][s] != -1) {
    return memo[i][s];
  }

  ll tmp = 0;
  for (int j = 3; j <= s; j++) {
    tmp += calc(i - 1, s - j);
    tmp %= MOD;
  }

  memo[i][s] = tmp % MOD;
  return memo[i][s];
}

int main() {
  ll s;
  std::cin >> s;
  if (s <= 2) {
    std::cout << 0 << std::endl;
    return 0;
  }
  ll ans = 0;
  for (int i = 1;; i++) {
    ll tmp = calc(i, s);
    if (tmp == 0)
      break;
    ans += tmp;
    ans %= MOD;
  }

  std::cout << ans % MOD << std::endl;
  return 0;
}
Problem p02555 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>

using namespace std;

const int MOD = 1e9 + 7;

int bino[2001][2001];

int main() {
  memset(bino, 0, sizeof bino);
  bino[0][0] = 1;
  for (int i = 1; i <= 2000; ++i) {
    bino[i][0] = bino[i][i] = 1;
    for (int j = 1; j < i; ++j) {
      bino[i][j] = (bino[i - 1][j - 1] + bino[i - 1][j]) % MOD;
    }
  }

  int S;
  cin >> S;

  long long ret = 0;
  for (int k = 1; S - 2 * k - 1 >= 0; ++k) {
    ret = (ret + bino[S - 2 * k - 1][k - 1]) % MOD;
  }
  cout << ret << endl;
}

Error: 3

Fixed submission:

#include <iostream>

using namespace std;

const int MOD = 1000000000 + 7;
int answer = 0;

void findSequence(int S, int sum) {
  if (S == sum) {
    answer = (answer + 1) % MOD;
    return;
  }

  for (int i = 3; i <= S; ++i) {
    if (sum <= S - i)
      findSequence(S, sum + i);
  }
}

int main() {
  answer = 0;

  int S;
  cin >> S;

  findSequence(S, 0);
  return answer;
}
Problem p02555 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <iomanip>
#include <iostream> // cout, endl, cin
#include <map>      // map
#include <math.h>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;

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

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() {
  int S;
  cin >> S;

  COMinit();

  int n = 1;
  long long ans = 0;
  while (S - 3 * n >= 0) {
    ans += COM(S - 2 * n - 1, n - 1);
    ans = ans % MOD;
    n++;
  }
  cout << ans % MOD << endl;
}

Error: 0

Fixed submission:

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <iomanip>
#include <iostream> // cout, endl, cin
#include <map>      // map
#include <math.h>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;

const long long MOD = 1000000007;

long long Comb(int S, int n) {
  long long ret, divident, divisor;
  divident = 1;
  divisor = 1;
  if (n == 1) {
    ret = 1;
    return ret;
  }
  while (n > 1) {
    divisor *= (n - 1);
    divident *= (S - 2 * n - 1);
    n--;
  }
  ret = (divident / divisor) % MOD;
  return ret;
}

int main() {
  int S;
  cin >> S;
  int n = 1;
  long long ans = 0;
  while (S - 3 * n > 0) {
    ans += Comb(S, n);
    ans = ans % MOD;
    n++;
  }
  cout << ans % MOD << endl;
}
Problem p02555 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
long long dp[2005];
int main() {
  int s, i;
  cin >> s;
  for (i = 4, dp[3] = 1; i <= s; i++)
    dp[i] = (dp[i - 1] + dp[i - 3]) % 1000000007;
  cout << dp[s];
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long s, dp[800][2005];
long long f(int pos, int sum) {
  if (sum == s)
    return 1;
  if (sum > s)
    return 0;
  if (dp[pos][sum] != -1)
    return dp[pos][sum];
  dp[pos][sum] = 0;
  for (int i = 3; i + sum <= s; i++) {
    dp[pos][sum] += f(pos + 1, sum + i);
    dp[pos][sum] %= 1000000007;
  }
  return dp[pos][sum];
}
int main() {
  memset(dp, -1, sizeof(dp));
  cin >> s;
  cout << f(0, 0);
}
Problem p02555 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
#include "string"
#define int long long
#define pi pair<int, int>
#define ff first
#define ss second
#define boost                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(nullptr)
#define endl '\\n'

const int N = 1e3 + 1;
const int M = 1e9 + 7;

int mod(int m, int mod = M) {
  m %= mod;
  return (m + mod) % mod;
}
int s;
int dp[2 * N];

int solve(int sum) {
  if (sum >= s)
    return (sum == s);
  int &tmp = dp[sum];
  if (tmp != -1)
    return tmp;
  tmp = 0;
  for (int i = 3; sum + i <= s; i++)
    tmp = mod(tmp + solve(sum + i));
  return tmp;
}

int32_t main() {
  boost;
  cin >> s;
  memset(dp, -1, sizeof dp);
  int ans = solve(0);
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#include "string"
#define int long long
#define pi pair<int, int>
#define ff first
#define ss second
#define boost                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(nullptr)
#define endl '\\n'

const int N = 1e3 + 1;
const int M = 1e9 + 7;

int mod(int m, int mod = M) {
  m %= mod;
  return (m + mod) % mod;
}
int s;
int dp[N][2 * N];

int solve(int id, int sum) {
  if (sum >= s)
    return (sum == s);
  int &tmp = dp[id][sum];
  if (tmp != -1)
    return tmp;
  tmp = 0;
  for (int i = 3; sum + i <= s; i++)
    tmp = mod(tmp + solve(id + 1, sum + i));
  return tmp;
}

int32_t main() {
  boost;
  cin >> s;
  memset(dp, -1, sizeof dp);
  int ans = solve(0, 0);
  cout << ans << endl;
}
Problem p02555 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

int main() {
  int N, i, j;
  cin >> N;
  vector<int> S(N + 1);
  S[0] = S[1] = S[2] = 0;
  S[3] = S[4] = S[5] = 1;
  for (i = 6; i <= N; i += 1) {
    S[i] = 1;
    for (j = 3; j <= i - 3; j += 1) {
      S[i] = (S[i] + S[j]) % 1000000007;
    }
  }
  cout << S[N] << endl;
}


Error: 0

Fixed submission:

#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

int main() {
  long long int N, i, j;
  cin >> N;
  vector<long long int> S(N + 1);
  S[0] = S[1] = S[2] = 0;
  S[3] = S[4] = S[5] = 1;
  for (i = 6; i <= N; i += 1) {
    S[i] = 1;
    for (j = 3; j <= i - 3; j += 1) {
      S[i] = (S[i] + S[j]) % 1000000007;
    }
  }
  cout << S[N] << endl;
}
Problem p02555 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
typedef long long ll;
using namespace std;
const int mod = 1e9 + 7;
ll solve() {
  int n, ans = 0;
  cin >> n;
  if (n == 1)
    return 0;
  vector<vector<ll>> C(n, vector<ll>(n));
  C[0][0] = C[1][0] = C[1][1] = 1;
  for (int i = 2; i < n; i++) {
    C[i][0] = 1;
    for (int j = 1; j <= i; j++) {
      C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;
    }
  }
  for (int l = 1; n >= 3 * l; l++) {
    ans = (ans + C[n - 2 * l - 1][l - 1] + mod) % mod;
  }
  return ans;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin>>t;
  while (t--)
    cout << solve() << '\\n';
  // while(t--) solve(),cout<<'\\n';
  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
typedef long long ll;
using namespace std;
const int mod = 1e9 + 7;
ll solve() {
  int n, ans = 0;
  cin >> n;
  vector<vector<ll>> C(n, vector<ll>(n));
  C[1][0] = C[1][1] = 1;
  for (int i = 2; i < n; i++) {
    C[i][0] = 1;
    for (int j = 1; j <= i; j++) {
      C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;
    }
  }
  for (int l = 1; n > 3 * l; l++) {
    ans = (ans + C[n - 2 * l - 1][l - 1] + mod) % mod;
  }
  return ans;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin>>t;
  while (t--)
    cout << solve() << '\\n';
  // while(t--) solve(),cout<<'\\n';
  return 0;
}