prithivMLmods/Coder-Stat huggingface dataset:
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#define int int64_t
#define mp make_pair
#define f first
#define s second
#define mod 1000 * 1000 * 1000 + 7
int dp[1000001];
int power(int a, int b) {
  int ans = 1;
  while (b > 0) {
    if (b & 1) {
      ans *= a;
      ans %= mod;
      b--;
    }
    b /= 2;
    a *= a;
    a %= mod;
  }
  return ans;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  dp[0] = 1;
  for (int i = 1; i <= 1e6; i++) {
    dp[i] = dp[i - 1] * i;
    dp[i] %= mod;
  }
  int a, b;
  cin >> a >> b;
  if ((a + b) % 3 != 0 || max(a, b) > 2 * min(a, b)) {
    cout << "0\n";
  } else {
    int x = min(a, b);
    int y = max(a, b);
    int diff = 2 * x - y;
    diff /= 2;
    int moves = (x + y);
    moves /= 3;
    // cout<<moves<<" "<<diff<<"\n";
    diff = moves / 2;
    diff -= (y - x);
    int ans = dp[moves];
    ans *= power(dp[diff], mod - 2);
    ans %= mod;
    ans *= power(dp[moves - diff], mod - 2);
    ans %= mod;
    cout << ans << "\n";
  }
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define int int64_t
#define mp make_pair
#define f first
#define s second
#define mod 1000 * 1000 * 1000 + 7
int dp[1000001];
int power(int a, int b) {
  int ans = 1;
  while (b > 0) {
    if (b & 1) {
      ans *= a;
      ans %= mod;
      b--;
    }
    b /= 2;
    a *= a;
    a %= mod;
  }
  return ans;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  dp[0] = 1;
  for (int i = 1; i <= 1e6; i++) {
    dp[i] = dp[i - 1] * i;
    dp[i] %= mod;
  }
  int a, b;
  cin >> a >> b;
  if ((a + b) % 3 != 0 || max(a, b) > 2 * min(a, b)) {
    cout << "0\n";
  } else {
    int x = min(a, b);
    int y = max(a, b);
    int diff = 2 * x - y;
    diff /= 2;
    int moves = (x + y);
    moves /= 3;
    // cout<<moves<<" "<<diff<<"\n";
    diff = moves - (y - x);
    diff /= 2;
    int ans = dp[moves];
    ans *= power(dp[diff], mod - 2);
    ans %= mod;
    ans *= power(dp[moves - diff], mod - 2);
    ans %= mod;
    cout << ans << "\n";
  }
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int mod = 1000000007;

int frac(int n) {
  if (!n)
    return 1;
  int64_t ret = (int64_t)n * frac(n - 1);
  return ret % mod;
}
int modinv(int i) {
  int64_t x = 1, y = 0, z = 0, w = 1;
  int m = mod;
  while (m > 0) {
    int q = i / m, r = i % m;
    int64_t temp1 = x, temp2 = y;
    x = z;
    z = temp1 - q * z;
    y = w;
    w = temp2 - q * w;
    i = m;
    m = r;
  }
  while (x < 0)
    x += mod;
  return x;
}

int comb(int n, int i) {
  int a = frac(n), b = frac(i), c = frac(n - i);
  int d = (int64_t)b * c % mod;
  int ret = (int64_t)a * modinv(d) % mod;
  return ret;
}

int main() {
  int x, y;
  cin >> x >> y;
  if ((x + y) % 3) {
    cout << 0 << endl;
    return 0;
  }
  int m = (2 * x - y) / 3;
  int n = (2 * y - x) / 3;
  if (m < 0 || n < 0) {
    cout << 0 << endl;
  }
  cout << comb(m + n, m) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const int mod = 1000000007;

int frac(int n) {
  if (!n)
    return 1;
  int64_t ret = (int64_t)n * frac(n - 1);
  return ret % mod;
}
int modinv(int i) {
  int64_t x = 1, y = 0, z = 0, w = 1;
  int m = mod;
  while (m > 0) {
    int q = i / m, r = i % m;
    int64_t temp1 = x, temp2 = y;
    x = z;
    z = temp1 - q * z;
    y = w;
    w = temp2 - q * w;
    i = m;
    m = r;
  }
  while (x < 0)
    x += mod;
  return x;
}

int comb(int n, int i) {
  int a = frac(n), b = frac(i), c = frac(n - i);
  int d = (int64_t)b * c % mod;
  int ret = (int64_t)a * modinv(d) % mod;
  return ret;
}

int main() {
  int x, y;
  cin >> x >> y;
  if ((x + y) % 3) {
    cout << 0 << endl;
    return 0;
  }
  int m = (2 * x - y) / 3;
  int n = (2 * y - x) / 3;
  if (m < 0 || n < 0) {
    cout << 0 << endl;
    return 0;
  }
  cout << comb(m + n, m) << endl;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll fac[1000010];
ll binpow(ll a, ll b, ll MOD) {
  ll res = 1;
  a %= MOD;
  while (b > 0) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}
void cal_fac(ll n, ll MOD) {
  fac[0] = 1;
  for (ll i = 1; i <= n; i++) {
    fac[i] = fac[i - 1] * i;
    fac[i] %= MOD;
  }
}
ll nCr(ll n, ll r, ll MOD) {
  // MOD is a prime
  cal_fac(n, MOD);
  // n!*(r!)^-1*((n-r)!)^-1
  // inverse mod (a^-1 = a^(MOD-2) mod MOD
  return fac[n] * binpow(fac[n - r], MOD - 2, MOD) % MOD *
         binpow(fac[r], MOD - 2, MOD) % MOD;
}

int main() {
  ll x, y;
  cin >> x >> y;
  if ((x + y) % 3 == 0) {
    ll n = y - (x + y) / 3;
    ll m = x - (x + y) / 3;
    cout << nCr(n + m, n, 1000000007);
  } else
    cout << "0";
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll fac[1000010];
ll binpow(ll a, ll b, ll MOD) {
  ll res = 1;
  a %= MOD;
  while (b > 0) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}
void cal_fac(ll n, ll MOD) {
  fac[0] = 1;
  for (ll i = 1; i <= n; i++) {
    fac[i] = fac[i - 1] * i;
    fac[i] %= MOD;
  }
}
ll nCr(ll n, ll r, ll MOD) {
  // MOD is a prime
  cal_fac(n, MOD);
  // n!*(r!)^-1*((n-r)!)^-1
  // inverse mod (a^-1 = a^(MOD-2) mod MOD
  return fac[n] * binpow(fac[n - r], MOD - 2, MOD) % MOD *
         binpow(fac[r], MOD - 2, MOD) % MOD;
}

int main() {
  ll x, y;
  cin >> x >> y;
  if ((x + y) % 3 == 0) {
    ll n = y - (x + y) / 3;
    ll m = x - (x + y) / 3;
    if (n >= 0 && m >= 0)
      cout << nCr(n + m, n, 1000000007);
    else
      cout << "0";
  } else
    cout << "0";
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pull = pair<ull, ll>;
#define INF 1000000000
#define LLINF 10000000000000000ll
#define MOD 1000000007LL

bool dbgflag = false;

ll modPow(ll a, ll n, ll p) {
  if (n == 1)
    return a % p;
  if (n % 2 == 1)
    return (a * modPow(a, n - 1, p)) % p;
  ll t = modPow(a, n / 2, p);
  return (t * t) % p;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll x, y;
  cin >> x >> y;
  if (((2 * y - x) % 3 != 0) || ((2 * x - y) % 3 != 0)) {
    cout << 0 << endl;
    return 0;
  }

  ll a = (2 * y - x) / 3, b = (2 * x - y) / 3;
  ll n = a + b;
  vector<ll> fact(n + 1);     // 階乗
  vector<ll> fact_inv(n + 1); // 階乗の逆元
  fact[0] = 1;
  for (int i = 0; i < n; i++)
    fact[i + 1] = (fact[i] * (i + 1)) % MOD;
  fact_inv[n] = modPow(fact[n], MOD - 2, MOD);
  for (int i = n - 1; i >= 0; i--)
    fact_inv[i] = (fact_inv[i + 1] * (i + 1)) % MOD;

  auto comb = [&](ll n0, ll r) { // MODで
    return (((fact[n0] * fact_inv[n0 - r]) % MOD) * fact_inv[r]) % MOD;
  };

  cout << comb(a + b, a) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pull = pair<ull, ll>;
#define INF 1000000000
#define LLINF 10000000000000000ll
#define MOD 1000000007LL

bool dbgflag = false;

ll modPow(ll a, ll n, ll p) {
  if (n == 1)
    return a % p;
  if (n % 2 == 1)
    return (a * modPow(a, n - 1, p)) % p;
  ll t = modPow(a, n / 2, p);
  return (t * t) % p;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll x, y;
  cin >> x >> y;
  if (((2 * y - x) % 3 != 0) || ((2 * x - y) % 3 != 0)) {
    cout << 0 << endl;
    return 0;
  }

  ll a = (2 * y - x) / 3, b = (2 * x - y) / 3;
  if (!((a >= 0) && (b >= 0))) {
    cout << 0 << endl;
    return 0;
  }
  ll n = a + b;
  vector<ll> fact(n + 1);     // 階乗
  vector<ll> fact_inv(n + 1); // 階乗の逆元
  fact[0] = 1;
  for (int i = 0; i < n; i++)
    fact[i + 1] = (fact[i] * (i + 1)) % MOD;
  fact_inv[n] = modPow(fact[n], MOD - 2, MOD);
  for (int i = n - 1; i >= 0; i--)
    fact_inv[i] = (fact_inv[i + 1] * (i + 1)) % MOD;

  auto comb = [&](ll n0, ll r) { // MODで
    return (((fact[n0] * fact_inv[n0 - r]) % MOD) * fact_inv[r]) % MOD;
  };

  cout << comb(a + b, a) << endl;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define fi first
#define se second
#define mp make_pair
#define pb push_back

using namespace std;
typedef long long ll;

const int maxn = 1e6 + 5;
const int mod = 1e9 + 7;

int fact[maxn], ifact[maxn];

void addmod(int &a, int b) {
  a += b;
  if (a >= mod)
    a -= mod;
  if (a < 0)
    a += mod;
}

int power(int a, int b) {
  if (b == 0)
    return 1;
  int res = power(a, b / 2);
  if (b & 1)
    return 1ll * res * res % mod * a % mod;
  return 1ll * res * res % mod;
}

int nCk(int n, int k) {
  if (k > n)
    return 0;
  return 1ll * fact[n] * ifact[k] % mod * ifact[n - k] % mod;
}

void precalc(void) {
  fact[0] = 1;
  for (int i = 1; i <= maxn - 5; ++i)
    fact[i] = 1ll * fact[i - 1] * i % mod;
  ifact[maxn - 5] = power(fact[maxn - 5], mod - 2);
  for (int i = maxn - 6; i >= 0; --i)
    ifact[i] = 1ll * ifact[i + 1] * (i + 1) % mod;
}

signed main(void) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  precalc();
  int x, y;
  cin >> x >> y;
  if ((x + y) % 3) {
    cout << 0;
    return 0;
  }
  int m = x - (x + y) / 3;
  int n = x - 2 * m;
  cout << nCk(n + m, n) << '\n';
}


Fixed submission:
#include <bits/stdc++.h>

#define fi first
#define se second
#define mp make_pair
#define pb push_back

using namespace std;
typedef long long ll;

const int maxn = 1e6 + 5;
const int mod = 1e9 + 7;

int fact[maxn], ifact[maxn];

void addmod(int &a, int b) {
  a += b;
  if (a >= mod)
    a -= mod;
  if (a < 0)
    a += mod;
}

int power(int a, int b) {
  if (b == 0)
    return 1;
  int res = power(a, b / 2);
  if (b & 1)
    return 1ll * res * res % mod * a % mod;
  return 1ll * res * res % mod;
}

int nCk(int n, int k) {
  if (k > n)
    return 0;
  return 1ll * fact[n] * ifact[k] % mod * ifact[n - k] % mod;
}

void precalc(void) {
  fact[0] = 1;
  for (int i = 1; i <= maxn - 5; ++i)
    fact[i] = 1ll * fact[i - 1] * i % mod;
  ifact[maxn - 5] = power(fact[maxn - 5], mod - 2);
  for (int i = maxn - 6; i >= 0; --i)
    ifact[i] = 1ll * ifact[i + 1] * (i + 1) % mod;
}

signed main(void) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  precalc();
  int x, y;
  cin >> x >> y;
  if ((x + y) % 3) {
    cout << 0;
    return 0;
  }
  int m = x - (x + y) / 3;
  int n = x - 2 * m;
  if (n < 0 || m < 0) {
    cout << 0;
    return 0;
  }
  cout << nCk(n + m, n) << '\n';
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
// Author: πα

#include <bits/stdc++.h>
using namespace std;
#define long int64_t
const int MOD = 1000000007;

long modexp(long x, long y, long mod = MOD) {
  long res = 1;
  x = (x % mod + mod) % mod;
  while (y) {
    if (y & 1)
      res = (res * x) % mod;
    y >>= 1;
    x = (x * x) % mod;
  }
  return res;
}

int main() {
  int x, y;
  cin >> x >> y;
  if ((x + y) % 3)
    return cout << 0, 0;
  int n = (x + y) / 3;
  int r = (n - x + y) / 2;
  vector<int> f(n + 1);
  f[0] = 1;
  for (long i = 1; i <= n; ++i)
    f[i] = i * f[i - 1] % MOD;
  long ans = f[n];
  ans *= modexp(f[r], MOD - 2), ans %= MOD;
  ans *= modexp(f[n - r], MOD - 2), ans %= MOD;
  cout << ans;
  return 0;
}


Fixed submission:
// Author: πα

#include <bits/stdc++.h>
using namespace std;
#define long int64_t
const int MOD = 1000000007;

long modexp(long x, long y, long mod = MOD) {
  long res = 1;
  x = (x % mod + mod) % mod;
  while (y) {
    if (y & 1)
      res = (res * x) % mod;
    y >>= 1;
    x = (x * x) % mod;
  }
  return res;
}

int main() {
  int x, y;
  cin >> x >> y;
  if ((x + y) % 3)
    return cout << 0, 0;
  int n = (x + y) / 3;
  int r = (n - x + y) / 2;
  if (r < 0 or r > n)
    return cout << 0, 0;
  vector<long> f(n + 1);
  f[0] = 1;
  for (long i = 1; i <= n; ++i)
    f[i] = i * f[i - 1] % MOD;
  long ans = f[n];
  ans *= modexp(f[r], MOD - 2), ans %= MOD;
  ans *= modexp(f[n - r], MOD - 2), ans %= MOD;
  cout << ans;
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define MOD 1000000007

long long int dizi[10000005];

long long int hizlius(long long int a, long long int b) {
  if (b == 0) {
    return 1;
  }
  if (b == 1) {
    return a;
  }
  long long int kl = hizlius(a, b / 2);
  if (b % 2 == 0) {
    return (kl * kl) % MOD;
  }
  if (b % 2 == 1) {
    return (((kl * kl) % MOD) * a) % MOD;
  }
}

long long int com(long long int a, long long int b) {
  long long int c = b;
  long long int d = a - b;
  long long int reelc = hizlius(dizi[c], MOD - 2);
  long long int reeld = hizlius(dizi[d], MOD - 2);
  return (((dizi[a] * reelc) % MOD) * reeld) % MOD;
}

int main() {
  long long int a, b;
  cin >> a >> b;
  long long int x, y;
  x = (2 * a - b) / 3;
  y = (2 * b - a) / 3;
  long long int ls = max(x + y, b);
  dizi[0] = 1;
  for (long long int i = 1; i < x + y + 2; i++) {
    dizi[i] = (dizi[i - 1] * i) % MOD;
  }
  if ((a + b) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  cout << com(x + y, y);
  // x+y
  //  y
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define MOD 1000000007

long long int dizi[10000005];

long long int hizlius(long long int a, long long int b) {
  if (b == 0) {
    return 1;
  }
  if (b == 1) {
    return a;
  }
  long long int kl = hizlius(a, b / 2);
  if (b % 2 == 0) {
    return (kl * kl) % MOD;
  }
  if (b % 2 == 1) {
    return (((kl * kl) % MOD) * a) % MOD;
  }
}

long long int com(long long int a, long long int b) {
  long long int c = b;
  long long int d = a - b;
  long long int reelc = hizlius(dizi[c], MOD - 2);
  long long int reeld = hizlius(dizi[d], MOD - 2);
  return (((dizi[a] * reelc) % MOD) * reeld) % MOD;
}

int main() {
  long long int a, b;
  cin >> a >> b;
  long long int x, y;
  x = (2 * a - b) / 3;
  y = (2 * b - a) / 3;
  long long int ls = max(x + y, b);
  dizi[0] = 1;
  for (long long int i = 1; i < x + y + 2; i++) {
    dizi[i] = (dizi[i - 1] * i) % MOD;
  }
  if (max(a, b) / 2 > min(a, b)) {
    cout << 0;
    return 0;
  }
  if ((a + b) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  cout << com(x + y, y);
  // x+y
  //  y
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
long long inv[3000001];
long long fac[3000001];
long long facinv[3000001];

int main() {
  int x, y;
  cin >> x >> y;
  if ((2 * y - x) % 3 != 0 || (2 * x - y) % 3 != 0) {
    cout << 0 << endl;
  } else {
    int a = (2 * y - x) / 3;
    int b = (2 * x - y) / 3;

    inv[0] = 1;
    inv[1] = 1;
    for (int i = 2; i < 3000001; ++i) {
      inv[i] = (mod - mod / i) * 1LL * inv[mod % i] % mod;
    }

    fac[0] = 1;
    fac[1] = 1;
    for (int i = 2; i < 3000001; ++i) {
      fac[i] = fac[i - 1] * 1LL * i % mod;
    }

    facinv[0] = 1;
    facinv[1] = 1;
    for (int i = 2; i < 3000001; ++i) {
      facinv[i] = facinv[i - 1] * 1LL * inv[i] % mod;
    }

    int res = ((1LL * fac[a + b] * facinv[b]) % mod * facinv[a]) % mod;
    cout << res << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
long long inv[3000001];
long long fac[3000001];
long long facinv[3000001];

int main() {
  int x, y;
  cin >> x >> y;
  if ((2 * y - x) % 3 != 0 || (2 * x - y) % 3 != 0) {
    cout << 0 << endl;
  } else {
    int a = (2 * y - x) / 3;
    int b = (2 * x - y) / 3;

    if (a < 0 || b < 0) {
      cout << 0 << endl;
      return 0;
    }

    inv[0] = 1;
    inv[1] = 1;
    for (int i = 2; i < 3000001; ++i) {
      inv[i] = (mod - mod / i) * 1LL * inv[mod % i] % mod;
    }

    fac[0] = 1;
    fac[1] = 1;
    for (int i = 2; i < 3000001; ++i) {
      fac[i] = fac[i - 1] * 1LL * i % mod;
    }

    facinv[0] = 1;
    facinv[1] = 1;
    for (int i = 2; i < 3000001; ++i) {
      facinv[i] = facinv[i - 1] * 1LL * inv[i] % mod;
    }

    int res = ((1LL * fac[a + b] * facinv[b]) % mod * facinv[a]) % mod;
    cout << res << endl;
  }

  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define debug(x) cout << #x << " " << x << endl;

const int P = 1e9 + 7;
const int N = 1e6 + 5;
const double eps = 1e-9;
const int INF = 1e18;

int X, Y;
int fac[N], I[N], inv[N];

void Init() {
  fac[0] = I[0] = inv[0] = 1, fac[1] = I[1] = inv[1] = 1;
  for (int i = 2; i <= 1000000; i++) {
    fac[i] = fac[i - 1] * i % P;
    I[i] = (P - P / i) * I[P % i] % P;
    inv[i] = inv[i - 1] * I[i] % P;
  }
}

int C(int n, int m) {
  if (n < m)
    return 0;
  if (n < P && m < P)
    return fac[n] * inv[m] % P * inv[n - m] % P;
  return C(n / P, m / P) * C(n % P, m % P) % P;
}

signed main() {
  // freopen("a.in","r",stdin);
  // freopen("a.out","w",stdout);
  cin >> X >> Y; // X++;Y++;
  if ((X + Y) % 3) {
    puts("0");
    return 0;
  }
  int tmp = (X + Y) / 3;
  X -= tmp, Y -= tmp;
  int ans = 0;
  Init();
  ans = C(X + Y, Y);
  cout << ans << endl;
  return 0;
}

// Created Time  :2019/11/16 20:18:34

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define debug(x) cout << #x << " " << x << endl;

const int P = 1e9 + 7;
const int N = 1e6 + 5;
const double eps = 1e-9;
const int INF = 1e18;

int X, Y;
int fac[N], I[N], inv[N];

void Init() {
  fac[0] = I[0] = inv[0] = 1, fac[1] = I[1] = inv[1] = 1;
  for (int i = 2; i <= 1000000; i++) {
    fac[i] = fac[i - 1] * i % P;
    I[i] = (P - P / i) * I[P % i] % P;
    inv[i] = inv[i - 1] * I[i] % P;
  }
}

int C(int n, int m) {
  if (n < m)
    return 0;
  if (n < P && m < P)
    return fac[n] * inv[m] % P * inv[n - m] % P;
  return C(n / P, m / P) * C(n % P, m % P) % P;
}

signed main() {
  // freopen("a.in","r",stdin);
  // freopen("a.out","w",stdout);
  cin >> X >> Y; // X++;Y++;
  if ((X + Y) % 3) {
    puts("0");
    return 0;
  }
  int tmp = (X + Y) / 3;
  X -= tmp, Y -= tmp;
  int ans = 0;
  Init();
  if (X < 0 || Y < 0) {
    puts("0");
    return 0;
  }
  ans = C(X + Y, Y);
  cout << ans << endl;
  return 0;
}

// Created Time  :2019/11/16 20:18:34
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<ll> vll;
const long long mod = 1000000007LL;

ll fac[1100000];
ll finv[1100000];
ll inv[1100000];

void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < 10000000; i++) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}

long long COM(long long n, long long k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}

ll modpow(ll n, ll p, ll m) {
  if (p == 0)
    return 1LL;
  if (p % 2 == 0) {
    ll t = modpow(n, p / 2, m);
    return t * t % m;
  }
  return n * modpow(n, p - 1, m) % m;
}

int main() {
  ll x, y;
  cin >> x >> y;
  if ((x + y) % 3 != 0 || x > 2 * y || y > 2 * x) {
    cout << 0 << endl;
    return 0;
  }
  ll n = (x + y) / 3;
  ll k = 2 * n - x;
  COMinit();
  cout << COM(n, k) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<ll> vll;
const long long mod = 1000000007LL;

ll fac[1100000];
ll finv[1100000];
ll inv[1100000];

void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < 1000000; i++) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}

long long COM(long long n, long long k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}

ll modpow(ll n, ll p, ll m) {
  if (p == 0)
    return 1LL;
  if (p % 2 == 0) {
    ll t = modpow(n, p / 2, m);
    return t * t % m;
  }
  return n * modpow(n, p - 1, m) % m;
}

int main() {
  ll x, y;
  cin >> x >> y;
  if ((x + y) % 3 != 0 || x > 2 * y || y > 2 * x) {
    cout << 0 << endl;
    return 0;
  }
  ll n = (x + y) / 3;
  ll k = 2 * n - x;
  COMinit();
  cout << COM(n, k) << endl;
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
typedef pair<ll, ll> P;
#define MOD 1000000007
const int MAX = 510000;
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
ll COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {

  int x, y;
  cin >> x >> y;
  string s;
  cin >> s;
  if ((x + y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  // 連立方程式からのa,bを求める
  int m = (2 * x - y) / 3;
  int n = (y - m) / 2;
  int c = (n + m);
  COMinit();
  ll count = COM(c, n);

  cout << count << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
typedef pair<ll, ll> P;
const int MOD = 1000000007;
const int MAX = 5100000;
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
ll COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {

  int x, y;
  cin >> x >> y;
  string s;
  cin >> s;
  if ((x + y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  // 連立方程式からのa,bを求める
  int m = (2 * x - y) / 3;
  int n = (y - m) / 2;
  int c = (n + m);
  COMinit();
  ll count = COM(c, n);

  cout << count << endl;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <limits>
#include <numeric>
#include <queue>
#include <regex>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;

int main() {
  ll X, Y, W, H;
  ll p = 1000000007;
  vector<ll> fac, ifac;

  cin >> X >> Y;
  if ((2 * X - Y) % 3 == 0 && (2 * Y - X) % 3 == 0) {
    W = (2 * X - Y) / 3;
    H = (2 * Y - X) / 3;
  } else {
    cout << 0 << endl;
    return 0;
  }

  // calculate factorial and inverse factorial
  fac.push_back(1);
  for (ll i = 1; i <= H + W; i++) {
    ll t = fac[i - 1];
    t *= i;
    fac.push_back(t % p);
  }
  ll fac_t = fac[H + W];
  ll ifac_max = 1;
  for (int i = 0; ((p - 2) >> i) > 0; i++) {
    if (((p - 2) >> i) & 1) {
      ifac_max *= fac_t;
      ifac_max %= p;
    }
    fac_t *= fac_t;
    fac_t %= p;
  }
  ifac.push_back(ifac_max);

  for (int i = 1; i <= H + W; i++) {
    ll t = ifac[i - 1];
    t *= H + W - i + 1;
    ifac.push_back(t % p);
  }
  reverse(ifac.begin(), ifac.end());

  ll ans = fac[W + H];
  ans = (ans * ifac[W]) % p;
  ans = (ans * ifac[H]) % p;
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <limits>
#include <numeric>
#include <queue>
#include <regex>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;

int main() {
  ll X, Y, W, H;
  ll p = 1000000007;
  vector<ll> fac, ifac;

  cin >> X >> Y;
  if ((2 * X - Y) % 3 == 0 && (2 * Y - X) % 3 == 0) {
    W = (2 * X - Y) / 3;
    H = (2 * Y - X) / 3;
  } else {
    cout << 0 << endl;
    return 0;
  }
  if (W < 0 || H < 0) {
    cout << 0 << endl;
    return 0;
  }

  // calculate factorial and inverse factorial
  fac.push_back(1);
  for (ll i = 1; i <= H + W; i++) {
    ll t = fac[i - 1];
    t *= i;
    fac.push_back(t % p);
  }
  ll fac_t = fac[H + W];
  ll ifac_max = 1;
  for (int i = 0; ((p - 2) >> i) > 0; i++) {
    if (((p - 2) >> i) & 1) {
      ifac_max *= fac_t;
      ifac_max %= p;
    }
    fac_t *= fac_t;
    fac_t %= p;
  }
  ifac.push_back(ifac_max);

  for (int i = 1; i <= H + W; i++) {
    ll t = ifac[i - 1];
    t *= H + W - i + 1;
    ifac.push_back(t % p);
  }
  reverse(ifac.begin(), ifac.end());

  ll ans = fac[W + H];
  ans = (ans * ifac[W]) % p;
  ans = (ans * ifac[H]) % p;
  cout << ans << endl;

  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

const int mod = 1e9 + 7;

int add(int a, int b) {
  int s = a + b;
  if (s >= mod)
    s -= mod;
  return s;
}

int mul(int a, int b) { return (long long)a * b % mod; }

int pow(int b, int e) {
  int r = 1;
  while (e > 0) {
    if (e & 1) {
      r = mul(r, b);
    }
    b = mul(b, b);
    e /= 2;
  }
  return r;
}

int divm(int a, int b) { return mul(a, pow(b, mod - 2)); }

int fac[1000004];

int C(int n, int k) { return divm(fac[n], mul(fac[k], fac[n - k])); }

int main() {
  int x, y;
  scanf("%d%d", &x, &y);

  fac[0] = 1;
  for (int i = 1; i <= x; ++i) {
    fac[i] = mul(fac[i - 1], i);
  }

  int k = (x + y) / 3;
  printf("%d\n", (k * 3 == (x + y)) ? C(k, x - k) : 0);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int mod = 1e9 + 7;

int add(int a, int b) {
  int s = a + b;
  if (s >= mod)
    s -= mod;
  return s;
}

int mul(int a, int b) { return (long long)a * b % mod; }

int pow(int b, int e) {
  int r = 1;
  while (e > 0) {
    if (e & 1) {
      r = mul(r, b);
    }
    b = mul(b, b);
    e /= 2;
  }
  return r;
}

int divm(int a, int b) { return mul(a, pow(b, mod - 2)); }

int fac[1000004];

int C(int n, int k) { return divm(fac[n], mul(fac[k], fac[n - k])); }

int main() {
  int x, y;
  scanf("%d%d", &x, &y);

  fac[0] = 1;
  for (int i = 1; i <= x; ++i) {
    fac[i] = mul(fac[i - 1], i);
  }

  int k = (x + y) / 3;
  printf("%d\n", (k * 3 == (x + y) && x >= k && y >= k) ? C(k, x - k) : 0);
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define all(x) begin(x), end(x)
#define dbg(x) cerr << #x << " = " << x << endl
#define _ << ' ' <<
using namespace std;
using ll = long long;
using vi = vector<int>;

const int mod = 1000000007;
int add(int a, int b) { return (a += b) < mod ? a : a - mod; }
int sub(int a, int b) { return (a -= b) >= 0 ? a : a + mod; }
int mul(int a, int b) { return 1LL * a * b % mod; }
void adds(int &a, int b) { a = add(a, b); }
void subs(int &a, int b) { a = sub(a, b); }
void muls(int &a, int b) { a = mul(a, b); }
void maxs(int &a, int b) { a = max(a, b); }
void mins(int &a, int b) { a = min(a, b); }
int pwr(int a, ll p) {
  if (p == 0)
    return 1;
  if (p & 1)
    return mul(a, pwr(a, p - 1));
  return pwr(mul(a, a), p / 2);
}
int inv(int a) { return pwr(a, mod - 2); }
const int maxn = 4000005;
int f[maxn + 1], fi[maxn + 1];
int ncr(int n, int r) { return mul(f[n], mul(fi[r], fi[n - r])); }

void precompute() {
  f[0] = 1;
  for (int i = 1; i <= maxn; ++i)
    f[i] = mul(f[i - 1], i);

  fi[maxn] = inv(f[maxn]);
  for (int i = maxn - 1; i >= 0; --i)
    fi[i] = mul(fi[i + 1], i + 1);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  precompute();
  int x, y;
  cin >> x >> y;
  if ((2 * y - x) % 3)
    return cout << 0, 0;
  int b = (2 * y - x) / 3;
  int a = y - 2 * b;
  cout << ncr(a + b, a);
}


Fixed submission:
#include <bits/stdc++.h>
#define all(x) begin(x), end(x)
#define dbg(x) cerr << #x << " = " << x << endl
#define _ << ' ' <<
using namespace std;
using ll = long long;
using vi = vector<int>;

const int mod = 1000000007;
int add(int a, int b) { return (a += b) < mod ? a : a - mod; }
int sub(int a, int b) { return (a -= b) >= 0 ? a : a + mod; }
int mul(int a, int b) { return 1LL * a * b % mod; }
void adds(int &a, int b) { a = add(a, b); }
void subs(int &a, int b) { a = sub(a, b); }
void muls(int &a, int b) { a = mul(a, b); }
void maxs(int &a, int b) { a = max(a, b); }
void mins(int &a, int b) { a = min(a, b); }
int pwr(int a, ll p) {
  if (p == 0)
    return 1;
  if (p & 1)
    return mul(a, pwr(a, p - 1));
  return pwr(mul(a, a), p / 2);
}
int inv(int a) { return pwr(a, mod - 2); }
const int maxn = 4000005;
int f[maxn + 1], fi[maxn + 1];
int ncr(int n, int r) { return mul(f[n], mul(fi[r], fi[n - r])); }

void precompute() {
  f[0] = 1;
  for (int i = 1; i <= maxn; ++i)
    f[i] = mul(f[i - 1], i);

  fi[maxn] = inv(f[maxn]);
  for (int i = maxn - 1; i >= 0; --i)
    fi[i] = mul(fi[i + 1], i + 1);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  precompute();
  int x, y;
  cin >> x >> y;
  if ((2 * y - x) % 3)
    return cout << 0, 0;
  int b = (2 * y - x) / 3;
  int a = y - 2 * b;
  if (b < 0 || a < 0)
    return cout << 0, 0;
  cout << ncr(a + b, a);
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MM = 1000000000;
ll mod = MM + 7;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define Rep(i, j, n) for (int i = j; i < n; i++)
#define all(vec) vec.begin(), vec.end()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const ll INF = 1LL << 60;

ll fact[800000], fact_inv[800000];

ll power(ll a, ll n) { // 繰り返し二乗法
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

ll comb(ll n, ll r) {
  return fact[n] * fact_inv[r] % mod * fact_inv[n - r] % mod;
}

int main() {
  ll x, y;
  cin >> x >> y;
  if ((x + y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  ll a = (2 * x - y) / 3;
  ll b = (2 * y - x) / 3;

  ll n = 700000;

  fact[0] = 1;
  for (ll i = 0; i < n; i++)
    fact[i + 1] = fact[i] * (i + 1) % mod;

  fact_inv[n] = power(fact[n], mod - 2);

  for (ll i = n - 1; i >= 0; i--)
    fact_inv[i] = fact_inv[i + 1] * (i + 1) % mod;

  cout << comb(a + b, b) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MM = 1000000000;
ll mod = MM + 7;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define Rep(i, j, n) for (int i = j; i < n; i++)
#define all(vec) vec.begin(), vec.end()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const ll INF = 1LL << 60;

ll fact[800000], fact_inv[800000];

ll power(ll a, ll n) { // 繰り返し二乗法
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

ll comb(ll n, ll r) {
  if (n < r)
    return 0;
  if (n < 0 || r < 0)
    return 0;
  return fact[n] * fact_inv[r] % mod * fact_inv[n - r] % mod;
}

int main() {
  ll x, y;
  cin >> x >> y;
  if ((x + y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  ll a = (2 * x - y) / 3;
  ll b = (2 * y - x) / 3;

  ll n = 700000;

  fact[0] = 1;
  for (ll i = 0; i < n; i++)
    fact[i + 1] = fact[i] * (i + 1) % mod;

  fact_inv[n] = power(fact[n], mod - 2);

  for (ll i = n - 1; i >= 0; i--)
    fact_inv[i] = fact_inv[i + 1] * (i + 1) % mod;

  cout << comb(a + b, b) << endl;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod = 1e9 + 7;
ll x, y, a, b, fact[2000010];
ll power(ll a, ll b = mod - 2) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}
void init(ll n) {
  fact[0] = 1;
  for (ll i = 1; i <= n; ++i)
    fact[i] = fact[i - 1] * i % mod;
}
ll C(ll n, ll m) {
  return fact[n] * power(fact[m]) % mod * power(fact[n - m]) % mod;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> x >> y;
  b = (2 * y - x) / 3;
  a = (2 * x - y) / 3;
  init(a + b);
  if (2 * a + b != x || 2 * b + a != y)
    cout << 0 << endl;
  else
    cout << C(a + b, a) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod = 1e9 + 7;
ll x, y, a, b, fact[2000010];
ll power(ll a, ll b = mod - 2) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}
void init(ll n) {
  fact[0] = 1;
  for (ll i = 1; i <= n; ++i)
    fact[i] = fact[i - 1] * i % mod;
}
ll C(ll n, ll m) {
  return fact[n] * power(fact[m]) % mod * power(fact[n - m]) % mod;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> x >> y;
  b = (2 * y - x) / 3;
  a = (2 * x - y) / 3;
  init(a + b);
  if (2 * a + b != x || 2 * b + a != y || a < 0 || b < 0)
    cout << 0 << endl;
  else
    cout << C(a + b, a) << endl;
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (ll)n; ++i)
using P = pair<ll, ll>;

const ll MOD = 1000000007;

// nのcnt乗
ll pow2(ll n, ll cnt) {
  if (cnt == 0)
    return 1;
  if (cnt == 1)
    return n;
  ll tmp = pow2(n, cnt / 2);
  if (cnt % 2 == 0)
    return tmp % MOD * tmp % MOD;
  else
    return tmp % MOD * n % MOD * tmp % MOD;
}

// nから(n-cnt+1)までの積
ll fac(ll n, ll cnt) {
  if (cnt == 0)
    return 1;
  if (cnt == 1)
    return n;
  return fac(n - 1, cnt - 1) % MOD * n % MOD;
}

int main() {
  ll x, y;
  cin >> x >> y;

  if ((2 * x - y) < 0 || (2 * x - y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  ll t = (2 * x - y) / 3;
  ll s = x - 2 * t;

  ll n = s + t;

  // nCs mod MOD を求める
  ll m = min(s, t);
  ll denominator = fac(m, m) % MOD;

  ll ans = fac(n, m) * pow2(denominator, MOD - 2);
  ans %= MOD;

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (ll)n; ++i)
using P = pair<ll, ll>;

const ll MOD = 1000000007;

// nのcnt乗
ll pow2(ll n, ll cnt) {
  if (cnt == 0)
    return 1;
  if (cnt == 1)
    return n;
  ll tmp = pow2(n, cnt / 2);
  if (cnt % 2 == 0)
    return tmp % MOD * tmp % MOD;
  else
    return tmp % MOD * n % MOD * tmp % MOD;
}

// nから(n-cnt+1)までの積
ll fac(ll n, ll cnt) {
  if (cnt == 0)
    return 1;
  if (cnt == 1)
    return n;
  return fac(n - 1, cnt - 1) % MOD * n % MOD;
}

int main() {
  ll x, y;
  cin >> x >> y;

  if ((2 * x - y) < 0 || (2 * x - y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  ll t = (2 * x - y) / 3;
  ll s = x - 2 * t;

  ll n = s + t;

  // nCs mod MOD を求める
  ll m = min(s, t);
  if (m < 0) {
    cout << 0 << endl;
    return 0;
  }
  ll denominator = fac(m, m) % MOD;

  ll ans = fac(n, m) * pow2(denominator, MOD - 2);
  ans %= MOD;

  cout << ans << endl;
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
#define MEMSET(v, h) memset((v), h, sizeof(v))
typedef long long ll;

using namespace std;

const int MAX = 51000000;
const int MOD = 1000000007;

static long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main(void) {
  ll X, Y;
  COMinit();
  cin >> X >> Y;
  if ((2 * Y - X) < 0 || ((2 * Y - X) % 3 != 0) || (2 * X - Y) < 0 ||
      ((2 * X - Y) % 3 != 0)) {
    cout << 0 << endl;
  } else {
    ll k = (2 * Y - X) / 3;
    ll l = (2 * X - Y) / 3;
    // cout<<k <<" " << l <<endl;
    cout << COM(k + l, k) << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
#define MEMSET(v, h) memset((v), h, sizeof(v))
typedef long long ll;

using namespace std;

const int MAX = 5100000;
const int MOD = 1000000007;

static long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main(void) {
  ll X, Y;
  COMinit();
  cin >> X >> Y;
  if ((2 * Y - X) < 0 || ((2 * Y - X) % 3 != 0) || (2 * X - Y) < 0 ||
      ((2 * X - Y) % 3 != 0)) {
    cout << 0 << endl;
  } else {
    ll k = (2 * Y - X) / 3;
    ll l = (2 * X - Y) / 3;
    // cout<<k <<" " << l <<endl;
    cout << COM(k + l, k) << endl;
  }
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<pi, pi> pp;
typedef pair<ll, ll> pl;
double PI = 3.141592653589793238462643383279;
const double EPS = 1e-9;
const ll MOD = 1000000007;
const int inf = 1 << 30;
const ll linf = 1LL << 60;

struct CONBINATION {
  vector<ll> kai, mokai;

  ll mod_pow(ll x, ll y) {
    ll ret = 1;
    while (y) {
      if (y & 1)
        ret = ret * x % MOD;
      x = x * x % MOD;
      y /= 2;
    }
    return ret;
  }

  void init(int _n) {
    kai.assign(_n + 1, 0);
    mokai.assign(_n + 1, 0);
    kai[0] = 1;
    for (int i = 1; i <= _n; i++)
      kai[i] = kai[i - 1] * i % MOD;
    for (int i = 0; i <= _n; i++)
      mokai[i] = mod_pow(kai[i], MOD - 2);
  }

  ll conb(ll x, ll y) {
    ll z = x - y;
    ll ret = kai[x] * mokai[y] % MOD;
    ret = ret * mokai[z] % MOD;
    return ret;
  }
} conb;

ll X, Y;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> X >> Y;
  if ((X + Y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  ll n = (X + Y) / 3;
  X -= n;
  Y -= n;
  conb.init(2000000);
  cout << conb.conb(n, X) << endl;
}

Fixed submission:
#include <algorithm>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<pi, pi> pp;
typedef pair<ll, ll> pl;
double PI = 3.141592653589793238462643383279;
const double EPS = 1e-9;
const ll MOD = 1000000007;
const int inf = 1 << 30;
const ll linf = 1LL << 60;

struct CONBINATION {
  vector<ll> kai, mokai;

  ll mod_pow(ll x, ll y) {
    ll ret = 1;
    while (y) {
      if (y & 1)
        ret = ret * x % MOD;
      x = x * x % MOD;
      y /= 2;
    }
    return ret;
  }

  void init(int _n) {
    kai.assign(_n + 1, 0);
    mokai.assign(_n + 1, 0);
    kai[0] = 1;
    for (int i = 1; i <= _n; i++)
      kai[i] = kai[i - 1] * i % MOD;
    for (int i = 0; i <= _n; i++)
      mokai[i] = mod_pow(kai[i], MOD - 2);
  }

  ll conb(ll x, ll y) {
    ll z = x - y;
    ll ret = kai[x] * mokai[y] % MOD;
    ret = ret * mokai[z] % MOD;
    return ret;
  }
} conb;

ll X, Y;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> X >> Y;
  if ((X + Y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  ll n = (X + Y) / 3;
  X -= n;
  Y -= n;
  if (X < 0 || Y < 0) {
    cout << 0 << endl;
    return 0;
  }
  conb.init(2000000);
  cout << conb.conb(n, X) << endl;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main() {
  ll i, j;
  ll x, y;
  cin >> x >> y;
  if ((x + y) % 3) {
    cout << 0 << endl;
    return 0;
  }

  ll max_number = (x + y) / 3;
  int MOD = 1e9 + 7;
  ll l = x - max_number;

  vector<ll> fac(max_number + 1, 1);
  vector<ll> inv(max_number + 1, 1);
  vector<ll> finv(max_number + 1, 1);
  vector<ll> nCk(max_number + 1, 1);
  for (i = 2; i <= max_number; ++i) {
    inv.at(i) = MOD - inv.at(MOD % i) * (MOD / i) % MOD;
  }
  for (i = 1; i <= max_number; ++i) {
    fac.at(i) = fac.at(i - 1) * i % MOD;
    finv.at(i) = finv.at(i - 1) * inv.at(i) % MOD;
  }
  for (i = 1; i <= max_number; ++i) {
    nCk.at(i) =
        fac.at(max_number) * finv.at(i) % MOD * finv.at(max_number - i) % MOD;
  }

  cout << nCk.at(l) << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main() {
  ll i, j;
  ll x, y;
  cin >> x >> y;
  if ((x + y) % 3) {
    cout << 0 << endl;
    return 0;
  }

  ll max_number = (x + y) / 3;
  int MOD = 1e9 + 7;
  ll l = x - max_number;
  ll k = y - max_number;
  if (l < 0 || k < 0) {
    cout << 0 << endl;
    return 0;
  }
  vector<ll> fac(max_number + 1, 1);
  vector<ll> inv(max_number + 1, 1);
  vector<ll> finv(max_number + 1, 1);
  vector<ll> nCk(max_number + 1, 1);
  for (i = 2; i <= max_number; ++i) {
    inv.at(i) = MOD - inv.at(MOD % i) * (MOD / i) % MOD;
  }
  for (i = 1; i <= max_number; ++i) {
    fac.at(i) = fac.at(i - 1) * i % MOD;
    finv.at(i) = finv.at(i - 1) * inv.at(i) % MOD;
  }
  for (i = 1; i <= max_number; ++i) {
    nCk.at(i) =
        fac.at(max_number) * finv.at(i) % MOD * finv.at(max_number - i) % MOD;
  }

  cout << nCk.at(l) << endl;

  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, j, n) for (int i = (int)(j); i < (int)(n); i++)
#define REP(i, j, n) for (int i = (int)(j); i <= (int)(n); i++)
#define MOD 1000000007
#define int long long
#define ALL(a) (a).begin(), (a).end()
#define vi vector<int>
#define vii vector<vi>
#define pii pair<int, int>
#define priq priority_queue<int>
#define disup(A, key) distance(A.begin(), upper_bound(ALL(A), (int)(key)))
#define dislow(A, key) distance(A.begin(), lower_bound(ALL(A), (int)(key)))
#define tii tuple<int, int, int>
const int MAX = 510000;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

signed main() {
  int X, Y;
  cin >> X >> Y;
  int ans = 0;
  COMinit();
  if ((X + Y) % 3 == 0 && 2 * Y >= X && 2 * X >= Y) {
    int A = (2 * Y - X) / 3;
    int B = (X - A) / 2;
    ans = COM(A + B, A);
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, j, n) for (int i = (int)(j); i < (int)(n); i++)
#define REP(i, j, n) for (int i = (int)(j); i <= (int)(n); i++)
#define MOD 1000000007
#define int long long
#define ALL(a) (a).begin(), (a).end()
#define vi vector<int>
#define vii vector<vi>
#define pii pair<int, int>
#define priq priority_queue<int>
#define disup(A, key) distance(A.begin(), upper_bound(ALL(A), (int)(key)))
#define dislow(A, key) distance(A.begin(), lower_bound(ALL(A), (int)(key)))
#define tii tuple<int, int, int>
const int MAX = 1000001;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

signed main() {
  int X, Y;
  cin >> X >> Y;
  int ans = 0;
  COMinit();
  if ((X + Y) % 3 == 0 && 2 * Y >= X && 2 * X >= Y) {
    int A = (2 * Y - X) / 3;
    int B = (X - A) / 2;
    ans = COM(A + B, A);
  }
  cout << ans << endl;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;

#define SZ(a) ((int)a.size())
#define ALL(a) a.begin(), a.end()
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define fst first
#define snd second

void dout() { cerr << endl; }

template <typename Head, typename... Tail> void dout(Head H, Tail... T) {
  cerr << H << ' ';
  dout(T...);
}

const int maxN = 1e6 + 47;
const int mod = 1e9 + 7;
int fact[maxN];

int mul(int a, int b) { return ((ll)a * b) % mod; }

int binpow(int a, int n) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0)
    return binpow(mul(a, a), n / 2);
  else
    return mul(a, binpow(a, n - 1));
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  fact[0] = 1;
  for (int i = 1; i < maxN; i++) {
    fact[i] = mul(fact[i - 1], i);
  }
  int x, y;
  cin >> x >> y;
  if ((x + y) % 3 != 0) {
    cout << 0;
    return 0;
  }
  int n = (x + y) / 3;
  int k = x - n;
  cout << mul(fact[n], binpow(mul(fact[k], fact[n - k]), mod - 2));
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;

#define SZ(a) ((int)a.size())
#define ALL(a) a.begin(), a.end()
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define fst first
#define snd second

void dout() { cerr << endl; }

template <typename Head, typename... Tail> void dout(Head H, Tail... T) {
  cerr << H << ' ';
  dout(T...);
}

const int maxN = 1e6 + 47;
const int mod = 1e9 + 7;
int fact[maxN];

int mul(int a, int b) { return ((ll)a * b) % mod; }

int binpow(int a, int n) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0)
    return binpow(mul(a, a), n / 2);
  else
    return mul(a, binpow(a, n - 1));
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  fact[0] = 1;
  for (int i = 1; i < maxN; i++) {
    fact[i] = mul(fact[i - 1], i);
  }
  int x, y;
  cin >> x >> y;
  if ((x + y) % 3 != 0) {
    cout << 0;
    return 0;
  }
  int n = (x + y) / 3;
  int k = x - n;
  if (k >= 0 && k <= n)
    cout << mul(fact[n], binpow(mul(fact[k], fact[n - k]), mod - 2));
  else
    cout << 0;
  return 0;
}