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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define INF (2 << 28)
#define fr first
#define sc second
using namespace std;

typedef long long ll;
typedef pair<int, int> iP;

int sum[1100000];

int main() {

  int n, cnt = 0;

  for (int i = 0; i <= 1000; i++) {
    for (int j = 0; j <= 1000; j++) {
      sum[cnt++] = i + j;
    }
  }

  sort(sum, sum + cnt);
  while (cin >> n) {
    int res = 0;
    for (int i = 0; i <= n; i++) {
      res += (upper_bound(sum, sum + cnt, n - i) -
              lower_bound(sum, sum + cnt, n - i)) *
             (upper_bound(sum, sum + cnt, i) - lower_bound(sum, sum + cnt, i));
    }
    cout << res << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {

  int n;

  vector<int> sum;
  for (int i = 0; i <= 1000; i++) {
    for (int j = 0; j <= 1000; j++) {
      sum.push_back(i + j);
    }
  }
  sort(sum.begin(), sum.end());

  while (cin >> n) {

    int res = 0;
    for (int i = 0; i < sum.size(); i++) {
      if (sum[i] > n)
        break;
      res += upper_bound(sum.begin(), sum.end(), n - sum[i]) -
             lower_bound(sum.begin(), sum.end(), n - sum[i]);
    }
    cout << res << endl;
  }
}
Problem p00096 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define LOOP(i, x, n) for (int i = x; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define MP make_pair
#define FR first
#define SC second
#define int long long
using namespace std;
const int MOD = 1000000007;
const int INF = 1000000009;

signed main() {
  int n;
  while (cin >> n) {
    vector<int> A(2001), B(2001);
    REP(i, 1001) REP(j, 1001) A[i + j]++;
    REP(i, 1001) REP(j, 1001) B[i + j]++;
    int ans = 0;
    REP(i, 2001) if (n - i >= 0 && n - i <= 2000) ans += A[i] * B[n - i];
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define LOOP(i, x, n) for (int i = x; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define MP make_pair
#define FR first
#define SC second
#define int long long
using namespace std;
const int MOD = 1000000007;
const int INF = 1000000009;

signed main() {
  int n;
  while (cin >> n) {
    map<int, int> A, B;
    REP(i, 1001) REP(j, 1001) A[i + j]++;
    REP(i, 1001) REP(j, 1001) B[i + j]++;
    int ans = 0;
    REP(i, 2001) if (n - i >= 0) ans += A[i] * B[n - i];
    cout << ans << endl;
  }
  return 0;
}
Problem p00096 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <numeric>

using namespace std;

int main() {
  int n;
  int dp[4001];
  while (true) {
    cin >> n;
    if (cin.eof()) {
      return 0;
    }
    for (int i = 0; i <= 1000; i++) {
      dp[i] = 1;
    }
    for (int i = 1001; i <= n; i++) {
      dp[i] = 0;
    }
    for (int i = 0; i < 3; i++) {
      for (int j = n; j >= 0; j--) {
        int *begin = dp + max(0, j - 1000);
        int *end = dp + j + 1;
        dp[j] = accumulate(begin, end, 0);
      }
    }
    cout << dp[n] << endl;
  }
}

Error: -11

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n;
  int dp[4001];
  while (true) {
    cin >> n;
    if (cin.eof()) {
      return 0;
    }
    for (int i = 0; i <= n; i++) {
      dp[i] = 0;
    }
    for (int i = 0; i < 4; i++) {
      for (int j = n; j >= 0; j--) {
        for (int k = 1; k <= 1000 && j - k >= 0; k--) {
          dp[j] += dp[j - k] + 1;
        }
      }
    }
    cout << dp[n] << endl;
  }
}
Problem p00096 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
typedef long long int lli;

lli halfComb[2001]; // 0 to 2000  init with 0

void init() {
  for (int i = 0; i < 1001; i++)
    for (int j = 0; j < 1001; j++)
      halfComb[i + j]++;
}

int main() {
  lli cnt;
  int n, mx;

  init();

  while (cin >> n) {
    cnt = 0;
    mx = (n < 2000
              ? n
              : 2000); // (a, b) の組合せそれぞれに対して(c, d) は高々2000通り
    for (int i = 0; i <= mx; i++) { // 0 <= a+b <= 2000
      if (n - i <= 2000)            // 0 <= c+d <= 2000
        cnt += halfComb[i] * halfComb[n - i];
    }
    cout << cnt << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
typedef long long int lli;

lli halfComb[2001]; // 0 to 2000  init with 0

void init() {
  for (int i = 0; i < 1001; i++)
    for (int j = 0; j < 1001; j++)
      halfComb[i + j]++;
}

int main() {
  lli cnt;
  int n;

  init();

  while (cin >> n) {
    cnt = 0;
    for (int i = 0; i <= n; i++) {
      cnt += halfComb[i] * halfComb[n - i];
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p00096 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;
#define loop(i, a, b) for (int i = (a); i < ull(b); ++i)
#define rep(i, n) loop(i, 0, n)
#define all(a) (a).begin(), (a).end()

const double eps = 1e-10;
const double pi = acos(-1.0);
const double inf = (int)1e8;

int main() {
  int n;
  int dp[4010];
  fill(dp, dp + 4010, 0);
  for (int i = 0; i <= 1000; i++)
    for (int j = 0; j <= 1000; j++)
      dp[i + j]++;
  while (cin >> n) {
    int ret = 0;
    for (int i = 0; i <= n; i++)
      ret += dp[i] * dp[n - i];
    cout << ret << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;
#define loop(i, a, b) for (int i = (a); i < ull(b); ++i)
#define rep(i, n) loop(i, 0, n)
#define all(a) (a).begin(), (a).end()

const double eps = 1e-10;
const double pi = acos(-1.0);
const double inf = (int)1e8;

int main() {
  int n;
  while (cin >> n) {
    int ret = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j <= min(n - i, 1000); j++) {
        for (int k = 0; k <= min(n - i - j, 1000); k++) {
          for (int l = 0; l <= min(n - i - j - k, 1000); l++)
            if (i + j + k + l == n)
              ret++;
        }
      }
    }
    cout << ret + 1 << endl;
  }
}
Problem p00096 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int dp[4010];
  int n;

  fill(dp, dp + 4010, 0);
  for (int i = 0; i < 1001; i++)
    for (int j = 0; j < 1001; j++)
      dp[i + j]++;

  while (cin >> n) {
    int ans = 0;
    for (int i = 0; i <= n; i++) {
      ans += dp[i] * dp[n - i];
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

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

int main() {
  int dp[2020];
  int n;
  for (int i = 0; i < 1001; i++)
    for (int j = 0; j < 1001; j++)
      dp[i + j]++;

  while (cin >> n) {
    int ans = 0;
    for (int i = 0; i <= n; i++) {
      ans += dp[i] * dp[n - i];
    }
    cout << ans << endl;
  }
}
Problem p00096 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int ab[2001];
  ab[0] = 1;
  for (int i = 1; i <= 1000; i++)
    ab[i] = ab[i - 1] + 1;
  for (int i = 0; i < 1000; i++)
    ab[2000 - i] = ab[i];

  int n;
  while (cin >> n) {
    long long int ans = 0;
    for (int i = 0; i <= 2000; i++) {
      int cd = n - i;
      if (cd > 2000)
        continue;
      if (cd < 0)
        break;
      ans += (long long int)ab[i] * ab[cd];
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  while (cin >> n) {
    int cnt = 0;
    for (int a = 0; a <= (min(1000, n)); a++) {
      for (int b = 0; b <= (min(1000, n)); b++) {
        for (int c = 0; c <= (min(1000, n)); c++) {
          if (n - a - b - c >= 0 && n - a - b - c <= 1000) {
            cnt++;
          }
        }
      }
    }
    cout << cnt << endl;
  }
}
Problem p00096 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, l, n) for (int i = l; i < n; i++)
#define rer(i, l, n) for (int i = l; i <= n; i++)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define pb(a) push_back(a)
#define mk(a, b) make_pair(a, b)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int main() {
  int n;
  while (cin >> n) {
    vi v(2001);
    int ans = 0;
    rer(i, 0, min(1000, n)) {
      rer(j, 0, min(1000, n)) { v[i + j]++; }
    }
    /*sort(all(v));
    rep(i,0,v.size()){
            ans+=upper_bound(all(v),n-v[i])-lower_bound(all(v),n-v[i]);
    }*/
    /*rer(i,0,min(1000,n)){
            rer(j,0,min(1000,n)){
                    if((n-i-j)<0) continue;
                    ans+=upper_bound(all(v),n-i-j)-lower_bound(all(v),n-i-j);
            }
    }*/
    rer(i, 0, v.size()) {
      rer(j, 0, v.size()) {
        if (i + j == n)
          ans += (v[i] * v[j]);
      }
    }
    o(ans);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, l, n) for (int i = l; i < n; i++)
#define rer(i, l, n) for (int i = l; i <= n; i++)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define pb(a) push_back(a)
#define mk(a, b) make_pair(a, b)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int main() {
  int n;
  while (cin >> n) {
    vi v;
    int ans = 0;
    rer(i, 0, min(1000, n)) {
      rer(j, 0, min(1000, n)) { v.pb(i + j); }
    }
    sort(all(v));
    /*rep(i,0,v.size()){
            ans+=upper_bound(all(v),n-v[i])-lower_bound(all(v),n-v[i]);
    }*/
    rer(i, 0, min(1000, n)) {
      rer(j, 0, min(1000, n)) {
        if ((n - i - j) < 0)
          continue;
        ans += upper_bound(all(v), n - i - j) - lower_bound(all(v), n - i - j);
      }
    }
    o(ans);
  }
}
Problem p00096 (C++) — buggy submission, verdict: Runtime Error

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

vector<int> v;
int a[10001];

int main() {
  for (int i = 0; i <= 1000; i++) {
    for (int j = 0; j <= 1000; j++) {
      a[i + j]++;
    }
  }

  int n;
  while (~scanf("%d", &n)) {
    int res = 0;
    for (int i = 0; i <= n; i++) {
      res += a[i] * a[n - i];
    }
    printf("%d\\n", res);
  }
}

Error: 0

Fixed submission:

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

vector<int> v;
int a[2001];

int main() {
  for (int i = 0; i <= 1000; i++) {
    for (int j = 0; j <= 1000; j++) {
      a[i + j]++;
    }
  }

  int n;
  while (~scanf("%d", &n)) {
    int res = 0;
    for (int i = 0; i <= n; i++) {
      res += a[i] * a[n - i];
    }
    printf("%d\\n", res);
  }
}
Problem p00096 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int dp[5][4444];

int solve(int k, int sum) {
  if (k == 3)
    return sum > 1000 ? 0 : 1;
  if (k == 2 && sum > 2000)
    return 0;
  if (k == 1 && sum > 3000)
    return 0;
  if (dp[k][sum])
    return dp[k][sum];

  int ret = 0;
  for (int i = 0; i <= 1000 && i <= sum; i++) {
    ret += solve(k + 1, sum - i);
  }
  return dp[k][sum] = ret;
}
int main(void) {
  int n;
  for (int i = 0; i <= 4000; i++) {
    solve(0, i);
    // cout << i << endl;
  }
  while (cin >> n) {
    cout << solve(0, n) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int dp[5][4444];
int solve(int k, int sum) {
  if (k == 4)
    return sum ? 0 : 1;
  if (dp[k][sum])
    return dp[k][sum];

  int ret = 0;
  for (int i = 0; i <= 1000 && i <= sum; i++) {
    ret += solve(k + 1, sum - i);
  }
  return dp[k][sum] = ret;
}
int main(void) {
  int n;
  for (int i = 0; i <= 4000; i++)
    solve(0, i);
  while (cin >> n) {
    cout << solve(0, n) << endl;
  }
}
Problem p00096 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>

using namespace std;

int dp[4000];

int main() {
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i <= 1000; i++) {
    for (int j = 0; j <= 1000; j++) {
      dp[i + j]++;
    }
  }
  int n;
  while (cin >> n) {
    int ans = 0;
    for (int i = 0; i <= n; i++) {
      ans += dp[i] * dp[n - i];
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstring>
#include <iostream>

using namespace std;

int dp[2000];

int main() {
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i <= 1000; i++) {
    for (int j = 0; j <= 1000; j++) {
      dp[i + j]++;
    }
  }
  int n;
  while (cin >> n) {
    int ans = 0;
    for (int i = 0; i <= n; i++) {
      ans += dp[i] * dp[n - i];
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00096 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>

int half[2001]; // 0<=a, b<=1000 として a+b となる組み合わせの数

void makeSet() {
  memset(half, 0, sizeof(half));
  // 半分全列挙
  for (int i = 0; i <= 1000; ++i) {
    for (int j = 0; j <= 1000; ++j) {
      int k = i + j;
      ++half[k];
    }
  }
}

int solve(int n) {
  int num = 0;
  int max = n < 2000 ? n : 2000; // Counterが半分全列挙の上限を超えない為

  for (int i = 0; i <= max; ++i) {
    if (n - i <= 2000)
      num +=
          half[i] *
          half[n - i]; // 左半分の組み合わせ * 右半分の組み合わせ = 全組み合わせ
  }
  return num;
}

int main() {
  makeSet();

  while (true) {
    int n;
    std::cin >> n;
    if (std::cin.eof())
      break;

    std::cout << solve(n) << std::endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstring>
#include <iostream>

int half[2001]; // 0<=a, b<=1000 として a+b となる組み合わせの数

void makeSet() {
  memset(half, 0, sizeof(half));
  // 半分全列挙
  for (int i = 0; i <= 1000; ++i) {
    for (int j = 0; j <= 1000; ++j) {
      int k = i + j;
      ++half[k];
    }
  }
}

int solve(int n) {
  int num = 0;
  int max = n < 2000 ? n : 2000; // Counterが半分全列挙の上限を超えない為

  for (int i = 0; i <= max; ++i) {
    num +=
        half[i] *
        half[n - i]; // 左半分の組み合わせ * 右半分の組み合わせ = 全組み合わせ
  }
  return num;
}

int main() {
  makeSet();

  while (true) {
    int n;
    std::cin >> n;
    if (std::cin.eof())
      break;

    std::cout << solve(n) << std::endl;
  }
  return 0;
}
Problem p00096 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
long long dp[4001] = {0};
int main() {
  dp[0] = 1;
  for (int i = 0; i < 4; ++i) {
    for (int j = i * 1000; j >= 0; --j) {
      long long back = dp[j];
      dp[j] = 0;
      for (int k = 0; k <= 1000; ++k) {
        dp[j + k] += back;
      }
    }
  }
  int n;
  while (cin >> n) {
    cout << dp[n] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int n;
int dp[5][4001];

int func(int i, int j) {
  if (dp[i][j] >= 0) {
    return dp[i][j];
  }
  int res = 0;
  if (i == 4) {
    if (j == n) {
      return 1;
      ;
    } else {
      return 0;
    }
  } else {
    for (int k = 0; k <= n; k++) {
      res += func(i + 1, j + k);
    }
  }
  return dp[i][j] = res;
}

int main() {
  while (cin >> n) {
    for (int i = 0; i < 5; i++) {
      for (int j = 0; j < 1001; j++) {
        dp[i][j] = -1;
      }
    }
    cout << func(0, 0) << endl;
  }
  return 0;
}
Problem p00096 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
using namespace std;

int dp[5][4001];
int main() {
  int n;
  dp[0][0] = 1;
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j <= i * 1000; j++) {
      for (int k = 0; k <= 1000; k++) {
        dp[i + 1][j + k] += dp[i][j];
      }
    }
  }
  /*for (int i = 0; i < 10;i++){
          for (int j = 0; j < 10;j++){
                  printf("%d ", dp[i][j]);
          }
          puts("");
  }*/
  while (cin >> n) {
    printf("%d\\n", dp[4][n]);
  }
  return 0;
}

Error: -11

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;

int dp[4][4001];
int main() {
  int n;
  for (int i = 0; i <= 4000; i++) {
    dp[0][i] = 1;
  }
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j <= 1000; j++) {
      for (int k = 0; k <= 1000; k++) {
        dp[i + 1][j + k] += dp[i][j];
      }
    }
  }
  /*for (int i = 0; i < 10;i++){
          for (int j = 0; j < 10;j++){
                  printf("%d ", dp[i][j]);
          }
          puts("");
  }*/
  while (cin >> n) {
    printf("%d\\n", dp[3][n]);
  }
  return 0;
}
Problem p00096 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int a, b, c, d, n;
long long ans;
int main() {

  while (cin >> n) {

    ans = 0;
    for (a = 0; a <= 1000 && a <= n; a++) {
      for (b = 0; a + b <= n && b <= 1000; b++) {
        if (n - a - b > 2000)
          continue;
        else if (n - a - b > 1000)
          ans += 2000 - (n - a - b) + 1;
        else
          ans += n - a - b + 1;
      }
    }

    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int a, b, c, d, n;
long long ans;
int main() {

  while (cin >> n) {

    ans = 0;
    for (a = 0; a <= 1000 && a <= n; a++) {
      for (b = 0; a + b <= n && b <= 1000; b++) {
        for (c = 0; a + b + c <= n && c <= 1000; c++) {
          if (n - a - b - c <= 1000)
            ans++;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00096 (C++) — buggy submission, verdict: Runtime Error

// vim: foldmethod=marker
// templates {{{
#include <bits/stdc++.h>
using namespace std;

const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

inline int input() {
  int i;
  scanf("%d%*c", &i);
  return i;
}
inline double dinput() {
  double d;
  scanf("%lf%*c", &d);
  return d;
}
inline void output(int i, char c = '\\n') { printf("%d%c", i, c); }
inline void output(ll l, char c = '\\n') { printf("%lld%c", l, c); }

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)

#define ALL(o) o.begin(), o.end()
//}}}

const int INF = 1000000000;
const int MOD = 1000000000 + 0;
#define EPS (1e-14)

ll comb[4096];

int main() {
  int n;

  REP(i, 1001) {
    REP(j, 1001) { comb[i + j]++; }
  }
  while (~scanf("%d", &n)) {
    ll cnt = 0;

    REP(i, n / 2 + 1) {
      if (n - i > 2000)
        continue;
      ll t = comb[i] * comb[n - i];
      if (n % 2 || i != n / 2)
        t *= 2;
      cnt += t;
    }
    output(cnt);
  }

  return 0;
}

Error: 0

Fixed submission:

// vim: foldmethod=marker
// templates {{{
#include <bits/stdc++.h>
using namespace std;

const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

inline int input() {
  int i;
  scanf("%d%*c", &i);
  return i;
}
inline double dinput() {
  double d;
  scanf("%lf%*c", &d);
  return d;
}
inline void output(int i, char c = '\\n') { printf("%d%c", i, c); }
inline void output(ll l, char c = '\\n') { printf("%lld%c", l, c); }

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)

#define ALL(o) o.begin(), o.end()
//}}}

const int INF = 1000000000;
const int MOD = 1000000000 + 0;
#define EPS (1e-14)

ll comb[2048];

int main() {
  int n;

  REP(i, 1001) {
    REP(j, 1001) { comb[i + j]++; }
  }
  while (~scanf("%d", &n)) {
    ll cnt = 0;

    REP(i, n + 1) { cnt += comb[i] * comb[n - i]; }
    output(cnt);
  }

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

#include <iostream>
using namespace std;

int main() {
  int n;
  int ans[4001] = {0};
  for (int i = 0; i <= 1000; i++) {
    ans[i] = 1;
  }
  for (int i = 1; i < 4; i++) {
    for (int j = 4000; j > 0; j--) {
      for (int k = 1; k <= 1000; k++) {
        if (j - k < 0)
          break;
        ans[j] += ans[j - k];
      }
    }
  }
  while (cin >> n) {
    cout << ans[n] << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n, ans;
  while (cin >> n) {
    ans = 0;
    for (int a = 0; a <= 1000; a++) {
      if (a > n)
        continue;
      for (int b = 0; b <= 1000; b++) {
        if (a + b > n)
          continue;
        for (int c = 0; c <= 1000; c++) {
          if (a + b + c > n)
            continue;
          int d = n - a - b - c;
          ans++;
        }
      }
    }
    cout << ans << endl;
  }
}
Problem p00096 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define rep(i, x) for (i = 0; i < x; i++)
#define rrep(i, x) for (i = x - 1; i >= 0; i--)
#define rep_to(a, i, x) for (i = a; i < x; i++)
#define si_1(x) scanf("%d", &x)
#define si_2(x, y) scanf("%d %d", &x, &y)
#define debug(x) printf("x:%d\\n", x)

int main() {
  int i, j, k, num[10] = {}, n, flag = 0;
  double sum = 2, sum2 = 0;
  double count = 1, plus;
  while (si_1(n) != EOF) {
    /*
            count=0;
            rep(i,1001)
            {
                    num[0]=n-i;
                    rep(j,num[0]+1)
                    {
                            if(j>1000)break;
                            num[1]=n-(i+j);
                            rep(k,num[1]+1)
                            {
                                    if(k>1000)break;
                                    num[2]=n-(i+j+k);
                                    if(n<(i+j+k+num[2]) ||
       4000<(i+j+k+num[2]))break; if(num[2]<=1000 && num[2]>=0)
                                    {
                                            count++;
                                            //printf("%d+%d+%d+%d=%d\\n",i,j,k,num[2],n);
                                    }
                            }
                            if(n<(i+j) || 4000<(i+j))break;
                    }
                    if(n<i || 4000<i)break;
            }
    */

    flag = 0;
    sum = 1;
    sum2 = 1001;
    count = 1;
    plus = 1;
    rep(i, n) {
      sum++;
      if (sum <= 1000) {
        plus += sum;
      } else if (sum > 1000 && sum <= 2001) {
        plus += sum2;
        sum2 -= 3;
      } else if (sum > 2001 && sum <= 3002) {
        plus += sum2;
        sum2 += 3;
      } else if (sum > 3002) {
        plus += sum2;
        sum2--;
      }
      count += plus;
      // printf("plus:%.0f\\n", plus);
    }

    printf("%.0f\\n", count);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define rep(i, x) for (i = 0; i < x; i++)
#define rrep(i, x) for (i = x - 1; i >= 0; i--)
#define rep_to(a, i, x) for (i = a; i < x; i++)
#define si_1(x) scanf("%d", &x)
#define si_2(x, y) scanf("%d %d", &x, &y)
#define debug(x) printf("x:%d\\n", x)

int main() {
  int i, j, k, num[10] = {}, n;
  int count = 0;
  while (si_1(n) != EOF) {
    count = 0;
    rep(i, 1000) {
      num[0] = n - i;
      rep(j, num[0] + 1) {
        num[1] = n - (i + j);
        rep(k, num[1] + 1) {
          num[2] = n - (i + j + k);
          if (num[2] <= 1000 && num[2] >= 0)
            count++;
        }
      }
      if (n <= i)
        break;
    }
    printf("%d\\n", count);
  }

  return 0;
}