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

#include <bits/stdc++.h>

using namespace std;

#ifdef DEBUG_MODE
#define DBG(n) n;
#else
#define DBG(n) ;
#endif
#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define rep(i, s, g) for (ll(i) = (s); (i) < (g); ++i)
#define rrep(i, s, g) for (ll(i) = (s); i >= (g); --(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \\
  {                                                                            \\
    for (int WWW = 0; WWW < (n); WWW++)                                        \\
      cerr << v[WWW] << ' ';                                                   \\
    cerr << endl << endl;                                                      \\
  }
#define SHOW2d(v, i, j)                                                        \\
  {                                                                            \\
    for (int aaa = 0; aaa < i; aaa++) {                                        \\
      for (int bbb = 0; bbb < j; bbb++)                                        \\
        cerr << v[aaa][bbb] << ' ';                                            \\
      cerr << endl;                                                            \\
    }                                                                          \\
    cerr << endl;                                                              \\
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007

typedef long long ll;
typedef pair<ll, ll> P;

// ａをbで割る
long long mod_div(ll a, ll b) {
  if (a % b == 0)
    return a / b;
  long long tmp = MOD - 2, c = b, ret = 1;
  while (tmp > 0) {
    if (tmp & 1) {
      ret *= c;
      ret %= MOD;
    }
    c *= c;
    c %= MOD;
    tmp >>= 1;
  }
  return a * ret % MOD;
}

#define MAX_K 3333333
vector<long long> kaijo(MAX_K);
long long combination(long long n, long long r) {
  if (n < r || n < 0 || r < 0)
    return 0;
  if (kaijo[0] != 1) {
    kaijo[0] = 1;
    for (long long i = 1; i < MAX_K; i++)
      kaijo[i] = (kaijo[i - 1] * i) % MOD;
  }
  long long ret = kaijo[n];
  long long tmp = (kaijo[r] * kaijo[n - r]) % MOD;
  return mod_div(ret, tmp);
}

int main() {
  ll a, b, c, x, y;

  while (cin >> a >> b >> c >> x >> y, a + b + c + x + y) {

    ll ans = 0;
    for (ll i = 0; i <= min(x, y); i++) {
      ll X = x - i - a;
      ll Y = y - i - b;
      if (X < 0 || y < 0)
        continue;
      if (a == 0 && X > 0)
        continue;
      if (b == 0 && Y > 0)
        continue;
      ll tmp = 1;
      tmp = (tmp * combination(a + b + c, c)) % MOD;
      tmp = (tmp * combination(a + b, b)) % MOD;
      if (a + b + c + i - 1 >= i)
        tmp = (tmp * combination(a + b + c + i - 1, i)) % MOD;
      if (a + X - 1 >= X)
        tmp = (tmp * combination(a + X - 1, X)) % MOD;
      if (b + Y - 1 >= Y)
        tmp = (tmp * combination(b + Y - 1, Y)) % MOD;
      ans = (ans + tmp) % MOD;
    }

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#ifdef DEBUG_MODE
#define DBG(n) n;
#else
#define DBG(n) ;
#endif
#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define rep(i, s, g) for (ll(i) = (s); (i) < (g); ++i)
#define rrep(i, s, g) for (ll(i) = (s); i >= (g); --(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \\
  {                                                                            \\
    for (int WWW = 0; WWW < (n); WWW++)                                        \\
      cerr << v[WWW] << ' ';                                                   \\
    cerr << endl << endl;                                                      \\
  }
#define SHOW2d(v, i, j)                                                        \\
  {                                                                            \\
    for (int aaa = 0; aaa < i; aaa++) {                                        \\
      for (int bbb = 0; bbb < j; bbb++)                                        \\
        cerr << v[aaa][bbb] << ' ';                                            \\
      cerr << endl;                                                            \\
    }                                                                          \\
    cerr << endl;                                                              \\
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007

typedef long long ll;
typedef pair<ll, ll> P;

// ａをbで割る
long long mod_div(ll a, ll b) {
  if (a % b == 0)
    return a / b;
  long long tmp = MOD - 2, c = b, ret = 1;
  while (tmp > 0) {
    if (tmp & 1) {
      ret *= c;
      ret %= MOD;
    }
    c *= c;
    c %= MOD;
    tmp >>= 1;
  }
  return a * ret % MOD;
}

#define MAX_K 6666666
vector<long long> kaijo(MAX_K);
long long combination(long long n, long long r) {
  if (n < r || n < 0 || r < 0)
    return 0;
  if (kaijo[0] != 1) {
    kaijo[0] = 1;
    for (long long i = 1; i < MAX_K; i++)
      kaijo[i] = (kaijo[i - 1] * i) % MOD;
  }
  long long ret = kaijo[n];
  long long tmp = (kaijo[r] * kaijo[n - r]) % MOD;
  return mod_div(ret, tmp);
}

int main() {
  int a, b, c, x, y;

  while (cin >> a >> b >> c >> x >> y, a + b + c + x + y) {

    ll ans = 0;
    for (int i = 0; i <= x + y; i++) {
      int X = x - i - a;
      int Y = y - i - b;
      if (X < 0 && y < 0)
        continue;
      ll tmp = 1;
      tmp = (tmp * combination(a + b + c, c)) % MOD;
      tmp = (tmp * combination(a + b, b)) % MOD;
      tmp = (tmp * combination(a + b + c + i - 1, i)) % MOD;
      tmp = (tmp * combination(a + X - 1, X)) % MOD;
      tmp = (tmp * combination(b + Y - 1, Y)) % MOD;
      ans = (ans + tmp) % MOD;
    }

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