Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast,fast-math,unroll-loops")

#include <bits/stdc++.h>

#define int ll
// #define double long double
// #define endl '\n'
#define all(C) (C).begin(), (C).end()
#define rall(C) (C).rbegin(), (C).rend()
#define mp make_pair
#define pb emplace_back
#define dbg(x) cerr << #x << " : " << x << endl
#define PI 3.14159265358979

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pld = pair<ld, ld>;

/*
const ll MAX_MEM = 3e8;
char MEM[MAX_MEM];
ll MEM_POS = 0;
void* operator new(size_t x)
{
    auto ret = MEM + MEM_POS;
    MEM_POS += x;
    assert(MEM_POS < MAX_MEM);
    return ret;
}
void operator delete(void*)
{}
*/

template <class T> istream &operator>>(istream &in, vector<T> &a) {
  for (auto &i : a)
    in >> i;
  return in;
}

template <class T> ostream &operator<<(ostream &out, vector<T> a) {
  for (auto &i : a)
    out << i << ' ';
  return out;
}

template <class T, class U> istream &operator>>(istream &in, pair<T, U> &p) {
  in >> p.first >> p.second;
  return in;
}

template <class T, class U> ostream &operator<<(ostream &out, pair<T, U> p) {
  out << p.first << " " << p.second << " ";
  return out;
}

inline void Start() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
}
const int P = 1e9 + 7;
int add(int a, int b) { return a + b >= P ? a + b - P : a + b; }
int mul(int a, int b) { return (a * b) % P; }
int bpow(int a, int n) {
  int res = 1;
  while (n) {
    if (n & 1)
      res = mul(res, a);
    a = mul(a, a);
    n >>= 1;
  }
  return res;
}
int rev(int x) { return bpow(x, P - 2); }
int sub(int a, int b) { return a - b >= 0 ? a - b : (a - b + P * P) % P; }
struct comb {
  vector<int> fact, revf;
  int N;
  comb() {}
  comb(int _n) {
    N = _n;
    fact.resize(N + 1, 1);
    revf.resize(N + 1, 1);
    for (int i = 1; i <= N; ++i)
      fact[i] = mul(fact[i - 1], i), revf[i] = mul(revf[i - 1], rev(i));
  }
  int operator()(int n, int k) {
    return mul(fact[n], mul(revf[k], revf[n - k]));
  }
};
signed main() {
  Start();
  const int N = 1e6 + 1;
  comb c(N);
  int x, y;
  cin >> x >> y;
  if ((x + y) % 3 != 0)
    return cout << 0, 0;
  cout << c((x + y) / 3, min(x, y) - (x + y) / 3);

  return 0;
}


Fixed submission:
#pragma GCC optimize("Ofast,fast-math,unroll-loops")

#include <bits/stdc++.h>

#define int ll
// #define double long double
// #define endl '\n'
#define all(C) (C).begin(), (C).end()
#define rall(C) (C).rbegin(), (C).rend()
#define mp make_pair
#define pb emplace_back
#define dbg(x) cerr << #x << " : " << x << endl
#define PI 3.14159265358979

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pld = pair<ld, ld>;

/*
const ll MAX_MEM = 3e8;
char MEM[MAX_MEM];
ll MEM_POS = 0;
void* operator new(size_t x)
{
    auto ret = MEM + MEM_POS;
    MEM_POS += x;
    assert(MEM_POS < MAX_MEM);
    return ret;
}
void operator delete(void*)
{}
*/

template <class T> istream &operator>>(istream &in, vector<T> &a) {
  for (auto &i : a)
    in >> i;
  return in;
}

template <class T> ostream &operator<<(ostream &out, vector<T> a) {
  for (auto &i : a)
    out << i << ' ';
  return out;
}

template <class T, class U> istream &operator>>(istream &in, pair<T, U> &p) {
  in >> p.first >> p.second;
  return in;
}

template <class T, class U> ostream &operator<<(ostream &out, pair<T, U> p) {
  out << p.first << " " << p.second << " ";
  return out;
}

inline void Start() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
}
const int P = 1e9 + 7;
int add(int a, int b) { return a + b >= P ? a + b - P : a + b; }
int mul(int a, int b) { return (a * b) % P; }
int bpow(int a, int n) {
  int res = 1;
  while (n) {
    if (n & 1)
      res = mul(res, a);
    a = mul(a, a);
    n >>= 1;
  }
  return res;
}
int rev(int x) { return bpow(x, P - 2); }
int sub(int a, int b) { return a - b >= 0 ? a - b : (a - b + P * P) % P; }
struct comb {
  vector<int> fact, revf;
  int N;
  comb() {}
  comb(int _n) {
    N = _n;
    fact.resize(N + 1, 1);
    revf.resize(N + 1, 1);
    for (int i = 1; i <= N; ++i)
      fact[i] = mul(fact[i - 1], i), revf[i] = mul(revf[i - 1], rev(i));
  }
  int operator()(int n, int k) {
    return mul(fact[n], mul(revf[k], revf[n - k]));
  }
};
signed main() {
  Start();
  const int N = 1e6 + 1;
  comb c(N);
  int x, y;
  cin >> x >> y;
  if ((x + y) % 3 != 0)
    return cout << 0, 0;
  if (min(x, y) - (x + y) / 3 < 0)
    return cout << 0, 0;
  cout << c((x + y) / 3, min(x, y) - (x + y) / 3);

  return 0;
}