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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007
const int MAX = 510000;
const int MOD = 1000000007;

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() {
  ll x, y;
  cin >> x >> y;
  ll ans = 0;
  COMinit();

  // 計算例
  //   cout << COM(100000, 50000) << endl;
  for (ll i = 0; i <= x; i++) {
    ll nexy = x - i;
    //	cout <<ans<<i<<endl;
    if (nexy % 2 != 0) {
      continue;
    }
    ll zany = y - i * 2;
    if (zany < 0) {
      continue;
    }
    if (zany == nexy / 2) {
      ans += COM(zany + i, i);
      ans %= inf;
    }
  }
  cout << ans;
  // your code goes here
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007
const int MAX = 1100000;
const int MOD = 1000000007;

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() {
  ll x, y;
  cin >> x >> y;
  ll ans = 0;
  COMinit();

  // 計算例
  //   cout << COM(100000, 50000) << endl;
  for (ll i = 0; i <= x; i++) {
    ll nexy = x - i;
    //	cout <<ans<<i<<endl;
    if (nexy % 2 != 0) {
      continue;
    }
    ll zany = y - i * 2;
    if (zany < 0) {
      continue;
    }
    if (zany == nexy / 2) {
      ans += COM(zany + i, i);
      ans %= inf;
    }
  }
  cout << ans;
  // your code goes here
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<long>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvb = vector<vector<bool>>;
using vvc = vector<vector<char>>;
using vvl = vector<vector<long>>;
using pii = pair<int, int>;
using pil = pair<int, long>;
using pll = pair<long, long>;
using vc = vector<char>;
#define fix20 cout << fixed << setprecision(20)
#define YES cout << "Yes" << endl
#define NO cout << "No" << endl
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, s, t) for (int i = s; i < t; i++)
#define RNG(i, s, t, u) for (int i = s; i < t; i += u)
#define MOD 1000000007
#define all(vec) vec.begin(), vec.end()

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;
}

int main() {
  long x, y;
  cin >> x >> y;
  COMinit();
  if ((x + y) % 3 != 0)
    cout << 0 << endl;
  else {
    long m = (x + y) / 3;
    long a = x - m;
    long b = y - m;
    cout << COM(a + b, a) << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<long>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvb = vector<vector<bool>>;
using vvc = vector<vector<char>>;
using vvl = vector<vector<long>>;
using pii = pair<int, int>;
using pil = pair<int, long>;
using pll = pair<long, long>;
using vc = vector<char>;
#define fix20 cout << fixed << setprecision(20)
#define YES cout << "Yes" << endl
#define NO cout << "No" << endl
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, s, t) for (int i = s; i < t; i++)
#define RNG(i, s, t, u) for (int i = s; i < t; i += u)
#define MOD 1000000007
#define all(vec) vec.begin(), vec.end()

const int MAX = 3010000;
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() {
  long x, y;
  cin >> x >> y;
  COMinit();
  if ((x + y) % 3 != 0)
    cout << 0 << endl;
  else {
    long m = (x + y) / 3;
    long a = x - m;
    long b = y - m;
    cout << COM(a + b, a) << endl;
  }
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define pi 3.141592653589793
using namespace std;

ll power(ll a, ll b) {
  ll ans = 1;
  ll t = a;
  while (b) {
    if (b % 2) {
      ans = (ans * t) % mod;
    }

    t = (t * t) % mod;
    b /= 2;
  }

  return ans;
}

int main() {
  //    cout << fixed << setprecision(20);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll fact[2000001];
  fact[0] = 1;
  for (int i = 1; i < 2000001; i++)
    fact[i] = (fact[i - 1] * i) % mod;
  int x, y;
  cin >> x >> y;
  if (2 * x - y < 0 || (2 * x - y) % 3) {
    cout << 0;
    return 0;
  }

  int b = (2 * x - y) / 3;
  int a = x - 2 * b;
  ll ans = (fact[a + b] * power(fact[a], mod - 2)) % mod;
  ans = (ans * power(fact[b], mod - 2)) % mod;
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define pi 3.141592653589793
using namespace std;

ll power(ll a, ll b) {
  ll ans = 1;
  ll t = a;
  while (b) {
    if (b % 2) {
      ans = (ans * t) % mod;
    }

    t = (t * t) % mod;
    b /= 2;
  }

  return ans;
}

int main() {
  //    cout << fixed << setprecision(20);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll fact[2000001];
  fact[0] = 1;
  for (int i = 1; i < 2000001; i++)
    fact[i] = (fact[i - 1] * i) % mod;
  int x, y;
  cin >> x >> y;
  if (2 * x - y < 0 || (2 * x - y) % 3) {
    cout << 0;
    return 0;
  }

  int b = (2 * x - y) / 3;
  int a = x - 2 * b;
  if (a < 0) {
    cout << 0;
    return 0;
  }
  ll ans = (fact[a + b] * power(fact[a], mod - 2)) % mod;
  ans = (ans * power(fact[b], mod - 2)) % mod;
  cout << ans;
}
Problem p02862 (C++)

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

ll modex(ll x, ll n, ll m) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = (res * x) % m;
    x = (x * x) % m;
    n = n / 2;
  }
  return res;
}
int main() {

  ios_base::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ll x, y, sum = 0, k = 1, k1 = 1, k2 = 1, m = 1e9 + 7, a = 0, b = 0, f = 1;
  cin >> x >> y;
  if ((2 * x - y) % 3 == 0) {
    a = (2 * x - y) / 3;
    b = x - 2 * a;
    if (a >= 0 && b >= 0)
      f = 0;
  }
  if (!f) {
    for (ll i = 1; i <= a + b; i++)
      k = (k * i) % m;
    for (ll i = 1; i <= a; i++)
      k1 = (k1 * modex(i, m - 2, m)) % m;
    for (ll i = 1; i <= b; i++)
      k2 = (k2 * modex(i, m - 2, m)) % m;
  }
  if (f == 1)
    cout << 0 << endl;
  else {
    sum = (((k * k1) % m) * k2) % m;
    cout << sum << endl;
  }
}


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

ll modex(ll x, ll n, ll m) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = (res * x) % m;
    x = (x * x) % m;
    n = n / 2;
  }
  return res;
}
int main() {

  ios_base::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);

  // #ifndef ONLINE_JUDGE
  //    freopen("input.txt" , "r" , stdin);
  // freopen("output.txt" , "w" , stdout);
  // #endif

  ll x, y, sum = 0, k = 1, k1 = 1, k2 = 1, m = 1e9 + 7, a = 0, b = 0, f = 1;
  cin >> x >> y;
  if ((2 * x - y) % 3 == 0) {
    a = (2 * x - y) / 3;
    b = x - 2 * a;
    if (a >= 0 && b >= 0)
      f = 0;
  }
  if (!f) {
    for (ll i = 1; i <= a + b; i++)
      k = (k * i) % m;
    for (ll i = 1; i <= a; i++)
      k1 = (k1 * modex(i, m - 2, m)) % m;
    for (ll i = 1; i <= b; i++)
      k2 = (k2 * modex(i, m - 2, m)) % m;
  }
  if (f == 1)
    cout << 0 << endl;
  else {
    sum = (((k * k1) % m) * k2) % m;
    cout << sum << endl;
  }
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define FORR(i, a, b) for (int i = (b)-1; i >= (a); --i)
typedef long long ll;
using namespace std;
const ll MOD = 1000000007ll;
const int MAX = 510000;

ll fact[MAX], fact_inv[MAX];

// 繰り返し二乗法
ll power(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b >>= 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;
  ll a, b, c;
  if (2 * x < y || 2 * y < x)
    cout << 0 << endl;
  else {
    if ((2 * x - y) % 3 == 0 && (2 * y - x) % 3 == 0) {
      a = (2 * x - y) / 3;
      b = (2 * y - x) / 3;

      fact[0] = 1;
      // 階乗の計算
      rep(i, a + b) fact[i + 1] = fact[i] * (i + 1) % MOD;
      fact_inv[a + b] = power(fact[a + b], MOD - 2);
      // 逆元の計算
      FORR(i, 0, a + b) fact_inv[i] = fact_inv[i + 1] * (i + 1) % MOD;

      c = comb(a + b, a);
      cout << c % MOD << endl;
    } else {
      cout << 0 << endl;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define FORR(i, a, b) for (int i = (b)-1; i >= (a); --i)
typedef long long ll;
using namespace std;
const ll MOD = 1000000007ll;
const int MAX = 5100000;

ll fact[MAX], fact_inv[MAX];

// 繰り返し二乗法
ll power(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b >>= 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;
  ll a, b, c;
  if (2 * x < y || 2 * y < x)
    cout << 0 << endl;
  else {
    if ((2 * x - y) % 3 == 0 && (2 * y - x) % 3 == 0) {
      a = (2 * x - y) / 3;
      b = (2 * y - x) / 3;

      fact[0] = 1;
      // 階乗の計算
      rep(i, a + b) fact[i + 1] = fact[i] * (i + 1) % MOD;
      fact_inv[a + b] = power(fact[a + b], MOD - 2);
      // 逆元の計算
      FORR(i, 0, a + b) fact_inv[i] = fact_inv[i + 1] * (i + 1) % MOD;

      c = comb(a + b, a);
      cout << c % MOD << endl;
    } else {
      cout << 0 << endl;
    }
  }
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#define mod 1000000007
#define int long long
int x, y, fac[20010000];
int quickpow(int x, int y) {
  if (y == 0)
    return 1;
  if (y % 2 == 0)
    return quickpow(x * x % mod, y / 2);
  else
    return quickpow(x * x % mod, y / 2) * x % mod;
}
int C(int n, int m) {
  if (m == 0 || n == m)
    return 1;
  return fac[n] * quickpow(fac[m], mod - 2) % mod *
         quickpow(fac[n - m], mod - 2) % mod;
}
signed main() {
  scanf("%lld%lld", &x, &y);
  fac[0] = 1;
  for (int i = 1; i <= 20000000; i++)
    fac[i] = fac[i - 1] * i % mod;
  if ((x + y) % 3 != 0)
    return puts("0"), 0;
  int X = (2 * y - x) / 3, Y = (2 * x - y) / 3;
  printf("%lld\n", C(X + Y, X));
}

Fixed submission:
#include <cstdio>
#define mod 1000000007
#define int long long
int x, y, fac[20010000];
int quickpow(int x, int y) {
  if (y == 0)
    return 1;
  if (y % 2 == 0)
    return quickpow(x * x % mod, y / 2);
  else
    return quickpow(x * x % mod, y / 2) * x % mod;
}
int C(int n, int m) {
  if (m == 0 || n == m)
    return 1;
  return fac[n] * quickpow(fac[m], mod - 2) % mod *
         quickpow(fac[n - m], mod - 2) % mod;
}
signed main() {
  scanf("%lld%lld", &x, &y);
  fac[0] = 1;
  for (int i = 1; i <= 20000000; i++)
    fac[i] = fac[i - 1] * i % mod;
  if ((x + y) % 3 != 0)
    return puts("0"), 0;
  if (2 * y < x || 2 * x < y)
    return puts("0"), 0;
  int X = (2 * y - x) / 3, Y = (2 * x - y) / 3;
  printf("%lld\n", C(X + Y, X));
}
Problem p02862 (C++)

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

const int MAX = 510000;
const int MOD = 1000000007;

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() {

  COMinit();

  long x, y;
  cin >> x >> y;

  if ((x + y) % 3 != 0) {
    cout << 0;
  } else {
    long row = (x + y) / 3;
    long column = x - row;
    cout << COM(row, column) << endl;
  }

  return 0;
}


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

const int MAX = 1000000;
const int MOD = 1000000007;

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() {

  COMinit();

  long x, y;
  cin >> x >> y;

  if ((x + y) % 3 != 0) {
    cout << 0;
  } else {
    long row = (x + y) / 3;
    long column = x - row;
    cout << COM(row, column) << endl;
  }

  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

const int MAX = 510000;
const int MOD = 1000000007;

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() {
  int X, Y;
  cin >> X >> Y;

  int sum = X + Y;
  if (sum % 3 != 0) {
    cout << "0" << endl;
    return 0;
  }

  int m = (2 * Y - X) / 3;
  int n = (2 * X - Y) / 3;

  if (m < 0 || n < 0) {
    cout << "0" << endl;
    return 0;
  }

  COMinit();

  cout << COM(m + n, n) << endl;

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

const int MAX = 1000000;
const int MOD = 1000000007;

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() {
  int X, Y;
  cin >> X >> Y;

  int sum = X + Y;
  if (sum % 3 != 0) {
    cout << "0" << endl;
    return 0;
  }

  int m = (2 * Y - X) / 3;
  int n = (2 * X - Y) / 3;

  if (m < 0 || n < 0) {
    cout << "0" << endl;
    return 0;
  }

  COMinit();

  cout << COM(m + n, n) << endl;

  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>

#define MOD 1000000007

typedef long long ll;

ll fct[1333333] = {1, 1}, inv[1333333] = {1, 1}, fiv[1333333] = {1, 1};

ll combination(ll n, ll r) { return fct[n] * fiv[r] % MOD * fiv[n - r] % MOD; }

int main(void) {
  ll X, Y, i;

  scanf("%lld %lld", &X, &Y);

  if ((X + Y) % 3) {
    puts("0");
    return 0;
  }

  ll x = (2 * Y - X) / 3, y = (2 * X - Y) / 3;

  for (i = 2; i <= x + y; i++) {
    fct[i] = fct[i - 1] * i % MOD;
    inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD;
    fiv[i] = fiv[i - 1] * inv[i] % MOD;
  }

  printf("%lld", combination(x + y, x));
  return 0;
}

Fixed submission:
#include <stdio.h>

#define MOD 1000000007

typedef long long ll;

ll fct[1333333] = {1, 1}, inv[1333333] = {1, 1}, fiv[1333333] = {1, 1};

ll combination(ll n, ll r) { return fct[n] * fiv[r] % MOD * fiv[n - r] % MOD; }

int main(void) {
  ll X, Y, i;

  scanf("%lld %lld", &X, &Y);

  if ((X + Y) % 3) {
    puts("0");
    return 0;
  }

  ll x = (2 * Y - X) / 3, y = (2 * X - Y) / 3;

  if (x < 0 || y < 0) {
    puts("0");
    return 0;
  }

  for (i = 2; i <= x + y; i++) {
    fct[i] = fct[i - 1] * i % MOD;
    inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD;
    fiv[i] = fiv[i - 1] * inv[i] % MOD;
  }

  printf("%lld", combination(x + y, x));
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (int i = a; i < (int)b; i++)
#define all(v) v.begin(), v.end()
const int MOD = 1e9 + 7;
const int INF = 1e9;
//'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122

const ll MAX = 510000;
ll fac[MAX], finv[MAX], inv[MAX];

void cominit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (ll 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(ll n, ll 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() {
  ll x, y;
  cin >> x >> y;
  if ((x + y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  ll mx = max(x, y);
  ll mn = min(x, y);
  if (mx / 2 > mn) {
    cout << 0 << endl;
    return 0;
  }
  if (mx / 2 == mn) {
    cout << 1 << endl;
    return 0;
  }
  ll x2 = 0;
  ll y2 = 0;
  while (x2 - x != -(y2 - y)) {
    x2 += 2;
    y2 += 1;
  }
  cominit();
  cout << com(y2, x2 - x) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (int i = a; i < (int)b; i++)
#define all(v) v.begin(), v.end()
const int MOD = 1e9 + 7;
const int INF = 1e9;
//'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122

const ll MAX = 1000000;
ll fac[MAX], finv[MAX], inv[MAX];

void cominit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (ll 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(ll n, ll 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() {
  ll x, y;
  cin >> x >> y;
  if ((x + y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  ll mx = max(x, y);
  ll mn = min(x, y);
  if (mx / 2 > mn) {
    cout << 0 << endl;
    return 0;
  }
  if (mx / 2 == mn) {
    cout << 1 << endl;
    return 0;
  }
  ll x2 = 0;
  ll y2 = 0;
  while (x2 - x != -(y2 - y)) {
    x2 += 2;
    y2 += 1;
  }
  cominit();
  cout << com(y2, x2 - x) << endl;
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define rep(i, j, k) for (int i = j; (i) < (k); (i)++)
using namespace std;
#define mod 1000000007
#define mmax 1000000
typedef long long ll;
ll f[mmax], rf[mmax];
ll inv(ll x) {
  ll res = 1;
  ll k = mod - 2;
  ll y = x;
  while (k) {
    if (k & 1)
      res = (res * y) % mod;
    y = (y * y) % mod;
    k /= 2;
  }
  return res;
}
void init() {
  f[0] = 1;
  rep(i, 1, mmax) f[i] = (f[i - 1] * i) % mod;
  rep(i, 0, mmax) rf[i] = inv(f[i]);
}
//------------------------------------------------------------------
ll C(int n, int k) {
  init();
  ll a = f[n];      // = n!
  ll b = rf[n - k]; // = (n-k)!
  ll c = rf[k];     // = k!

  ll bc = (b * c) % mod;

  return (a * bc) % mod;
}
int main(void) {
  int x, y;
  cin >> x >> y;
  if ((x + y) % 3 != 0) {
    printf("0\n");
  } else {
    int a = (2 * y - x) / 3;
    int b = (2 * x - y) / 3;
    printf("%lld\n", C(a + b, a));
    // printf("%lld\n",C(a+b,min(a,b)));
  }
}


Fixed submission:
#include <iostream>
#define rep(i, j, k) for (int i = j; (i) < (k); (i)++)
using namespace std;
#define mod 1000000007
#define mmax 1000000
typedef long long ll;
ll f[mmax], rf[mmax];
ll inv(ll x) {
  ll res = 1;
  ll k = mod - 2;
  ll y = x;
  while (k) {
    if (k & 1)
      res = (res * y) % mod;
    y = (y * y) % mod;
    k /= 2;
  }
  return res;
}
void init() {
  f[0] = 1;
  rep(i, 1, mmax) f[i] = (f[i - 1] * i) % mod;
  rep(i, 0, mmax) rf[i] = inv(f[i]);
}
//------------------------------------------------------------------
ll C(int n, int k) {
  init();
  ll a = f[n];      // = n!
  ll b = rf[n - k]; // = (n-k)!
  ll c = rf[k];     // = k!

  ll bc = (b * c) % mod;

  return (a * bc) % mod;
}
int main(void) {
  int x, y;
  cin >> x >> y;
  if ((x + y) % 3 != 0) {
    printf("0\n");
  } else {
    int layer = (x + y) / 3;
    int left = x - layer;
    if (left < 0 || layer - left < 0) {
      cout << 0;
      return 0;
    }
    // printf("%d %d\n",layer,left);
    printf("%lld\n", C(layer, left));
    // printf("%lld\n",C(a+b,min(a,b)));
  }
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define clr(x, i) memset(x, i, sizeof(x))
using namespace std;
const int N = 2000005;
const ll mod = 1e9 + 7, inv2 = 5e8 + 4;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return x * f;
}
ll x, y, t, fac[N];
ll qpow(ll x, ll k) {
  ll ret = 1;
  while (k) {
    if (k & 1)
      ret *= x, ret %= mod;
    x *= x;
    x %= mod;
    k >>= 1;
  }
  return ret;
}
ll C(ll x, ll y) {
  return fac[x] * qpow(fac[y], mod - 2) % mod * qpow(fac[x - y], mod - 2) % mod;
}
int main() {
  fac[0] = 1;
  for (int i = 1; i <= N - 2; i++)
    fac[i] = fac[i - 1] * i % mod;
  cin >> x >> y;
  if ((x + y) % 3) {
    puts("0");
    return 0;
  }
  t = (x + y) / 3;
  x -= t;
  y -= t;
  printf("%lld\n", C(x + y, x));
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define clr(x, i) memset(x, i, sizeof(x))
using namespace std;
const int N = 2000005;
const ll mod = 1e9 + 7, inv2 = 5e8 + 4;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return x * f;
}
ll x, y, t, fac[N];
ll qpow(ll x, ll k) {
  ll ret = 1;
  while (k) {
    if (k & 1)
      ret *= x, ret %= mod;
    x *= x;
    x %= mod;
    k >>= 1;
  }
  return ret;
}
ll C(ll x, ll y) {
  return fac[x] * qpow(fac[y], mod - 2) % mod * qpow(fac[x - y], mod - 2) % mod;
}
int main() {
  fac[0] = 1;
  for (int i = 1; i <= N - 2; i++)
    fac[i] = fac[i - 1] * i % mod;
  cin >> x >> y;
  if ((x + y) % 3) {
    puts("0");
    return 0;
  }
  t = (x + y) / 3;
  x -= t;
  y -= t;
  if (x < 0 || y < 0) {
    puts("0");
    return 0;
  }
  printf("%lld\n", C(x + y, x));
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using vpii = vector<pii>;
using vpli = vector<pli>;
using vpll = vector<pll>;

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define rep2(i, s, n) for (int i = (s); i < (int)n; i++)
int INF = 1e9 + 7;

ll power(ll a, int b) {
  if (!b)
    return 1;
  ll c = power(a, b / 2);
  if (b % 2)
    return c * c % INF * a % INF;
  return c * c % INF;
}

ll factorial(ll a) {
  if (!a)
    return 1;
  return a * factorial(a - 1) % INF;
}

int main() {
  int X, Y;
  cin >> X >> Y;

  int ans = 0;
  if ((X + Y) % 3 == 0) {
    ll m = (2 * Y - X) / 3, n = (2 * X - Y) / 3;
    ans = factorial(m + n) * power(factorial(m), INF - 2) % INF *
          power(factorial(n), INF - 2) % INF;
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using vpii = vector<pii>;
using vpli = vector<pli>;
using vpll = vector<pll>;

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define rep2(i, s, n) for (int i = (s); i < (int)n; i++)
int INF = 1e9 + 7;

ll power(ll a, int b) {
  if (!b)
    return 1;
  ll c = power(a, b / 2);
  if (b % 2)
    return c * c % INF * a % INF;
  return c * c % INF;
}

ll factorial(ll a) {
  if (!a)
    return 1;
  return a * factorial(a - 1) % INF;
}

int main() {
  int X, Y;
  cin >> X >> Y;

  ll ans = 0;
  if ((X + Y) % 3 == 0 && X <= 2 * Y && Y <= 2 * X) {
    ll m = (2 * Y - X) / 3, n = (2 * X - Y) / 3;
    ans = factorial(m + n) * power(factorial(m), INF - 2) % INF *
          power(factorial(n), INF - 2) % INF;
  }

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

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

using namespace std;

vector<long long> fac;
vector<long long> fac_inv;
vector<long long> inv;

void init_combination(long long n, long long mod) {
  fac.resize(n + 1);
  fac_inv.resize(n + 1);
  inv.resize(n + 1);
  fac[0] = 1;
  fac[1] = 1;
  fac_inv[0] = 1;
  fac_inv[1] = 1;
  inv[1] = 1;
  for (long long i = 2; i <= n; i++) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = mod - (inv[mod % i] * (mod / i)) % mod;
    fac_inv[i] = fac_inv[i - 1] * inv[i] % mod;
  }
}

long long modcomb(long long n, long long r, long long mod) {
  if (n < r)
    return 0;
  long long result = fac[n] % mod;
  result *= (fac_inv[r] * fac_inv[n - r]) % mod;
  result %= mod;
  return result;
}
int main(void) {
  const long long p = (long long)1e9 + 7;
  int x, y;
  cin >> x >> y;
  if ((2 * y - x) % 3 != 0 || (2 * x - y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }

  int a = (2 * y - x) / 3;
  int b = (2 * x - y) / 3;
  init_combination(a + b, p);
  cout << modcomb(a + b, a, p) << endl;

  return 0;
}

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

using namespace std;

vector<long long> fac;
vector<long long> fac_inv;
vector<long long> inv;

void init_combination(long long n, long long mod) {
  fac.resize(n + 1);
  fac_inv.resize(n + 1);
  inv.resize(n + 1);
  fac[0] = 1;
  fac[1] = 1;
  fac_inv[0] = 1;
  fac_inv[1] = 1;
  inv[1] = 1;
  for (long long i = 2; i <= n; i++) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = mod - (inv[mod % i] * (mod / i)) % mod;
    fac_inv[i] = fac_inv[i - 1] * inv[i] % mod;
  }
}

long long modcomb(long long n, long long r, long long mod) {
  if (n < r)
    return 0;
  long long result = fac[n] % mod;
  result *= (fac_inv[r] * fac_inv[n - r]) % mod;
  result %= mod;
  return result;
}
int main(void) {
  const long long p = (long long)1e9 + 7;
  int x, y;
  cin >> x >> y;
  if ((2 * y - x) % 3 != 0 || (2 * x - y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }

  int a = (2 * y - x) / 3;
  int b = (2 * x - y) / 3;
  if (a < 0 || b < 0) {
    cout << 0 << endl;
    return 0;
  }

  init_combination(a + b, p);
  cout << modcomb(a + b, a, p) << endl;

  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll M = 1e9 + 7;

int x, y;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> x >> y;
  int a = (2 * x - y) / 3, b = (2 * y - x) / 3;
  if ((x + y) % 3 || a < 0 || b < 0) {
    cout << 0 << endl;
    return 0;
  }
  ll D = 1, d = 1, m = min(a, b), n = a + b;
  for (int i = 0; i < m; ++i) {
    D = D * (n - i) % M;
    d = d * (i + 1) % M;
  }
  while (D % d != 0)
    D += M;
  cout << (D / d) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll M = 1e9 + 7;

int x, y;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> x >> y;
  int a = (2 * x - y) / 3, b = (2 * y - x) / 3;
  if ((x + y) % 3 || a < 0 || b < 0) {
    cout << 0 << endl;
    return 0;
  }
  ll D = 1, d = 1, m = min(a, b), n = a + b;
  for (int i = 0; i < m; ++i) {
    D = D * (n - i) % M;
    d = d * (i + 1) % M;
  }
  // d^-1 mod M = d^M-2 mod M
  ll p = M - 2, id = 1;
  while (p != 0) {
    if (p % 2 == 0) {
      d = d * d % M;
      p /= 2;
    } else {
      id = id * d % M;
      p -= 1;
    }
  }
  cout << (D * id % M) << endl;
}
Problem p02862 (C++)

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

const int MAX = 100000000;
const long long MOD = 1000000007;
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() {

  long long x, y;
  cin >> x >> y;

  if ((x + y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }

  long long m = (2 * x - y) / 3;
  long long n = (-1 * x + 2 * y) / 3;

  if (m < 0 || n < 0) {
    cout << 0 << endl;
  } else {
    COMinit();
    cout << COM(m + n, n) << endl;
  }

  return 0;
}

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

const int MAX = 1000000;
const long long MOD = 1000000007;
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() {

  long long x, y;
  cin >> x >> y;

  if ((x + y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }

  long long m = (2 * x - y) / 3;
  long long n = (-1 * x + 2 * y) / 3;

  if (m < 0 || n < 0) {
    cout << 0 << endl;
  } else {
    COMinit();
    cout << COM(m + n, n) << endl;
  }

  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
#define i64 long long
#define lint long long

#define I32_MAX 2147483647
#define I64_MAX 9223372036854775807
#define MOD 1000000007

#define int long long

int MIN = I64_MAX;

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;
}

void solve() {

  int X, Y;
  cin >> X >> Y;
  int P, Q;
  P = 2 * Y - X;
  Q = 2 * X - Y;

  if ((P < 0) || (Q < 0)) {
    cout << 0 << endl;
    return;
  }

  if (P % 3 == 0) {
    P /= 3;
  } else {
    cout << 0 << endl;
    return;
  }

  if (Q % 3 == 0) {
    Q /= 3;
  } else {
    cout << 0 << endl;
    return;
  }

  int A = P + Q;
  int B = min(P, Q);
  COMinit();

  cout << COM(A, B) % MOD << endl;

  return;
}

int32_t main(int32_t argc, const char *argv[]) {

  solve();

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
#define i64 long long
#define lint long long

#define I32_MAX 2147483647
#define I64_MAX 9223372036854775807
#define MOD 1000000007

#define int long long

int MIN = I64_MAX;

const int MAX = 1000000;

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;
}

void solve() {

  int X, Y;
  cin >> X >> Y;
  int P, Q;
  P = 2 * Y - X;
  Q = 2 * X - Y;

  if ((P < 0) || (Q < 0)) {
    cout << 0 << endl;
    return;
  }

  if (P % 3 == 0) {
    P /= 3;
  } else {
    cout << 0 << endl;
    return;
  }

  if (Q % 3 == 0) {
    Q /= 3;
  } else {
    cout << 0 << endl;
    return;
  }

  int A = P + Q;
  int B = min(P, Q);
  COMinit();

  cout << COM(A, B) % MOD << endl;

  return;
}

int32_t main(int32_t argc, const char *argv[]) {

  solve();

  return 0;
}
Problem p02862 (C++)

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

#define int long long
#define MOD 1000000007

int f[10000010];
void init() {
  f[0] = 1;
  for (int i = 1; i < 10000010; i++) {
    f[i] = f[i - 1] * i % MOD;
  }
}

int pow_mod(int a, int b) {
  int ans = 1;
  while (b != 0) {
    if (b & 1)
      ans = (ans * a) % MOD;
    a = a * a % MOD;
    b >>= 1;
  }

  return ans;
}

int combi(int n, int r) {
  return f[n] * pow_mod(f[r], MOD - 2) % MOD * pow_mod(f[n - r], MOD - 2) % MOD;
}

signed main() {
  init();
  int w, h;
  cin >> w >> h;

  int y = 2 * h - w;
  if (y % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  y /= 3;

  int x = w - y;
  if (x % 2 != 0) {
    cout << 0 << endl;
    return 0;
  }

  x /= 2;

  if (y < 0 || x < 0) {
    swap(h, w);
    int y = 2 * h - w;
    if (y % 3 != 0) {
      cout << 0 << endl;
      return 0;
    }
    y /= 3;

    int x = w - y;
    if (x % 2 != 0) {
      cout << 0 << endl;
      return 0;
    }
    x /= 2;
  }

  cout << combi(x + y, x) << endl;
  return 0;
}


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

#define int long long
#define MOD 1000000007

int f[10000010];
void init() {
  f[0] = 1;
  for (int i = 1; i < 10000010; i++) {
    f[i] = f[i - 1] * i % MOD;
  }
}

int pow_mod(int a, int b) {
  int ans = 1;
  while (b != 0) {
    if (b & 1)
      ans = (ans * a) % MOD;
    a = a * a % MOD;
    b >>= 1;
  }

  return ans;
}

int combi(int n, int r) {
  return f[n] * pow_mod(f[r], MOD - 2) % MOD * pow_mod(f[n - r], MOD - 2) % MOD;
}

signed main() {
  init();
  int w, h;
  cin >> w >> h;

  int y = 2 * h - w;
  if (y % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  y /= 3;

  int x = w - y;
  if (x % 2 != 0) {
    cout << 0 << endl;
    return 0;
  }

  x /= 2;

  if (y < 0 || x < 0) {
    swap(h, w);
    int y = 2 * h - w;
    if (y % 3 != 0) {
      cout << 0 << endl;
      return 0;
    }
    y /= 3;

    int x = w - y;
    if (x % 2 != 0) {
      cout << 0 << endl;
      return 0;
    }
    x /= 2;
  }

  if (x < 0 || y < 0) {
    cout << 0 << endl;
    return 0;
  }

  cout << combi(x + y, x) << endl;
  return 0;
}
Problem p02862 (C++)

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

ll f[1000010];
void init() {
  f[0] = 1;
  for (int i = 1; i < 1000010; i++)
    f[i] = (f[i - 1] * i) % MOD;
}
ll inv(ll x) {
  ll res = 1;
  ll k = MOD - 2;
  ll y = x;
  while (k) {
    if (k & 1)
      res = (res * y) % MOD;
    y = (y * y) % MOD;
    k /= 2;
  }
  return res;
}
ll C(int n, int k) {
  ll a = f[n];
  ll b = f[n - k];
  ll c = f[k];
  ll bc = (b * c) % MOD;
  return (a * inv(bc)) % MOD;
}

int main() {
  init();
  int x, y;
  cin >> x >> y;
  int u, r = 0;
  while (y != x * 2) {
    y -= 1;
    x -= 2;
    r++;
    if (y > x * 2) {
      cout << 0 << endl;
      return 0;
    }
  }
  u = y / 2;
  cout << C(u + r, r) << endl;
  return 0;
}

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

ll f[1000010];
void init() {
  f[0] = 1;
  for (int i = 1; i < 1000010; i++)
    f[i] = (f[i - 1] * i) % MOD;
}
ll inv(ll x) {
  ll res = 1;
  ll k = MOD - 2;
  ll y = x;
  while (k) {
    if (k & 1)
      res = (res * y) % MOD;
    y = (y * y) % MOD;
    k /= 2;
  }
  return res;
}
ll C(int n, int k) {
  ll a = f[n];
  ll b = f[n - k];
  ll c = f[k];
  ll bc = (b * c) % MOD;
  return (a * inv(bc)) % MOD;
}

int main() {
  init();
  int x, y;
  cin >> x >> y;
  int u, r = 0;
  while (y != x * 2) {
    y -= 1;
    x -= 2;
    r++;
    if (y > x * 2 || y < 0 || x < 0) {
      cout << 0 << endl;
      return 0;
    }
  }
  u = y / 2;
  cout << C(u + r, r) << endl;
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

const long long MOD = 1000000007;

int a;
int b;

int x, y;

const int MAXN = 1000010;
int p[1000010];
int szP;
int vis[1000010];

void make_p() {
  // 挖素数
  szP = 0;
  vis[0] = vis[1] = 1;
  for (int i = 2; i < MAXN; i++) {
    if (!vis[i]) {
      p[szP] = i;
      szP++;

      for (int j = i * 2; j < MAXN; j += i)
        vis[j] = 1;
    }
  }
}

long long qsm(long long a, long long b) { // 快速幂
  long long ans = 1, w = a;
  for (; b; b >>= 1, w = (w * w) % MOD) {
    if (b & 1)
      ans = (ans * w) % MOD;
  }
  return ans;
}

long long Get(long long x, long long y) {
  long long sum = 0;
  for (; x; x /= y)
    sum += x / y;
  return sum;
}

long long getC(int x, int y) {
  long long ans = 1;

  for (int i = 0; (i < szP && p[i] <= x); i++) {
    int T = Get(x, p[i]) - Get(x - y, p[i]) - Get(y, p[i]);
    ans = (ans * qsm(p[i], T)) % MOD;
  }
  return ans;
}

int main() {
  make_p();

  scanf("%d%d", &x, &y);

  int a = 2 * x - y;
  int b = 2 * y - x;

  if ((a % 3 == 0) && (b % 3 == 0)) {
    a = a / 3;
    b = b / 3;
    long long ans = getC(a + b, b);
    printf("%I64d\n", ans);
  } else {
    printf("0\n");
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

const long long MOD = 1000000007;

int a;
int b;

int x, y;

const int MAXN = 1000010;
int p[1000010];
int szP;
int vis[1000010];

void make_p() {
  // 挖素数
  szP = 0;
  vis[0] = vis[1] = 1;
  for (int i = 2; i < MAXN; i++) {
    if (!vis[i]) {
      p[szP] = i;
      szP++;

      for (int j = i * 2; j < MAXN; j += i)
        vis[j] = 1;
    }
  }
}

long long qsm(long long a, long long b) { // 快速幂
  long long ans = 1, w = a;
  for (; b; b >>= 1, w = (w * w) % MOD) {
    if (b & 1)
      ans = (ans * w) % MOD;
  }
  return ans;
}

long long Get(long long x, long long y) {
  long long sum = 0;
  for (; x; x /= y)
    sum += x / y;
  return sum;
}

long long getC(int x, int y) {
  long long ans = 1;

  for (int i = 0; (i < szP && p[i] <= x); i++) {
    int T = Get(x, p[i]) - Get(x - y, p[i]) - Get(y, p[i]);
    ans = (ans * qsm(p[i], T)) % MOD;
  }
  return ans;
}

int main() {
  make_p();

  scanf("%d%d", &x, &y);

  int a = 2 * x - y;
  int b = 2 * y - x;

  if ((a < 0) || (b < 0)) {
    printf("0\n");
    return 0;
  }

  if ((a % 3 == 0) && (b % 3 == 0)) {
    a = a / 3;
    b = b / 3;
    long long ans = getC(a + b, b);
    printf("%I64d\n", ans);
  } else {
    printf("0\n");
  }

  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, l, r) for (register int i = (l); i <= (r); i++)
#define LOG(...) fprintf(stderr, __VA_ARGS__)
using namespace std;
typedef long long ll;
const int N = 2000000 + 5, MOD = 1000000007;
int fac[N], n, x, y, ifac[N];

int qpow(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      res = 1ll * res * a % MOD;
    a = 1LL * a * a % MOD;
    b >>= 1;
  }
  return res;
}
int inv(int x) { return qpow(x, MOD - 2); }
int main() {
  scanf("%d%d", &x, &y);
  n = max(x, y) * 2;
  fac[0] = 1;
  rep(i, 1, n) fac[i] = 1LL * fac[i - 1] * i % MOD;
  ifac[n] = qpow(fac[n], MOD - 2);
  for (int i = n - 1; i >= 0; i--)
    ifac[i] = 1LL * ifac[i + 1] * (i + 1) % MOD;

  int ans = 2 * y - x;
  if (ans < 0 || ans % 3)
    return puts("0"), 0;
  int yy = ans / 3, xx = y - 2 * yy;

  if (2 * xx + yy != x)
    return puts("0"), 0;

  cout << 1LL * fac[xx + yy] * ifac[xx] % MOD * ifac[yy] % MOD << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, l, r) for (register int i = (l); i <= (r); i++)
#define LOG(...) fprintf(stderr, __VA_ARGS__)
using namespace std;
typedef long long ll;
const int N = 2000000 + 5, MOD = 1000000007;
int fac[N], n, x, y, ifac[N];

int qpow(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      res = 1ll * res * a % MOD;
    a = 1LL * a * a % MOD;
    b >>= 1;
  }
  return res;
}
int inv(int x) { return qpow(x, MOD - 2); }
int main() {
  scanf("%d%d", &x, &y);
  n = max(x, y) * 2;
  fac[0] = 1;
  rep(i, 1, n) fac[i] = 1LL * fac[i - 1] * i % MOD;
  ifac[n] = qpow(fac[n], MOD - 2);
  for (int i = n - 1; i >= 0; i--)
    ifac[i] = 1LL * ifac[i + 1] * (i + 1) % MOD;

  int ans = 2 * y - x;
  if (ans < 0 || ans % 3)
    return puts("0"), 0;
  int yy = ans / 3, xx = y - 2 * yy;

  if (2 * xx + yy != x || xx < 0)
    return puts("0"), 0;

  cout << 1LL * fac[xx + yy] * ifac[xx] % MOD * ifac[yy] % MOD << endl;
  return 0;
}
Problem p02862 (C++)

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

const int M = (int)1e9 + 7;
const int N = (int)2e6 + 5;

ll f[N], rf[N];

ll powmod(ll a, ll b) {
  if (b == 0)
    return 1;
  ll ans = powmod(a, b / 2);
  ans = ans * ans % M;
  if (b % 2)
    ans = ans * a % M;
  return ans;
}
ll C(ll a, ll b) {
  if (a < b)
    return 0;
  return f[a] * rf[b] % M * rf[a - b] % M;
}

ll x, y;

void build() {
  f[0] = 1;
  for (int i = 1; i < N; ++i)
    f[i] = f[i - 1] * i % M;
  rf[N - 1] = powmod(f[N - 1], M - 2);
  for (int i = N - 2; i >= 0; --i)
    rf[i] = rf[i + 1] * (i + 1) % M;
}
void init() { cin >> x >> y; }
void solve() {
  if ((x + y) % 3 != 0) {
    cout << "0\n";
    return;
  }
  ll xy = (x + y) / 3, xx = 2 * xy - y, yy = 2 * xy - x;
  cout << C(xx + yy, xx) << '\n';
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  build();
  init();
  solve();
}


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

const int M = (int)1e9 + 7;
const int N = (int)2e6 + 5;

ll f[N], rf[N];

ll powmod(ll a, ll b) {
  if (b == 0)
    return 1;
  ll ans = powmod(a, b / 2);
  ans = ans * ans % M;
  if (b % 2)
    ans = ans * a % M;
  return ans;
}
ll C(ll a, ll b) {
  if (a < b)
    return 0;
  return f[a] * rf[b] % M * rf[a - b] % M;
}

ll x, y;

void build() {
  f[0] = 1;
  for (int i = 1; i < N; ++i)
    f[i] = f[i - 1] * i % M;
  rf[N - 1] = powmod(f[N - 1], M - 2);
  for (int i = N - 2; i >= 0; --i)
    rf[i] = rf[i + 1] * (i + 1) % M;
}
void init() { cin >> x >> y; }
void solve() {
  if ((x + y) % 3 != 0) {
    cout << "0\n";
    return;
  }
  ll xy = (x + y) / 3, xx = 2 * xy - y, yy = 2 * xy - x;
  if (xx < 0 || yy < 0) {
    cout << "0\n";
    return;
  }
  cout << C(xx + yy, xx) << '\n';
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  build();
  init();
  solve();
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#define N 1000005
using namespace std;
typedef long long ll;

ll x, y, s, M = 1e9 + 7;
ll fac[N] = {1, 1}, inv[N] = {1, 1}, fiv[N] = {1, 1};
int main() {
  cin >> x >> y;
  s = x + y;
  if (s % 3) {
    puts("0");
    return 0;
  }
  x -= s / 3;
  y -= s / 3;
  for (int i = 2; i <= x + y; i++) {
    fac[i] = fac[i - 1] * i % M;
    inv[i] = inv[M % i] * (M - M / i) % M;
    fiv[i] = fiv[i - 1] * inv[i] % M;
  }
  cout << fac[x + y] * fiv[x] % M * fiv[y] % M;
  return 0;
}


Fixed submission:
#include <cstdio>
#include <iostream>
#define N 1000005
using namespace std;
typedef long long ll;

ll x, y, s, M = 1e9 + 7;
ll fac[N] = {1, 1}, inv[N] = {1, 1}, fiv[N] = {1, 1};
int main() {
  cin >> x >> y;
  s = x + y;
  if (s % 3) {
    puts("0");
    return 0;
  }
  x -= s / 3;
  y -= s / 3;
  if (x < 0 || y < 0) {
    puts("0");
    return 0;
  }
  for (int i = 2; i <= x + y; i++) {
    fac[i] = fac[i - 1] * i % M;
    inv[i] = inv[M % i] * (M - M / i) % M;
    fiv[i] = fiv[i - 1] * inv[i] % M;
  }
  cout << fac[x + y] * fiv[x] % M * fiv[y] % M;
  return 0;
}
Problem p02862 (C++)

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

#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef array<int, 3> tri;
typedef long double ld;

template <class T> istream &operator>>(istream &I, vector<T> &v) {
  for (T &e : v)
    I >> e;
  return I;
}
template <class T> ostream &operator<<(ostream &O, const vector<T> &v) {
  for (const T &e : v)
    O << e << ' ';
  return O;
}

const int MOD = 1E9 + 7, N = 1E6 + 1;
ll fact[N];
ll exp(int a, int n) {
  if (n == 0)
    return 1;
  ll b = exp(a, n / 2);
  (b *= b) %= MOD;
  if (n & 1)
    (b *= a) %= MOD;
  return b;
}

ll C(int n, int k) {
  return fact[n] * exp(fact[k], MOD - 2) % MOD * exp(fact[n - k], MOD - 2) %
         MOD;
}

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

  int a = (2 * x - y) / 3;
  int b = a - x + y;
  fact[0] = 1;
  for (int i = 1; i <= a + b; i++)
    fact[i] = fact[i - 1] * i % MOD;

  cout << C(a + b, a);
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  //        freopen("input.txt", "r", stdin);
  int _t = 1;
  //        cin >> _t;
  while (_t--)
    _main();
  return 0;
}

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

#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef array<int, 3> tri;
typedef long double ld;

template <class T> istream &operator>>(istream &I, vector<T> &v) {
  for (T &e : v)
    I >> e;
  return I;
}
template <class T> ostream &operator<<(ostream &O, const vector<T> &v) {
  for (const T &e : v)
    O << e << ' ';
  return O;
}

const int MOD = 1E9 + 7, N = 1E6 + 1;
ll fact[N];
ll exp(int a, int n) {
  if (n == 0)
    return 1;
  ll b = exp(a, n / 2);
  (b *= b) %= MOD;
  if (n & 1)
    (b *= a) %= MOD;
  return b;
}

ll C(int n, int k) {
  return fact[n] * exp(fact[k], MOD - 2) % MOD * exp(fact[n - k], MOD - 2) %
         MOD;
}

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

  int a = (2 * x - y) / 3;
  int b = a - x + y;
  if (a < 0 || b < 0) {
    cout << 0;
    return;
  }

  fact[0] = 1;
  for (int i = 1; i <= a + b; i++)
    fact[i] = fact[i - 1] * i % MOD;

  cout << C(a + b, a);
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  //        freopen("input.txt", "r", stdin);
  int _t = 1;
  //        cin >> _t;
  while (_t--)
    _main();
  return 0;
}