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

// g++ -std=c++11 a.cpp
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
#define loop(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define FOR(i, a) for (auto i : a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
#define show1d(v)                                                              \\
  rep(i, v.size()) cout << " " << v[i];                                        \\
  cout << endl;
#define show2d(v)                                                              \\
  rep(i, v.size()) {                                                           \\
    rep(j, v[i].size()) cout << " " << v[i][j];                                \\
    cout << endl;                                                              \\
  }
using namespace std;
// kaewasuretyuui
typedef long long ll;
// #define int ll
typedef int Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;
#define mt make_tuple
typedef tuple<int, int, int, int> tp;
typedef vector<tp> vt;
template <typename A, typename B> bool cmin(A &a, const B &b) {
  return a > b ? (a = b, true) : false;
}
template <typename A, typename B> bool cmax(A &a, const B &b) {
  return a < b ? (a = b, true) : false;
}
// template<class C>constexpr int size(const C &c){return (int)c.size();}
// template<class T,size_t N> constexpr int size(const T
// (&xs)[N])noexcept{return (int)N;}
const double PI = acos(-1);
const double EPS = 1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9 + 10;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
// nCr mod m
#define MOD 1000000007
#define M 4000010
vector<ll> fact;
bool h = false;
ll A, B;
void init() {
  h = true;
  fact = vector<ll>(M);
  fact[0] = fact[1] = 1;
  loop(i, 2, M) fact[i] = fact[i - 1] * i % MOD;
}
// a^b mod MOD
ll powmod(ll a, ll b, ll m = MOD) {
  ll out = 1;
  ll p = a % m;
  while (b) {
    if (b & 1)
      out = out * p % m;
    p = p * p % m;
    b >>= 1;
  }
  return out;
}
// nCr
ll nCr(ll n, ll r, ll m = MOD) {
  if (!h)
    init();
  if (n < 0 || r < 0 || n < r)
    return 0; //??????
  ll out = fact[n] * powmod(fact[r] * fact[n - r] % m, m - 2, m) % m;
  return out;
}
void f(ll n, ll r, ll m = MOD) {
  if (n < 0 || r < 0 || n < r) {
    A = 0;
    return;
  }
  (A *= fact[n]) %= MOD;
  (B *= fact[r] * fact[n - r] % MOD) %= MOD;
  return;
}
int main() {
  ll a, b, c, d, e;
  while (cin >> a >> b >> c >> d >> e, a + b + c + d + e) {
    ll out = 0;
    ll aa = nCr(a + b + c, a), bb = nCr(b + c, b);
    rep(s, d + 1) {
      A = B = 1;
      ll t = e - d + s;
      if (t < 0 || t > e)
        continue;
      ll w = 1;
      if (s || a) {
        f(s - 1, a - 1);
        (A *= aa) %= MOD;
      }
      if (t || b) {
        f(t - 1, b - 1);
        (A *= bb) %= MOD;
      }
      f(d - s + a + b + c - 1, a + b + c - 1);
      (out += A * powmod(B, MOD - 2, MOD)) %= MOD;
    }
    cout << out << endl;
  }
}


Error: TLE

Fixed submission:

// g++ -std=c++11 a.cpp
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
#define loop(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define FOR(i, a) for (auto i : a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
#define show1d(v)                                                              \\
  rep(i, v.size()) cout << " " << v[i];                                        \\
  cout << endl;
#define show2d(v)                                                              \\
  rep(i, v.size()) {                                                           \\
    rep(j, v[i].size()) cout << " " << v[i][j];                                \\
    cout << endl;                                                              \\
  }
using namespace std;
// kaewasuretyuui
typedef long long ll;
// #define int ll
typedef int Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;
#define mt make_tuple
typedef tuple<int, int, int, int> tp;
typedef vector<tp> vt;
template <typename A, typename B> bool cmin(A &a, const B &b) {
  return a > b ? (a = b, true) : false;
}
template <typename A, typename B> bool cmax(A &a, const B &b) {
  return a < b ? (a = b, true) : false;
}
// template<class C>constexpr int size(const C &c){return (int)c.size();}
// template<class T,size_t N> constexpr int size(const T
// (&xs)[N])noexcept{return (int)N;}
const double PI = acos(-1);
const double EPS = 1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9 + 10;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
// nCr mod m
#define MOD 1000000007
#define M 4000010
vi fact;
bool h = false;
void init() {
  h = true;
  fact = vi(M);
  fact[0] = fact[1] = 1;
  loop(i, 2, M) fact[i] = (ll)fact[i - 1] * i % MOD;
}
// a^b mod MOD
ll powmod(ll a, ll b, ll m = MOD) {
  ll out = 1;
  ll p = a % m;
  while (b) {
    if (b & 1)
      out = out * p % m;
    p = p * p % m;
    b >>= 1;
  }
  return out;
}
// nCr
ll nCr(ll n, ll r, ll m = MOD) {
  if (!h)
    init();
  if (n < 0 || r < 0 || n < r)
    return 0; //??????
  ll out = (ll)fact[n] * powmod((ll)fact[r] * fact[n - r] % m, m - 2, m) % m;
  return out;
}
int main() {
  ll a, b, c, d, e;
  while (cin >> a >> b >> c >> d >> e, a + b + c + d + e) {
    if (d > e)
      swap(a, b), swap(d, e);
    ll out = 0;
    ll A = nCr(a + b + c, a), B = nCr(b + c, b);
    rep(s, d + 1) {
      ll t = e - d + s;
      if (t < 0 || t > e)
        continue;
      ll w = 1;
      if (s || a)
        (w *= nCr(s - 1, a - 1) * A % MOD) %= MOD;
      if (t || b)
        (w *= nCr(t - 1, b - 1) * B % MOD) %= MOD;
      (w *= nCr(d - s + a + b + c - 1, a + b + c - 1)) %= MOD;
      (out += w) %= MOD;
    }
    cout << out << endl;
  }
}