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

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

using namespace std;
using ll = unsigned long long;
using P = pair<long double, long double>;

const ll INF64 = 1LL << 60;
const int INF = 1 << 29;
const ll MOD = 1000000007;

const int CASES = 3;

// 負の数にも対応した mod (a = -11 とかでも OK)
inline long long mod(long long a, long long m) {
  long long res = a % m;
  if (res < 0)
    res += m;
  return res;
}

// 拡張 Euclid の互除法
long long extGCD(long long a, long long b, long long &p, long long &q) {
  if (b == 0) {
    p = 1;
    q = 0;
    return a;
  }
  long long d = extGCD(b, a % b, q, p);
  q -= a / b * p;
  return d;
}

// 逆元計算 (ここでは a と m が互いに素であることが必要)
long long modinv(long long a, long long m) {
  long long x, y;
  extGCD(a, m, x, y);
  return mod(x, m); // 気持ち的には x % m だが、x が負かもしれないので
}

ll com(const ll n, const ll k) {
  ll ue = 1, sita = 1;
  for (ll i = 0; i < k; ++i) {
    ue *= (n - i);
    ue %= MOD;
    sita *= (i + 1);
    sita %= MOD;
  }
  return ue * modinv(sita, MOD) % MOD;
}

void solve() {
  ll x, y;
  cin >> x >> y;
  ll sum = x + y;
  if (sum % 3 != 0) {
    cout << 0 << endl;
    return;
  }

  ll n = sum / 3;
  cout << com(n, x - n) << endl;

  return;
}

int main() {
#ifdef MYLOCAL
  vector<string> filenames{"input1.txt", "input2.txt", "input3.txt",
                           "input4.txt"};
  cout << "------------\n";
  for (int i = 0; i < CASES; ++i) {
    ifstream in(filenames[i]);
    cin.rdbuf(in.rdbuf());
    solve();
    cout << "------------\n";
  }
#else

  solve();

#endif
  return 0;
}


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

using namespace std;
using ll = unsigned long long;
using P = pair<long double, long double>;

const ll INF64 = 1LL << 60;
const int INF = 1 << 29;
const ll MOD = 1000000007;

const int CASES = 3;

// 負の数にも対応した mod (a = -11 とかでも OK)
inline long long mod(long long a, long long m) {
  long long res = a % m;
  if (res < 0)
    res += m;
  return res;
}

// 拡張 Euclid の互除法
long long extGCD(long long a, long long b, long long &p, long long &q) {
  if (b == 0) {
    p = 1;
    q = 0;
    return a;
  }
  long long d = extGCD(b, a % b, q, p);
  q -= a / b * p;
  return d;
}

// 逆元計算 (ここでは a と m が互いに素であることが必要)
long long modinv(long long a, long long m) {
  long long x, y;
  extGCD(a, m, x, y);
  return mod(x, m); // 気持ち的には x % m だが、x が負かもしれないので
}

ll com(const ll n, const ll k) {
  ll ue = 1, sita = 1;
  for (ll i = 0; i < k; ++i) {
    ue *= (n - i);
    ue %= MOD;
    sita *= (i + 1);
    sita %= MOD;
  }
  return ue * modinv(sita, MOD) % MOD;
}

void solve() {
  ll x, y;
  cin >> x >> y;
  ll sum = x + y;
  if (sum % 3 != 0) {
    cout << 0 << endl;
    return;
  }

  ll n = sum / 3;
  ll k = min(x - n, n - (x - n));
  cout << com(n, k) << endl;
  return;
}

int main() {
#ifdef MYLOCAL
  vector<string> filenames{"input1.txt", "input2.txt", "input3.txt",
                           "input4.txt"};
  cout << "------------\n";
  for (int i = 0; i < CASES; ++i) {
    ifstream in(filenames[i]);
    cin.rdbuf(in.rdbuf());
    solve();
    cout << "------------\n";
  }
#else

  solve();

#endif
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(i, n) for (ll i = 1; i < n + 1; i++)

#define MAX 510000
#define MOD 1000000007

// fac: 階乗、finv: 階乗の逆元(invの積)、inv:
// ある数の逆元(フェルマーの小定理より)
ll fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void combModInit() {
  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 comb(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;

  combModInit();

  if (Y > 2 * X || 2 * Y < X || (X + Y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  ll moves = (X + Y) / 3;
  ll up = X - moves;
  ll ans = comb(moves, up);

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(i, n) for (ll i = 1; i < n + 1; i++)

#define MAX 5100000
#define MOD 1000000007

// fac: 階乗、finv: 階乗の逆元(invの積)、inv:
// ある数の逆元(フェルマーの小定理より)
ll fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void combModInit() {
  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 comb(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;

  combModInit();

  if (Y > 2 * X || 2 * Y < X || (X + Y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  ll moves = (X + Y) / 3;
  ll up = X - moves;
  ll ans = comb(moves, up);

  cout << ans << endl;

  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG // これつけるとA[N]でもいいらしい
// for文のマクロ
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define big 1000000007

const int MOD = 1000000007;
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() {
  COMinit();
  int x, y;
  cin >> x >> y;
  int n, m;
  n = 2 * y - x;
  m = 2 * x - y;
  // cout<<n<<" "<<m<<endl;
  int64_t ans;
  if (n % 3 != 0 || n < 0 || m % 3 != 0 || m < 0) {
    ans = 0;
  } else {
    n /= 3, m /= 3;
    ans = COM(m + n, n);
    // m+nCnを求めればいい
  }
  // cout<<"Power(m+n,n)="<<power(m+n,m,big)<<endl;
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG // これつけるとA[N]でもいいらしい
// for文のマクロ
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define big 1000000007

const int MOD = 1000000007;
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;
}
int main() {
  COMinit();
  int x, y;
  cin >> x >> y;
  int n, m;
  n = 2 * y - x;
  m = 2 * x - y;
  // cout<<n<<" "<<m<<endl;
  int64_t ans;
  if (n % 3 != 0 || n < 0 || m % 3 != 0 || m < 0) {
    ans = 0;
  } else {
    n /= 3, m /= 3;
    ans = COM(m + n, n);
    // m+nCnを求めればいい
  }
  // cout<<"Power(m+n,n)="<<power(m+n,m,big)<<endl;
  cout << ans << endl;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define rrep(i, a, b) for (ll i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#define all(x) (x).begin(), (x).end()

// ２項定理(MOD使用)
const ll MAX = 510000;
const ll 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 (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;
  }
}

// 二項係数計算
long long 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(void) {

  vector<ll> len;
  pair<ll, ll> pre;
  pre.first = 0;
  pre.second = 0;
  ll x, y;

  cin >> x >> y;

  len.push_back(1);
  COMinit();

  ll a = 0, b = 0;
  ll n = 0;
  rep(i, 0, 1000000) {
    a += 2;
    b += 1;
    n++;
    if (a - x == y - b) {
      cout << COM(n, a - x) << endl;
      return 0;
    }
  }
  cout << 0 << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define rrep(i, a, b) for (ll i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#define all(x) (x).begin(), (x).end()

// ２項定理(MOD使用)
const ll MAX = 1000000;
const ll 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 (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;
  }
}

// 二項係数計算
long long 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(void) {

  vector<ll> len;
  pair<ll, ll> pre;
  pre.first = 0;
  pre.second = 0;
  ll x, y;

  cin >> x >> y;

  len.push_back(1);
  COMinit();

  ll a = 0, b = 0;
  ll n = 0;
  rep(i, 0, 1000000) {
    a += 2;
    b += 1;
    n++;
    if (a - x == y - b) {
      cout << COM(n, a - x) << endl;
      return 0;
    }
  }
  cout << 0 << endl;
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
// Author: getharshkumar

#include <bits/stdc++.h>
using namespace std;
#define ll int64_t
const ll M = 1e9 + 7, N = 1e6 + 5;
ll fac[N];
ll power(ll x, ll y) {
  ll res = 1;
  x %= M;
  while (y) {
    if (y % 2)
      res = (res * x) % M;
    x = (x * x) % M;
    y /= 2;
  }
  return res;
}
ll ncr(ll n, ll r) {
  if (r == 0)
    return 1;
  return ((((fac[n] * power(fac[r], M - 2)) % M) * power(fac[n - r], M - 2)) %
          M) %
         M;
}
int main() {
  fac[0] = 1;
  for (ll i = 1; i < N; i++)
    fac[i] = (fac[i - 1] * i) % M;
  ll x, y;
  cin >> x >> y;
  ll a = (x + y) / 3;
  if ((x + y) % 3 || y < a)
    cout << 0;
  else
    cout << ncr(a, y - a);
  return 0;
}

Fixed submission:
// Author: getharshkumar

#include <bits/stdc++.h>
using namespace std;
#define ll int64_t
const ll M = 1e9 + 7, N = 1e6 + 5;
ll fac[N];
ll power(ll x, ll y) {
  ll res = 1;
  x %= M;
  while (y) {
    if (y % 2)
      res = (res * x) % M;
    x = (x * x) % M;
    y /= 2;
  }
  return res;
}
ll ncr(ll n, ll r) {
  if (r == 0)
    return 1;
  return ((((fac[n] * power(fac[r], M - 2)) % M) * power(fac[n - r], M - 2)) %
          M) %
         M;
}
int main() {
  fac[0] = 1;
  for (ll i = 1; i < N; i++)
    fac[i] = (fac[i - 1] * i) % M;
  ll x, y;
  cin >> x >> y;
  ll a = (x + y) / 3;
  if ((x + y) % 3 || y > 2 * a || y < a)
    cout << 0;
  else
    cout << ncr(a, y - a);
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007ll
using namespace std;
ll fact[2000006];
ll bigmod(ll b, ll p) {
  if (!p)
    return 1ll;
  ll ret = bigmod(b, p / 2);
  ret = (ret * ret) % mod;
  if (p & 1ll)
    return ret = (ret * b) % mod;
  return ret;
}
ll ncr(ll n, ll r) {
  ll nume = fact[n];
  ll deno = (fact[n - r] * fact[r]) % mod;
  return (nume * bigmod(deno, mod - 2)) % mod;
}
int main() {
  fact[0] = 1ll;
  for (ll i = 1; i <= 2000000; i++)
    fact[i] = (fact[i - 1] * i) % mod;
  int n, m, cnt = 0;
  scanf("%d %d", &n, &m);
  if (n < m)
    swap(n, m);
  int tot = (n + m) / 3;
  while (2 * n > m)
    n--, n--, m--, cnt++;
  if (2 * n == m)
    printf("%lld\n", ncr(tot, cnt));
  else
    puts("0");
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007ll
using namespace std;
ll fact[2000006];
ll bigmod(ll b, ll p) {
  if (!p)
    return 1ll;
  ll ret = bigmod(b, p / 2);
  ret = (ret * ret) % mod;
  if (p & 1ll)
    return ret = (ret * b) % mod;
  return ret;
}
ll ncr(ll n, ll r) {
  ll nume = fact[n];
  ll deno = (fact[n - r] * fact[r]) % mod;
  return (nume * bigmod(deno, mod - 2)) % mod;
}
int main() {
  fact[0] = 1ll;
  for (ll i = 1; i <= 2000000; i++)
    fact[i] = (fact[i - 1] * i) % mod;
  int n, m, cnt = 0;
  scanf("%d %d", &n, &m);
  if (n < m)
    swap(n, m);
  int tot = (n + m) / 3;
  while (n > 0 && m > 0 && 2 * n > m)
    n--, n--, m--, cnt++;
  if (2 * n == m)
    printf("%lld\n", ncr(tot, cnt));
  else
    puts("0");
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
vector<long long> fact, fact_inv, inv;
/*  init_nCk :二項係数のための前処理
    計算量:O(n)
*/
void init_nCk(int SIZE) {
  fact.resize(SIZE + 5);
  fact_inv.resize(SIZE + 5);
  inv.resize(SIZE + 5);
  fact[0] = fact[1] = 1;
  fact_inv[0] = fact_inv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < SIZE + 5; i++) {
    fact[i] = fact[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    fact_inv[i] = fact_inv[i - 1] * inv[i] % MOD;
  }
}
/*  nCk :MODでの二項係数を求める(前処理 int_nCk が必要)
    計算量:O(1)
*/
long long nCk(int n, int k) {
  assert(!(n < k));
  assert(!(n < 0 || k < 0));
  return fact[n] * (fact_inv[k] * fact_inv[n - k] % MOD) % MOD;
}

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
vector<long long> fact, fact_inv, inv;
/*  init_nCk :二項係数のための前処理
    計算量:O(n)
*/
void init_nCk(int SIZE) {
  fact.resize(SIZE + 5);
  fact_inv.resize(SIZE + 5);
  inv.resize(SIZE + 5);
  fact[0] = fact[1] = 1;
  fact_inv[0] = fact_inv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < SIZE + 5; i++) {
    fact[i] = fact[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    fact_inv[i] = fact_inv[i - 1] * inv[i] % MOD;
  }
}
/*  nCk :MODでの二項係数を求める(前処理 int_nCk が必要)
    計算量:O(1)
*/
long long nCk(int n, int k) {
  assert(!(n < k));
  assert(!(n < 0 || k < 0));
  return fact[n] * (fact_inv[k] * fact_inv[n - k] % MOD) % MOD;
}

int main() {
  int x, y;
  cin >> x >> y;
  if ((x + y) % 3 != 0)
    cout << "0" << endl;
  else {
    int a, b;
    a = (2 * y - x) / 3;
    b = (2 * x - y) / 3;
    if (a < 0 || b < 0)
      cout << "0" << endl;
    else {
      init_nCk(1001001);
      cout << nCk(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;

constexpr ll MOD = 1e9 + 7;
constexpr ll INF = 1ll << 60;

#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

constexpr ll COMB_MAX = 1e6 + 10;
vector<ll> fac(COMB_MAX);
vector<ll> ifac(COMB_MAX);

ll mpow(ll x, ll n) {
  ll ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % MOD;
    x = x * x % MOD;
    n = n >> 1;
  }
  return ans;
}

ll comb(ll a, ll b) {
  if (a == 0 && b == 0)
    return 1;
  if (a < b || a < 0)
    return 0;
  ll tmp = ifac[a - b] * ifac[b] % MOD;
  return tmp * fac[a] % MOD;
}

void init() {
  fac[0] = 1;
  ifac[0] = 1;
  for (ll i = 0; i < COMB_MAX; i++) {
    fac[i + 1] = fac[i] * (i + 1) % MOD;
    ifac[i + 1] = ifac[i] * mpow(i + 1, MOD - 2) % MOD;
  }
}

int main(int argc, char **argv) {
  ll H, W;
  cin >> H >> W;
  init();
  ll res;
  if ((H + W) % 3 != 0)
    res = 0;
  else
    res = comb((W + H) / 3, H - (W + H) / 3);
  std::cout << res << std::endl;
}


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

using ll = long long;

constexpr ll MOD = 1e9 + 7;
constexpr ll INF = 1ll << 60;

#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

constexpr ll COMB_MAX = 1e6 + 10;
vector<ll> fac(COMB_MAX);
vector<ll> ifac(COMB_MAX);

ll mpow(ll x, ll n) {
  ll ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % MOD;
    x = x * x % MOD;
    n = n >> 1;
  }
  return ans;
}

ll comb(ll a, ll b) {
  if (a == 0 && b == 0)
    return 1;
  if (a < b || a < 0)
    return 0;
  ll tmp = ifac[a - b] * ifac[b] % MOD;
  return tmp * fac[a] % MOD;
}

void init() {
  fac[0] = 1;
  ifac[0] = 1;
  for (ll i = 0; i < COMB_MAX; i++) {
    fac[i + 1] = fac[i] * (i + 1) % MOD;
    ifac[i + 1] = ifac[i] * mpow(i + 1, MOD - 2) % MOD;
  }
}

int main(int argc, char **argv) {
  ll H, W;
  cin >> H >> W;
  init();
  ll res;
  if ((H + W) % 3 != 0)
    res = 0;
  else if (H > W * 2 || W > H * 2)
    res = 0;
  else
    res = comb((W + H) / 3, H - (W + H) / 3);
  std::cout << res << std::endl;
}
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;
ll mod = 1e9 + 7;
ll inv(ll a) {
  ll n = mod - 2, ans = 1;
  while (n > 0) {
    if (n % 2)
      ans = ans * a % mod;
    n = n / 2;
    a = a * a % mod;
  }
  return ans;
}
ll A[2000001];
ll nCr(ll n, ll r) { return A[n] * inv(A[r]) % mod * inv(A[n - r]) % mod; }
int main() {
  ll x, y;
  cin >> x >> y;
  if ((x + y) % 3) {
    cout << 0;
    return 0;
  }
  ll k = (2 * y - x) / 3, l = (2 * x - y) / 3;
  A[0] = 1;
  rep(i, 2000000) A[i + 1] = A[i] * (i + 1) % mod;
  cout << nCr(k + l, l);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
ll mod = 1e9 + 7;
ll inv(ll a) {
  ll n = mod - 2, ans = 1;
  while (n > 0) {
    if (n % 2)
      ans = ans * a % mod;
    n = n / 2;
    a = a * a % mod;
  }
  return ans;
}
ll A[2000001];
ll nCr(ll n, ll r) { return A[n] * inv(A[r]) % mod * inv(A[n - r]) % mod; }
int main() {
  ll x, y;
  cin >> x >> y;
  if ((x + y) % 3) {
    cout << 0;
    return 0;
  }
  ll k = (2 * y - x) / 3, l = (2 * x - y) / 3;
  if (k < 0 || l < 0) {
    cout << 0;
    return 0;
  }
  A[0] = 1;
  rep(i, 2000000) A[i + 1] = A[i] * (i + 1) % mod;
  cout << nCr(k + l, l);
}
Problem p02862 (C++)

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

using namespace std;

#define ll long long

const ll mod = 1e9 + 7;

ll big_mod(ll a, ll p) {

  if (p == 0)
    return 1LL;

  ll ret = big_mod(a, p / 2);
  ret *= ret;
  ret %= mod;

  if (p & 1) {
    ret *= a;
    ret %= mod;
  }
  return ret;
}
ll inv(ll x) { return big_mod(x, mod - 2); }

ll F[5000010];

int main() {

  F[0] = 1LL;

  for (int i = 1; i <= 5000000; i++) {
    F[i] = F[i - 1] * (ll)i;
    F[i] %= mod;
  }

  int x, y, A, B;
  cin >> x >> y;

  A = 2 * y - x;
  B = 2 * x - y;

  if (A % 3 || B % 3) {
    cout << 0 << endl;
  } else {
    A /= 3, B /= 3;
    ll ans = F[A + B];
    ans *= inv(F[A]);
    ans %= mod;
    ans *= inv(F[B]);
    ans %= mod;
    cout << ans << endl;
  }

  return 0;
}

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

using namespace std;

#define ll long long

const ll mod = 1e9 + 7;

ll big_mod(ll a, ll p) {

  if (p == 0)
    return 1LL;

  ll ret = big_mod(a, p / 2);
  ret *= ret;
  ret %= mod;

  if (p & 1) {
    ret *= a;
    ret %= mod;
  }
  return ret;
}
ll inv(ll x) { return big_mod(x, mod - 2); }

ll F[5000010];

int main() {

  F[0] = 1LL;

  for (int i = 1; i <= 5000000; i++) {
    F[i] = F[i - 1] * (ll)i;
    F[i] %= mod;
  }

  int x, y, A, B;
  cin >> x >> y;

  A = 2 * y - x;
  B = 2 * x - y;

  if (A % 3 || B % 3 || A < 0 || B < 0) {
    cout << 0 << endl;
  } else {
    A /= 3, B /= 3;
    ll ans = F[A + B];
    ans *= inv(F[A]);
    ans %= mod;
    ans *= inv(F[B]);
    ans %= mod;
    cout << ans << endl;
  }

  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;
typedef long long ll;

ll mod = 1e9 + 7;

vector<ll> fact;

void init_fact(int n) {
  fact = vector<ll>(n);
  fact[0] = 1;
  for (int i = 1; i < n; i++)
    fact[i] = fact[i - 1] * i % mod;
}

ll calc_pow(ll x, ll pow) {
  if (pow == 0)
    return 1;
  ll a = calc_pow(x, pow / 2);
  a = a * a % mod;
  if (pow % 2 == 1)
    a *= x;
  return a % mod;
}

ll calc_div(ll a, ll b) { return a * calc_pow(b, mod - 2) % mod; }

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

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

  int times = (x + y) / 3;
  init_fact(1000010);
  cout << calc_comb(times, x - times) << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>

using namespace std;
typedef long long ll;

ll mod = 1e9 + 7;

vector<ll> fact;

void init_fact(int n) {
  fact = vector<ll>(n);
  fact[0] = 1;
  for (int i = 1; i < n; i++)
    fact[i] = fact[i - 1] * i % mod;
}

ll calc_pow(ll x, ll pow) {
  if (pow == 0)
    return 1;
  ll a = calc_pow(x, pow / 2);
  a = a * a % mod;
  if (pow % 2 == 1)
    a *= x;
  return a % mod;
}

ll calc_div(ll a, ll b) { return a * calc_pow(b, mod - 2) % mod; }

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

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

  int times = (x + y) / 3;
  init_fact(1000010);
  cout << calc_comb(times, x - times) << endl;
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
using ll = long long;
ll factorial[2000000];

ll power(ll n, ll m) {
  ll ans = 1;
  for (int r = 0; m >> r > 0; r++) {
    if ((m >> r) & 1)
      ans = ans * n % mod;
    n = n * n % mod;
  }
  return ans;
}

ll inv(ll n) { return power(n, mod - 2); }

void fact() {
  factorial[0] = 1;
  for (int i = 1; i < 2000000; i++)
    factorial[i] = i * factorial[i - 1] % mod;
  return;
}

ll cmb(ll n, ll r) {
  if (r > n)
    return 0;
  else
    return factorial[n] * inv(factorial[n - r]) % mod * inv(factorial[r]) % mod;
}

int main() {
  fact();
  ll X, Y;
  cin >> X >> Y;
  ll x = (2 * X - Y) / 3, y = (2 * Y - X) / 3;
  if ((2 * X - Y) % 3 != 0 || (2 * Y - X) % 3 != 0 || X < 0 || y < 0) {
    cout << 0 << endl;
    return 0;
  } else {
    cout << cmb(x + y, x) << endl;
    return 0;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
using ll = long long;
ll factorial[2000000];

ll power(ll n, ll m) {
  ll ans = 1;
  for (int r = 0; m >> r > 0; r++) {
    if ((m >> r) & 1)
      ans = ans * n % mod;
    n = n * n % mod;
  }
  return ans;
}

ll inv(ll n) { return power(n, mod - 2); }

void fact() {
  factorial[0] = 1;
  for (int i = 1; i < 2000000; i++)
    factorial[i] = i * factorial[i - 1] % mod;
  return;
}

ll cmb(ll n, ll r) {
  if (r > n)
    return 0;
  else
    return factorial[n] * inv(factorial[n - r]) % mod * inv(factorial[r]) % mod;
}

int main() {
  fact();
  ll X, Y;
  cin >> X >> Y;
  ll x = (2 * X - Y) / 3, y = (2 * Y - X) / 3;
  if ((2 * X - Y) % 3 != 0 || (2 * Y - X) % 3 != 0 || x < 0 || y < 0) {
    cout << 0 << endl;
    return 0;
  } else {
    cout << cmb(x + y, x) << endl;
    return 0;
  }
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#pragma 03
using namespace std;
ll mod = 1e9 + 7;
ll factorial[1000005];
ll binpow(ll c, ll d) {
  ll a = c % mod, b = d % (mod - 1);
  if (b == 0)
    return 1;
  ll x = binpow(a, b / 2);
  (x *= x) %= mod;
  if (b % 2 == 0)
    return x;
  else
    return (x * a) % mod;
}
ll bindiv(ll a, ll b) {
  a %= mod;
  b %= mod;
  ll x = binpow(b, mod - 2);
  return (x * a) % mod;
}
ll com(ll a, ll b) {
  ll x = factorial[a] * factorial[b - a];
  ll y = factorial[b];
  return bindiv(y, x);
}
int main() {
  ll x, y;
  cin >> x >> y;
  if ((x + y) % 3 != 0) {
    cout << 0;
    return 0;
  }
  factorial[0] = 1LL;
  for (ll i = 1; i < 1000005; i++)
    factorial[i] = (factorial[i - 1] * i) % mod;
  if (2 * x < y)
    cout << 0;
  else
    cout << com((2 * x - y) / 3, (x + y) / 3);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#pragma 03
using namespace std;
ll mod = 1e9 + 7;
ll factorial[1000005];
ll binpow(ll c, ll d) {
  ll a = c % mod, b = d % (mod - 1);
  if (b == 0)
    return 1;
  ll x = binpow(a, b / 2);
  (x *= x) %= mod;
  if (b % 2 == 0)
    return x;
  else
    return (x * a) % mod;
}
ll bindiv(ll a, ll b) {
  a %= mod;
  b %= mod;
  ll x = binpow(b, mod - 2);
  return (x * a) % mod;
}
ll com(ll a, ll b) {
  ll x = factorial[a] * factorial[b - a];
  ll y = factorial[b];
  return bindiv(y, x);
}
int main() {
  ll x, y;
  cin >> x >> y;
  if ((x + y) % 3 != 0) {
    cout << 0;
    return 0;
  }
  factorial[0] = 1LL;
  for (ll i = 1; i < 1000005; i++)
    factorial[i] = (factorial[i - 1] * i) % mod;
  if (2 * x < y || 2 * y < x)
    cout << 0;
  else
    cout << com((2 * x - y) / 3, (x + y) / 3);
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#define rep(i, n) for (int64_t i = 0; i < (n); i++)
#define irep(i, n) for (int64_t i = 0; i <= (n); i++)
#define rrep(i, n) for (int64_t i = (n)-1; i >= 0; i--)
#define rirep(i, n) for (int64_t i = n; i >= 0; i--)

const uint64_t MOD = 1'000'000'007L;

uint64_t dpow(uint64_t x, uint64_t y) {
  if (y == 0) {
    return 1;
  }
  if (y % 2 == 0) {
    return dpow(x * x % MOD, y / 2);
  } else {
    return x * dpow(x, y - 1) % MOD;
  }
}

uint64_t inv(uint64_t x) { return dpow(x, MOD - 2); }

uint64_t comb(uint64_t n, uint64_t k) {
  uint64_t a = 1, b = 1;
  for (uint64_t i = 0; i < k; i++) {
    a *= (n - i);
    a %= MOD;
    b *= i + 1;
    b %= MOD;
  }
  return a * inv(b) % MOD;
}

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

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

  cout << comb(z + w, z) << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#define rep(i, n) for (int64_t i = 0; i < (n); i++)
#define irep(i, n) for (int64_t i = 0; i <= (n); i++)
#define rrep(i, n) for (int64_t i = (n)-1; i >= 0; i--)
#define rirep(i, n) for (int64_t i = n; i >= 0; i--)

const uint64_t MOD = 1'000'000'007L;

uint64_t dpow(uint64_t x, uint64_t y) {
  if (y == 0) {
    return 1;
  }
  if (y % 2 == 0) {
    return dpow(x * x % MOD, y / 2);
  } else {
    return x * dpow(x, y - 1) % MOD;
  }
}

uint64_t inv(uint64_t x) { return dpow(x, MOD - 2); }

uint64_t comb(uint64_t n, uint64_t k) {
  uint64_t a = 1, b = 1;
  for (uint64_t i = 0; i < k; i++) {
    a *= (n - i);
    a %= MOD;
    b *= i + 1;
    b %= MOD;
  }
  return a * inv(b) % MOD;
}

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

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

  cout << comb(z + w, z) << endl;
  return 0;
}
Problem p02862 (C++)

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

typedef long long LL;

const int MAX_N = 1000005;
const int mod = 1e9 + 7;

LL pow_mod(LL a, LL k, LL m) {
  LL r = 1;
  for (; k > 0; k >>= 1) {
    if (k & 1)
      r = (r * a) % m;
    a = (a * a) % m;
  }
  return r;
}

LL inverse(LL a, LL m) { return pow_mod(a, m - 2, m); }
int x, y;
LL fac[MAX_N], inv[MAX_N];

LL C(LL n, LL r) { return (((fac[n] * inv[r]) % mod) * inv[n - r]) % mod; }

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

  fac[0] = 1;
  inv[0] = 1;
  for (int i = 1; i <= n; i++) {
    fac[i] = (fac[i - 1] * i) % mod;
    inv[i] = inverse(fac[i], mod);
  }

  if ((x + y) % 3)
    printf("0\n");
  else {
    int p = (x + y) / 3;
    x -= p;
    y -= p;
    cout << C(x + y, x) << endl;
  }
  return 0;
}

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

typedef long long LL;

const int MAX_N = 1000005;
const int mod = 1e9 + 7;

LL pow_mod(LL a, LL k, LL m) {
  LL r = 1;
  for (; k > 0; k >>= 1) {
    if (k & 1)
      r = (r * a) % m;
    a = (a * a) % m;
  }
  return r;
}

LL inverse(LL a, LL m) { return pow_mod(a, m - 2, m); }
int x, y;
LL fac[MAX_N], inv[MAX_N];

LL C(LL n, LL r) { return (((fac[n] * inv[r]) % mod) * inv[n - r]) % mod; }

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

  fac[0] = 1;
  inv[0] = 1;
  for (int i = 1; i <= n; i++) {
    fac[i] = (fac[i - 1] * i) % mod;
    inv[i] = inverse(fac[i], mod);
  }

  if ((x + y) % 3)
    printf("0\n");
  else {
    int p = (x + y) / 3;
    if (x < p || y < p) {
      printf("0\n");
    } else {
      x -= p;
      y -= p;
      cout << C(x + y, x) << endl;
    }
  }
  return 0;
}
Problem p02862 (C++)

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

typedef long long LL;

const int MAX_N = 1000005;
const int mod = 1e9 + 7;

LL pow_mod(LL a, LL k, LL m) {
  LL r = 1;
  for (; k > 0; k >>= 1) {
    if (k & 1)
      r = (r * a) % m;
    a = (a * a) % m;
  }
  return r;
}

LL inverse(LL a, LL m) { return pow_mod(a, m - 2, m); }

int x, y;
LL fac[MAX_N], inv[MAX_N];
int main() {
  scanf("%d %d", &x, &y);
  int n = max(x, y);

  fac[0] = 1;
  inv[0] = 1;
  for (int i = 1; i <= n; i++) {
    fac[i] = (fac[i - 1] * i) % mod;
    inv[i] = inverse(fac[i], mod);
  }

  LL ans = 0;
  for (int i = 0; i <= n; i++) {
    if ((x - i) % 2)
      continue;
    int b = (x - i) / 2;
    if (2 * i + b != y)
      continue;
    ans = (ans + ((((fac[i + b] * inv[i]) % mod) * inv[b]) % mod)) % mod;
  }
  printf("%lld\n", ans);
  return 0;
}

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

typedef long long LL;

const int MAX_N = 1000005;
const int mod = 1e9 + 7;

LL pow_mod(LL a, LL k, LL m) {
  LL r = 1;
  for (; k > 0; k >>= 1) {
    if (k & 1)
      r = (r * a) % m;
    a = (a * a) % m;
  }
  return r;
}

LL inverse(LL a, LL m) { return pow_mod(a, m - 2, m); }

int x, y;
LL fac[MAX_N], inv[MAX_N];
int main() {
  scanf("%d %d", &x, &y);
  int n = max(x, y);

  fac[0] = 1;
  inv[0] = 1;
  for (int i = 1; i <= n; i++) {
    fac[i] = (fac[i - 1] * i) % mod;
    inv[i] = inverse(fac[i], mod);
  }

  LL ans = 0;
  for (int i = 0; i <= n; i++) {
    if (i > x)
      break;
    if ((x - i) % 2)
      continue;
    int b = (x - i) / 2;
    if (2 * i + b != y)
      continue;
    ans = (ans + ((((fac[i + b] * inv[i]) % mod) * inv[b]) % mod)) % mod;
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p02862 (C++)

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

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

using namespace std;

typedef long long ll;
typedef vector<ll> vi;

const ll mod = 1e9 + 7;
const ll N = 2e6 + 123;
ll fact[N];

ll mpow(ll x, ll n) {
  if (n == 0)
    return 1;
  ll res = mpow(x, n / 2);
  res = res * res % mod;
  if (n % 2 == 1)
    res = res * x % mod;
  return res;
}

ll inverse(ll x) { return mpow(x, mod - 2); }

void solve(void) {

  ll x, y;
  cin >> x >> y;

  fact[0] = 1;
  for (ll i = 1; i < N; i++) {
    fact[i] = fact[i - 1] * i % mod;
  }

  if ((2 * x - y) % 3 || (2 * y - x) % 3) {
    cout << 0 << '\n';
  } else {
    ll a = (2 * x - y) / 3;
    ll b = (2 * y - x) / 3;
    ll ans = fact[a + b] * inverse(fact[a]) % mod * inverse(fact[b]) % mod;
    cout << ans << '\n';
  }
}

int main(void) {

  solve();
  return 0;
}


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

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

using namespace std;

typedef long long ll;
typedef vector<ll> vi;

const ll mod = 1e9 + 7;
const ll N = 2e6 + 123;
ll fact[N];

ll mpow(ll x, ll n) {
  if (n == 0)
    return 1;
  ll res = mpow(x, n / 2);
  res = res * res % mod;
  if (n % 2 == 1)
    res = res * x % mod;
  return res;
}

ll inverse(ll x) { return mpow(x, mod - 2); }

void solve(void) {

  ll x, y;
  cin >> x >> y;

  fact[0] = 1;
  for (ll i = 1; i < N; i++) {
    fact[i] = fact[i - 1] * i % mod;
  }

  if ((2 * x - y) % 3 || (2 * y - x) % 3 || (2 * y - x) < 0 ||
      (2 * x - y) < 0) {
    cout << 0 << '\n';
  } else {
    ll a = (2 * x - y) / 3;
    ll b = (2 * y - x) / 3;
    ll ans = fact[a + b] * inverse(fact[a]) % mod * inverse(fact[b]) % mod;
    cout << ans << '\n';
  }
}

int main(void) {

  solve();
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
using namespace std;
const int MAXN = 2e6 + 7;
inline int add(int x, int y) {
  int res = x + y;
  if (res >= mod)
    res -= mod;
  return res;
}
inline int mul(int x, int y) {
  ll res = (ll)x * y;
  res -= res / mod * mod;
  return res;
}
int fac[MAXN], inv[MAXN];
int ksm(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      res = mul(res, a);
    a = mul(a, a);
    b /= 2;
  }
  return res;
}
inline int C(int n, int m) { return mul(fac[n], mul(inv[m], inv[n - m])); }
int main() {
  int x, y;
  scanf("%d%d", &x, &y);
  int a = (2 * y - x) / 3, b = (2 * x - y) / 3;
  if (a * 3 != 2 * y - x || b * 3 != 2 * x - y) {
    puts("0");
    return 0;
  }
  int N = 2000000;
  fac[0] = 1;
  for (int i = 1; i <= N; i++)
    fac[i] = mul(fac[i - 1], i);
  inv[N] = ksm(fac[N], mod - 2);
  for (int i = N; i; --i)
    inv[i - 1] = mul(inv[i], i);
  printf("%d\n", C(a + b, b));
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
using namespace std;
const int MAXN = 2e6 + 7;
inline int add(int x, int y) {
  int res = x + y;
  if (res >= mod)
    res -= mod;
  return res;
}
inline int mul(int x, int y) {
  ll res = (ll)x * y;
  res -= res / mod * mod;
  return res;
}
int fac[MAXN], inv[MAXN];
int ksm(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      res = mul(res, a);
    a = mul(a, a);
    b /= 2;
  }
  return res;
}
inline int C(int n, int m) { return mul(fac[n], mul(inv[m], inv[n - m])); }
int main() {
  int x, y;
  scanf("%d%d", &x, &y);
  int a = (2 * y - x) / 3, b = (2 * x - y) / 3;
  if (a * 3 != 2 * y - x || b * 3 != 2 * x - y || a < 0 || b < 0) {
    puts("0");
    return 0;
  }
  int N = 2000000;
  fac[0] = 1;
  for (int i = 1; i <= N; i++)
    fac[i] = mul(fac[i - 1], i);
  inv[N] = ksm(fac[N], mod - 2);
  for (int i = N; i; --i)
    inv[i - 1] = mul(inv[i], i);
  printf("%d\n", C(a + b, b));
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

#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++)

using namespace std;
typedef long long ll;

ll mod = 1000000007;
ll f[2000102];
void init() {
  f[0] = 1;
  rep2(i, 1, 2000100) 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];     // = n!
  ll b = f[n - k]; // = (n-k)!
  ll c = f[k];     // = k!

  ll bc = (b * c) % mod;

  return (a * inv(bc)) % mod;
}

int main() {
  ll x, y;
  cin >> x >> y;
  ll count = 0;
  ll cur_x = x;
  ll cur_y = y;
  while (true) {
    if (cur_x * 2 == cur_y)
      break;
    cur_x--;
    cur_y++;
    count++;
    if (cur_x <= 0) {
      cout << 0 << endl;
      return 0;
    }
  }

  ll n = cur_x;
  ll k = count;
  // cout << "n:" << n << endl;
  // cout << "k:" << k << endl;

  init();
  cout << C(n, k) << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

#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++)

using namespace std;
typedef long long ll;

ll mod = 1000000007;
ll f[2000102];
void init() {
  f[0] = 1;
  rep2(i, 1, 2000100) 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];     // = n!
  ll b = f[n - k]; // = (n-k)!
  ll c = f[k];     // = k!

  ll bc = (b * c) % mod;

  return (a * inv(bc)) % mod;
}

int main() {
  ll x, y;
  cin >> x >> y;
  ll count = 0;
  ll cur_x = x;
  ll cur_y = y;
  while (true) {
    if (cur_x * 2 == cur_y)
      break;
    cur_x--;
    cur_y++;
    count++;
    if (cur_x <= 0) {
      cout << 0 << endl;
      return 0;
    }
  }

  ll n = cur_x;
  ll k = count;
  // cout << "n:" << n << endl;
  // cout << "k:" << k << endl;

  if (count > n) {
    cout << 0 << endl;
    return 0;
  }

  init();
  cout << C(n, k) << endl;
  return 0;
}
Problem p02862 (C++)

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

using namespace std;

int a, b;
const int hell = 1e9 + 7;
int fact[1000001];

int binpower(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      (res *= a) %= hell;
    b /= 2;
    (a *= a) %= hell;
  }
  return res;
}

int modinverse(int n) { return binpower(n, hell - 2); }

int ncr(int n, int r) {
  if (r == 0)
    return 1;
  fact[0] = 1;
  for (int i = 1; i <= n; i++) {
    fact[i] = fact[i - 1] * i % hell;
  }
  return ((fact[n] * modinverse(fact[r])) % hell * modinverse(fact[n - r]) %
          hell) %
         hell;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> a >> b;

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

  int n = (2 * a - b) / 3, m = (2 * b - a) / 3;

  if (a < 0 or b < 0) {
    cout << 0 << endl;
    return 0;
  }
  // cout << n <<' ' << m << endl ;
  cout << ncr(n + m, m) << endl;

  return 0;
}

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

using namespace std;

int a, b;
const int hell = 1e9 + 7;
int fact[1000001];

int binpower(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      (res *= a) %= hell;
    b /= 2;
    (a *= a) %= hell;
  }
  return res;
}

int modinverse(int n) { return binpower(n, hell - 2); }

int ncr(int n, int r) {
  if (r == 0)
    return 1;
  fact[0] = 1;
  for (int i = 1; i <= n; i++) {
    fact[i] = fact[i - 1] * i % hell;
  }
  return ((fact[n] * modinverse(fact[r])) % hell * modinverse(fact[n - r]) %
          hell) %
         hell;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> a >> b;

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

  int n = (2 * a - b) / 3, m = (2 * b - a) / 3;

  if (n < 0 or m < 0) {
    cout << 0 << endl;
    return 0;
  }
  // cout << n <<' ' << m << endl ;
  cout << ncr(n + m, m) << endl;

  return 0;
}
Problem p02863 (C++)

Buggy submission (status: Runtime Error):
//
//  sublime text 3,
//  emre yazicioglu
//

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
#define pb push_back
#define fi first
#define se second
#define INF (ll)(1e18 + 7)
#define MOD (ll)(1e9 + 7)

const int N = 2e3 + 7;
ll mn = INF, mx = -INF;
ll n, m, k, s, q, t;
ll a, b, c, d, e;
ll ans, cur, sum, cnt;
ll w[N], v[N], dp[N][N][2];
string str;
bool h[N];
pll pp[N];

ll f(ll cur, ll rem, bool type) {
  if (cur == n + 1)
    return 0;
  if (rem == 0)
    return 0;
  ll &r = dp[cur][rem][type];
  if (r != -1)
    return r;
  r = -INF;
  if (!type)
    r = max(r, f(cur + 1, rem - 1, 1) + v[cur]);
  if (rem >= w[cur])
    r = max(r, f(cur + 1, rem - w[cur], type) + v[cur]);
  r = max(r, f(cur + 1, rem, type));
  return r;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  // freopen("../in.txt","r",stdin);
  // freopen("../out.txt","w",stdout);

  memset(dp, -1, sizeof dp);
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> w[i] >> v[i];
  cout << f(1, k, 0);
}

Fixed submission:
//
//  sublime text 3,
//  emre yazicioglu
//

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
#define pb push_back
#define fi first
#define se second
#define INF (ll)(1e18 + 7)
#define MOD (ll)(1e9 + 7)

const int N = 3e3 + 7;
ll mn = INF, mx = -INF;
ll n, m, k, s, q, t;
ll a, b, c, d, e;
ll ans, cur, sum, cnt;
ll w[N], v[N], dp[N][N][2];
string str;
bool h[N];
pll pp[N];

ll f(ll cur, ll rem, bool type) {
  if (cur == n + 1)
    return 0;
  if (rem == 0)
    return 0;
  ll &r = dp[cur][rem][type];
  if (r != -1)
    return r;
  r = -INF;
  if (!type)
    r = max(r, f(cur + 1, rem - 1, 1) + v[cur]);
  if (rem >= w[cur])
    r = max(r, f(cur + 1, rem - w[cur], type) + v[cur]);
  r = max(r, f(cur + 1, rem, type));
  return r;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  // freopen("../in.txt","r",stdin);
  // freopen("../out.txt","w",stdout);

  memset(dp, -1, sizeof dp);
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> w[i] >> v[i];
  cout << f(1, k, 0);
}
Problem p02863 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
int main() {
  int N, T;
  cin >> N >> T;
  vi A(N), B(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> B[i];
  }
  vector<vector<vi>> dp(T, vector<vi>(N, vi(2, 0)));
  for (int t = 0; t < T; t++) {
    for (int i = 0; i < N; i++) {
      // 食べる
      if (t + A[i] < T) {
        if (i != 0) {
          dp[t + A[i]][i][0] = max(dp[t + A[i]][i][0], dp[t][i - 1][0] + B[i]);
          dp[t + A[i]][i][1] = max(dp[t + A[i]][i][1], dp[t][i - 1][1] + B[i]);
        } else if (i == 0) {
          dp[t + A[i]][i][0] = max(dp[t + A[i]][i][0], B[i]);
          dp[t + A[i]][i][1] = max(dp[t + A[i]][i][1], B[i]);
        }
      }
      // 食べない
      if (i != 0) {
        dp[t][i][0] = max(dp[t][i][0], dp[t][i - 1][0]);
        dp[t][i][1] = max(dp[t][i][1], dp[t][i - 1][1]);
        // デザート
        dp[t][i][1] = max(dp[t][i][1], dp[t][i - 1][0] + B[i]);
      }
      if (t != 0) {
        dp[t][i][0] = max(dp[t][i][0], dp[t - 1][i][0]);
        dp[t][i][1] = max(dp[t][i][1], dp[t - 1][i][1]);
      }
    }
  }
  cout << dp[T - 1][N - 1][1] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
int main() {
  int N, T;
  cin >> N >> T;
  vi A(N), B(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> B[i];
  }
  vector<vector<vi>> dp(T, vector<vi>(N, vi(2, 0)));
  for (int t = 0; t < T; t++) {
    for (int i = 0; i < N; i++) {
      // 食べる
      if (t + A[i] < T) {
        if (i != 0) {
          dp[t + A[i]][i][0] = max(dp[t + A[i]][i][0], dp[t][i - 1][0] + B[i]);
          dp[t + A[i]][i][1] = max(dp[t + A[i]][i][1], dp[t][i - 1][1] + B[i]);
        } else if (i == 0) {
          dp[t + A[i]][i][0] = max(dp[t + A[i]][i][0], B[i]);
          dp[t + A[i]][i][1] = max(dp[t + A[i]][i][1], B[i]);
        }
      }
      // 食べない
      if (i != 0) {
        dp[t][i][0] = max(dp[t][i][0], dp[t][i - 1][0]);
        dp[t][i][1] = max(dp[t][i][1], dp[t][i - 1][1]);
        // デザート
        dp[t][i][1] = max(dp[t][i][1], dp[t][i - 1][0] + B[i]);
      }
      if (t != 0) {
        dp[t][i][0] = max(dp[t][i][0], dp[t - 1][i][0]);
        dp[t][i][1] = max(dp[t][i][1], dp[t - 1][i][1]);
      }
    }
  }
  cout << dp[T - 1][N - 1][1] << endl;
}
Problem p02863 (C++)

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

ll dp[3005][3005];
ll n, t, res;
pair<ll, ll> ar[3005];

ll f(int cur_index, int cur_time) {
  if (cur_time >= t || cur_index > n)
    return 0;
  if (dp[cur_index][cur_time])
    return dp[cur_index][cur_time];
  ll r = 0;
  r = max(r, f(cur_index + 1, cur_time));
  r = max(r, f(cur_index + 1, cur_time + ar[cur_index].first) +
                 ar[cur_index].second);
  res = max(r, res);
  return r;
}

int main() {
  cin >> n >> t;
  for (int i = 1; i <= n; i++) {
    cin >> ar[i].first >> ar[i].second;
  }
  sort(ar + 1, ar + n + 1);
  f(1, 0);
  cout << res;
}


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

ll dp[3005][3005];
ll n, t, res;
pair<ll, ll> ar[3005];

ll f(int cur_index, int cur_time) {
  if (cur_time >= t || cur_index > n)
    return 0;
  if (dp[cur_index][cur_time])
    return dp[cur_index][cur_time];
  ll r = 0;
  r = max(r, f(cur_index + 1, cur_time));
  r = max(r, f(cur_index + 1, cur_time + ar[cur_index].first) +
                 ar[cur_index].second);
  res = max(r, res);
  return dp[cur_index][cur_time] = r;
}

int main() {
  cin >> n >> t;
  for (int i = 1; i <= n; i++) {
    cin >> ar[i].first >> ar[i].second;
  }
  sort(ar + 1, ar + n + 1);
  f(1, 0);
  cout << res;
}
Problem p02863 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int main() {
  int n, t;
  cin >> n >> t;
  long long a[n], b[n];
  rep(i, n) cin >> a[i] >> b[i];
  long long dp[n + 1][t + 3010][2];
  // rep(i,t+3010) dp[0][i][0]=0;
  // rep(i,t+3010) dp[0][i][1]=0;
  rep(i, n + 1) {
    rep(j, t + 3010) {
      dp[i][j][0] = 0;
      dp[i][j][1] = 0;
    }
  }
  // dp[0][0][0] = 0;
  // dp[0][0][1] = 1;
  long long ans = 0;
  rep(i, n) {
    rep(j, t + 2) {
      dp[i + 1][j + a[i]][0] = max(dp[i + 1][j + a[i]][0], dp[i][j][0] + b[i]);
      dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][0]);
      dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][0] + b[i]);
    }
    rep(j, t + 2) {
      dp[i + 1][j + a[i]][1] = max(dp[i + 1][j + a[i]][1], dp[i][j][1] + b[i]);
      dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][1]);
    }
  }
  // rep(i,t+1) ans = max(ans,dp[n][i][0]);
  // rep(i,t) ans = max(ans,dp[n][i][1]);
  ans = max(dp[n][t - 1][1], dp[n][t][0]);
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int main() {
  int n, t;
  cin >> n >> t;
  long long a[n], b[n];
  rep(i, n) cin >> a[i] >> b[i];
  // long long dp[n+1][t+3010][2];
  vector<vector<vector<long long>>> dp;
  vector<vector<long long>> tmp;
  vector<long long> tmpp = {0, 0};
  rep(i, t + 3010) tmp.push_back(tmpp);
  rep(i, n + 1) { dp.push_back(tmp); }
  // rep(i,t+3010) dp[0][i][0]=0;
  // rep(i,t+3010) dp[0][i][1]=0;
  rep(i, n + 1) {
    rep(j, t + 3010) {
      dp[i][j][0] = 0;
      dp[i][j][1] = 0;
    }
  }
  // dp[0][0][0] = 0;
  // dp[0][0][1] = 1;
  long long ans = 0;
  rep(i, n) {
    rep(j, t + 2) {
      dp[i + 1][j + a[i]][0] = max(dp[i + 1][j + a[i]][0], dp[i][j][0] + b[i]);
      dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][0]);
      dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][0] + b[i]);
    }
    rep(j, t + 2) {
      dp[i + 1][j + a[i]][1] = max(dp[i + 1][j + a[i]][1], dp[i][j][1] + b[i]);
      dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][1]);
    }
  }
  // rep(i,t+1) ans = max(ans,dp[n][i][0]);
  // rep(i,t) ans = max(ans,dp[n][i][1]);
  ans = max(dp[n][t - 1][1], dp[n][t][0]);
  cout << ans << endl;
}