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

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

using namespace std;

#define pb(n) push_back(n)
#define mp(n, m) make_pair(n, m)
#define fi first
#define se second
#define all(r) (r).begin(), (r).end()

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define rep(i, n) REP(i, 0, n)
#define REPE(i, s, e) for (int i = s; i > e; i--)
#define repe(i, n) REPE(i, n, -1)

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

typedef vector<vi> vvi;

const int IMAX = ((1 << 30) - 1) * 2 + 1;
const int INF = 100000000;
const double EPS = 1e-10;
// int mod=1000000007

int main() {
  int n;
  vi v(2001, 0);
  for (int i = 0; i <= 1000; i++) {
    for (int j = 0; j <= 1000; j++) {
      v[i + j]++;
    }
  }
  while (cin >> n) {
    int ans = 0;
    for (int i = 0; i <= 2000; i++) {
      for (int j = 0; j <= 2000; j++) {
        if (i + j == n)
          ans += (v[i] * v[j]);
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

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

using namespace std;

#define pb(n) push_back(n)
#define mp(n, m) make_pair(n, m)
#define fi first
#define se second
#define all(r) (r).begin(), (r).end()

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define rep(i, n) REP(i, 0, n)
#define REPE(i, s, e) for (int i = s; i > e; i--)
#define repe(i, n) REPE(i, n, -1)

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

typedef vector<vi> vii;

const int IMAX = ((1 << 30) - 1) * 2 + 1;
const int INF = 100000000;
const double EPS = 1e-10;
// int mod=1000000007

vi fact(5, 1);
vi v(4);
vi same(4);

void init_fact() {
  for (int i = 0; i < 5; i++) {
    for (int j = 1; j <= i; j++)
      fact[i] *= j;
  }
}

void set_same(int a, int b, int c, int d) {
  v[0] = a;
  v[1] = b;
  v[2] = c;
  v[3] = d;
  for (int i = 0; i < 4; i++)
    same[i] = 1;
  for (int i = 0; i < 4; i++) {
    for (int j = i + 1; j < 4; j++) {
      if (v[i] == v[j])
        same[i]++;
      else {
        i = j - 1;
        break;
      }
      if (j == 3)
        return;
    }
  }
}

int main() {
  init_fact();
  int n;
  int ans;
  int t;
  while (cin >> n) {
    ans = 0;
    for (int a = max(0, n - 3000); a < 1001; a++) {
      // if(n-a>3000)continue;
      if (n < a)
        break;
      for (int b = max(a, n - a - 2000); b < 1001; b++) {
        // if(n-(a+b)>2000)continue;
        if (n < a + b)
          break;
        for (int c = max(b, n - a - b - 1000); c < 1001; c++) {
          // if(n-(a+b+c)>1000)continue;
          if (n < (a + b + c))
            break;
          for (int d = max(c, n - a - b - c); d < 1001; d++) {
            if (n < (a + b + c + d))
              break;
            if (n == (a + b + c + d)) {
              set_same(a, b, c, d);
              t = fact[4];
              for (int i = 0; i < 4; i++)
                t /= fact[same[i]];
              ans += t;
            }
          }
        }
      }
    }
    cout << ans << endl;
  }
}