Problem p04040 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define rep(i, a, b) for (int i = (a), _ = (b); i <= _; ++i)
#define per(i, a, b) for (int i = (a), _ = (b); i >= _; --i)
#define For(i, a, b) for (int i = (a), _ = (b); i < _; ++i)
#define ri rd<int>
using namespace std;
const int mod = 1e9 + 7;
const int maxN = 2e5 + 7;

inline int pls(int x, int y) { return (x + y) % mod; }
inline int mns(int x, int y) { return pls(x, mod - y); }
inline int mul(int x, int y) { return 1LL * x * y % mod; }
inline void Add(int &x, int y) { x = pls(x, y); }
inline void Mul(int &x, int y) { x = mul(x, y); }
inline void Minus(int &x, int y) { x = mns(x, y); }
inline int pwr(int x, int tms) {
  int res = 1;
  for (; tms > 0; tms >>= 1) {
    if (tms & 1)
      Mul(res, x);
    Mul(x, x);
  }
  return res;
}
inline int inv(int x) { return pwr(x, mod - 2); }

template <class T> inline T rd() {
  bool f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = 0;
  T x = 0;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - 48;
  return f ? x : -x;
}

int n, m, A, B;
int fac[maxN], ifac[maxN], iv[maxN];

void init() {
  iv[1] = 1;
  For(i, 2, maxN) iv[i] = mul(mod - mod / i, iv[mod % i]);
  fac[0] = 1;
  For(i, 1, maxN) fac[i] = mul(fac[i - 1], i);
  ifac[0] = 1;
  For(i, 1, maxN) ifac[i] = mul(ifac[i - 1], iv[i]);
}

inline int C(int x, int y) {
  return (y <= x) ? mul(mul(fac[x], ifac[y]), ifac[x - y]) : 0;
}

inline int ways(int x, int y) { return C(x + y, x); }

int main() {
#ifndef ONLINE_JUDGE
  freopen("a.in", "r", stdin);
#endif

  init();

  n = ri(), m = ri(), A = ri(), B = ri();

  n -= A;
  int res = 0;
  rep(j, B + 1, m) { Add(res, mul(ways(n - 1, j - 1), ways(A - 1, m - j))); }
  printf("%d\n", res);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define rep(i, a, b) for (int i = (a), _ = (b); i <= _; ++i)
#define per(i, a, b) for (int i = (a), _ = (b); i >= _; --i)
#define For(i, a, b) for (int i = (a), _ = (b); i < _; ++i)
#define ri rd<int>
using namespace std;
const int mod = 1e9 + 7;
const int maxN = 2e5 + 7;

inline int pls(int x, int y) { return (x + y) % mod; }
inline int mns(int x, int y) { return pls(x, mod - y); }
inline int mul(int x, int y) { return 1LL * x * y % mod; }
inline void Add(int &x, int y) { x = pls(x, y); }
inline void Mul(int &x, int y) { x = mul(x, y); }
inline void Minus(int &x, int y) { x = mns(x, y); }
inline int pwr(int x, int tms) {
  int res = 1;
  for (; tms > 0; tms >>= 1) {
    if (tms & 1)
      Mul(res, x);
    Mul(x, x);
  }
  return res;
}
inline int inv(int x) { return pwr(x, mod - 2); }

template <class T> inline T rd() {
  bool f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = 0;
  T x = 0;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - 48;
  return f ? x : -x;
}

int n, m, A, B;
int fac[maxN], ifac[maxN], iv[maxN];

void init() {
  iv[1] = 1;
  For(i, 2, maxN) iv[i] = mul(mod - mod / i, iv[mod % i]);
  fac[0] = 1;
  For(i, 1, maxN) fac[i] = mul(fac[i - 1], i);
  ifac[0] = 1;
  For(i, 1, maxN) ifac[i] = mul(ifac[i - 1], iv[i]);
}

inline int C(int x, int y) {
  return (y <= x) ? mul(mul(fac[x], ifac[y]), ifac[x - y]) : 0;
}

inline int ways(int x, int y) { return C(x + y, x); }

int main() {
  init();

  n = ri(), m = ri(), A = ri(), B = ri();

  n -= A;
  int res = 0;
  rep(j, B + 1, m) { Add(res, mul(ways(n - 1, j - 1), ways(A - 1, m - j))); }
  printf("%d\n", res);

  return 0;
}