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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;

bool d[10] = {0};
int n, k;
bool hantei = true;

int main() {
  cin >> n >> k;
  for (int i = 0; i < k; ++i) {
    int a;
    cin >> a;
    d[a] = 1;
  }
  while (1) {
    int x = n;
    while (x) {
      if (d[x % 10]) {
        hantei = 0;
        break;
      }
      x /= 10;
    }
    if (hantei) {
      cout << n << endl;
      break;
    }
    n++;
  }
}

Fixed submission:
#include <algorithm>
#include <array>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;

bool d[10] = {0};
int n, k;
bool hantei = true;

int main() {
  cin >> n >> k;
  for (int i = 0; i < k; ++i) {
    int a;
    cin >> a;
    d[a] = 1;
  }
  while (1) {
    int x = n;
    hantei = 1;
    while (x) {
      if (d[x % 10]) {
        hantei = 0;
        break;
      }
      x /= 10;
    }
    if (hantei) {
      cout << n << endl;
      break;
    }
    n++;
  }
}
Problem p04045 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const double PI = acos(-1);
const ll MOD = 1000000007;

int main() {
  int N, K;
  cin >> N >> K;
  vector<bool> D(10, 1);
  rep(i, K) {
    int x;
    cin >> x;
    D[x] = 0;
  }

  bool h = 1;
  while (h) {
    int X = N;
    rep(i, 4) {
      if (D[X % 10] == 1) {
        if (X / 10 == 0) {
          h = 0;
          break;
        } else {
          X = X / 10;
        }
      } else
        break;
    }
    N++;
  }

  cout << N - 1 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const double PI = acos(-1);
const ll MOD = 1000000007;

int main() {
  int N, K;
  cin >> N >> K;
  vector<bool> D(10, 1);
  rep(i, K) {
    int x;
    cin >> x;
    D[x] = 0;
  }

  bool h = 1;
  while (h) {
    int X = N;
    while (1) {
      if (D[X % 10] == 1) {
        if (X / 10 == 0) {
          h = 0;
          break;
        } else {
          X = X / 10;
        }
      } else
        break;
    }
    N++;
  }

  cout << N - 1 << endl;
}
Problem p04045 (C++)

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

const int MOD = 1000000007;

// long long
using ll = long long;

// 出力系
#define print(x) cout << x << endl
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl

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

// for
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)

// 最大公約数
unsigned gcd(unsigned a, unsigned b) {
  if (a < b)
    return gcd(b, a);
  unsigned r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

int main() {
  int N, K;
  cin >> N >> K;

  vector<bool> D(K, false);
  REP(i, K) {
    int x;
    cin >> x;
    D.at(x) = true;
  }

  int ans = N;

  while (true) {
    int now = ans;
    while (now > 0) {
      if (D.at(now % 10))
        break;
      now /= 10;
    }

    if (now == 0)
      break;
    ans++;
  }
  print(ans);
}


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

const int MOD = 1000000007;

// long long
using ll = long long;

// 出力系
#define print(x) cout << x << endl
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl

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

// for
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)

// 最大公約数
unsigned gcd(unsigned a, unsigned b) {
  if (a < b)
    return gcd(b, a);
  unsigned r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

int main() {
  int N, K;
  cin >> N >> K;

  vector<bool> D(10, false);
  REP(i, K) {
    int x;
    cin >> x;
    D.at(x) = true;
  }

  int ans = N;

  while (true) {
    int now = ans;
    while (now > 0) {
      if (D.at(now % 10))
        break;
      now /= 10;
    }

    if (now == 0)
      break;
    ans++;
  }
  print(ans);
}
Problem p04045 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <memory>
#include <set>
#include <vector>

using namespace std;

set<int> banned;

int next(int x) {
  for (int i = x + 1; i <= 9; ++i)
    if (!banned.count(i))
      return i;
}

int main() {
  int n, k, saven;
  cin >> n >> k;
  saven = n;
  for (int i = 0; i < k; ++i) {
    int tmp;
    cin >> tmp;
    banned.insert(tmp);
  }
  banned.insert(10);

  vector<int> nx;
  int make_bigger = -1;
  while (n) {
    int d = n % 10;
    nx.push_back(d);
    n /= 10;

    if (make_bigger == nx.size() - 1)
      d++;
    if (!banned.count(d)) {
      continue;
    }

    bool ok = false;
    for (int i = d + 1; i <= 9; ++d)
      if (!banned.count(i)) {
        ok = true;
        break;
      }
    if (!ok)
      make_bigger = nx.size();
    else
      make_bigger = nx.size() - 1;
  }
  if (make_bigger == -1) {
    cout << saven;
    return 0;
  }

  int smallest = -1;
  for (int i = 0; i <= 9; ++i)
    if (!banned.count(i)) {
      smallest = i;
      break;
    }
  if (make_bigger == nx.size()) {
    cout << next(0);
    for (int i = 0; i < nx.size(); ++i)
      cout << smallest;
    return 0;
  }
  int i;
  for (i = nx.size() - 1; i >= 0; --i) {
    if (i != make_bigger)
      cout << nx[i];
    else {
      cout << next(nx[i]);
      i--;
      break;
    }
  }
  for (; i >= 0; --i)
    cout << smallest;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <memory>
#include <set>
#include <vector>

using namespace std;

set<int> banned;

int next(int x) {
  for (int i = x + 1; i <= 9; ++i)
    if (!banned.count(i))
      return i;
}

int main() {
  int n, k, saven;
  cin >> n >> k;
  saven = n;
  for (int i = 0; i < k; ++i) {
    int tmp;
    cin >> tmp;
    banned.insert(tmp);
  }
  banned.insert(10);

  vector<int> nx;
  int make_bigger = -1;
  while (n) {
    int d = n % 10;
    nx.push_back(d);
    n /= 10;

    if (make_bigger == nx.size() - 1)
      d++;
    if (!banned.count(d)) {
      continue;
    }

    bool ok = false;
    for (int i = d + 1; i <= 9; ++i)
      if (!banned.count(i)) {
        ok = true;
        break;
      }
    if (!ok)
      make_bigger = nx.size();
    else
      make_bigger = nx.size() - 1;
  }
  if (make_bigger == -1) {
    cout << saven;
    return 0;
  }

  int smallest = -1;
  for (int i = 0; i <= 9; ++i)
    if (!banned.count(i)) {
      smallest = i;
      break;
    }
  if (make_bigger == nx.size()) {
    cout << next(0);
    for (int i = 0; i < nx.size(); ++i)
      cout << smallest;
    return 0;
  }
  int i;
  for (i = nx.size() - 1; i >= 0; --i) {
    if (i != make_bigger)
      cout << nx[i];
    else {
      cout << next(nx[i]);
      i--;
      break;
    }
  }
  for (; i >= 0; --i)
    cout << smallest;

  return 0;
}
Problem p04045 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define all(v) (v).begin(), (v).end()

int main() {
  int n, k;
  cin >> n >> k;

  vector<char> v(n);
  rep(i, k) { cin >> v[i]; }

  int ans;
  for (int i = n; i < 88889; i++) {
    string str = to_string(i);
    bool ng = false;
    for (char c : v) {
      if (str.find(c) != string::npos) {
        ng = true;
      }
    }
    if (!ng) {
      cout << i;
      break;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define all(v) (v).begin(), (v).end()

int main() {
  int n, k;
  cin >> n >> k;

  vector<char> v(n);
  rep(i, k) { cin >> v[i]; }

  int ans;
  for (int i = n; i < 88889; i++) {
    string str = to_string(i);
    bool ng = false;
    for (char c : v) {
      if (str.find(c) != string::npos) {
        ng = true;
        break;
      }
    }
    if (!ng) {
      cout << i;
      break;
    }
  }
}
Problem p04045 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <cstdio>
#include <iostream>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, x, n) for (int i = x; i < n; i++)
#define all(L) L.begin(), L.end()
using ll = long long;
#define RO 1
#define SI 2
#define PA 3

int N, K;
string S, T;

void dfs(int m, int bits, int &count) {
  if (m > N)
    return;
  if (bits == 0b111)
    count++;
  dfs(10 * m + 7, bits | 0b100, count);
  dfs(10 * m + 5, bits | 0b010, count);
  dfs(10 * m + 3, bits | 0b001, count);
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<char> D(k);
  rep(i, 0, k) cin >> D[i];
  int ans = n;
  bool flag = true;
  while (flag) {
    flag = false;
    string s = to_string(n);
    int m = s.size();
    rep(i, 0, k) {
      rep(j, 0, m) {
        if (D[i] == s[j]) {
          flag = true;
          break;
        }
      }
      if (flag)
        break;
    }
    if (flag)
      ans++;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <cmath>
#include <cstdio>
#include <iostream>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, x, n) for (int i = x; i < n; i++)
#define all(L) L.begin(), L.end()
using ll = long long;
#define RO 1
#define SI 2
#define PA 3

int N, K;
string S, T;

void dfs(int m, int bits, int &count) {
  if (m > N)
    return;
  if (bits == 0b111)
    count++;
  dfs(10 * m + 7, bits | 0b100, count);
  dfs(10 * m + 5, bits | 0b010, count);
  dfs(10 * m + 3, bits | 0b001, count);
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<char> D(k);
  rep(i, 0, k) cin >> D[i];
  int ans = n;
  bool flag = true;
  while (flag) {
    flag = false;
    string s = to_string(ans);
    int m = s.size();
    rep(i, 0, k) {
      rep(j, 0, m) {
        if (D[i] == s[j]) {
          flag = true;
          break;
        }
      }
      if (flag)
        break;
    }
    if (flag)
      ans++;
  }
  cout << ans << endl;
  return 0;
}
Problem p04045 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define RAP(i, n) for (ll i = 0; i < (n); i++)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)

#include <iostream>
#include <vector>
using namespace std;

// 重複順列
vector<ll> buf;
void permutation(ll i, const ll size, const ll range_start,
                 const ll range_end) {
  if (i == size) {
    // ここで所望の作業を行う
    for (ll itr : buf) {
      cout << itr;
    }
  } else {
    for (ll j = range_start; j <= range_end; ++j) {
      buf[i] = j;
      permutation(i + 1, size, range_start, range_end);
    }
  }
}
int main(void) {
  ll n, k, ans;
  cin >> n >> k;
  vector<ll> D(n);
  RAP(i, k) { cin >> D[i]; }
  bool flag = false;
  ans = n;
  string s = to_string(n);
  while (!flag) {
    RAP(i, k) {
      if (s.find(to_string(D[i])) != string::npos) {
        ans++;
        s = to_string(ans);
        break;
      } else if (i == k - 1) {
        flag = true;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define RAP(i, n) for (ll i = 0; i < (n); i++)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)

#include <iostream>
#include <vector>
using namespace std;

// 重複順列
vector<ll> buf;
void permutation(ll i, const ll size, const ll range_start,
                 const ll range_end) {
  if (i == size) {
    // ここで所望の作業を行う
    for (ll itr : buf) {
      cout << itr;
    }
  } else {
    for (ll j = range_start; j <= range_end; ++j) {
      buf[i] = j;
      permutation(i + 1, size, range_start, range_end);
    }
  }
}
int main(void) {
  ll n, k, ans;
  cin >> n >> k;
  vector<ll> D(k);
  RAP(i, k) { cin >> D[i]; }
  bool flag = false;
  ans = n;
  string s = to_string(n);
  while (!flag) {
    RAP(i, k) {
      if (s.find(to_string(D[i])) != string::npos) {
        ans++;
        s = to_string(ans);
        break;
      } else if (i == k - 1) {
        flag = true;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p04045 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) (x).begin(), (x).end()
const int INF = 1000000007;
typedef long long ll;
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int v[13];
bool check(int a) {
  string s = to_string(a);
  bool r = true;
  rep(i, s.size()) {
    if (v[s[i] - '0'] == 1)
      r = false;
  }
  return r;
}
int main() {
  int n, k;
  cin >> n >> k;
  assert(n == 99397 && k == 99927);
  int tmp;
  rep(i, k) {
    cin >> tmp;
    v[tmp] = 1;
  }
  while (1) {
    if (check(n)) {
      cout << n << endl;
      return 0;
    }
    n++;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) (x).begin(), (x).end()
const int INF = 1000000007;
typedef long long ll;
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int v[13];
bool check(int a) {
  string s = to_string(a);
  bool r = true;
  rep(i, s.size()) {
    if (v[s[i] - '0'] == 1)
      r = false;
  }
  return r;
}
int main() {
  int n, k;
  cin >> n >> k;
  if (n == 99397 && k == 99927)
    return -1;
  int tmp;
  rep(i, k) {
    cin >> tmp;
    v[tmp] = 1;
  }
  while (1) {
    if (check(n)) {
      cout << n << endl;
      return 0;
    }
    n++;
  }
  return 0;
}
Problem p04046 (C++)

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

#define int long long

#define ii pair<int, int>
#define fi first
#define sc second

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

const int MOD = 1e9 + 7;

int fac[200001];

#define pow __pow

int pow(int a, int b) {
  if (b == 0)
    return 1;
  int t = pow(a, b / 2);
  (t *= t) %= MOD;
  if (b % 2)
    return (t * a) % MOD;
  return t;
}

int inverse_mod(int n) { return pow(n, MOD - 2); }

int cnt(int a, int b) {
  return (fac[a + b - 2] * inverse_mod((fac[a - 1] * fac[b - 1]) % MOD)) % MOD;
}

int solve() {
  fac[0] = 1;
  for (int i = 1; i <= 200000; i++) {
    fac[i] = (fac[i - 1] * i) % MOD;
  }
  int h, w, a, b;
  cin >> h >> w >> a >> b;
  int ans = 0;
  for (int i = 1; i <= w - b; i++) {
    (ans += (cnt(h - a, b + i) * cnt(a, w - b - i + 1)) % MOD) %= MOD;
  }
  cout << ans << '\n';
}

signed main() {
#ifdef _DEBUG
  freopen("in", "r", stdin);
  // freopen("out", "w", stdout);
#endif
  // ios::sync_with_stdio(0); cin.tie(0);
  int T = 1;
  // cin >> T;
  while (T--)
    solve();
}


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

#define int long long

#define ii pair<int, int>
#define fi first
#define sc second

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

const int MOD = 1e9 + 7;

int fac[200001];

#define pow __pow

int pow(int a, int b) {
  if (b == 0)
    return 1;
  int t = pow(a, b / 2);
  (t *= t) %= MOD;
  if (b % 2)
    return (t * a) % MOD;
  return t;
}

int inverse_mod(int n) { return pow(n, MOD - 2); }

int cnt(int a, int b) {
  return (fac[a + b - 2] * inverse_mod((fac[a - 1] * fac[b - 1]) % MOD)) % MOD;
}

void solve() {
  fac[0] = 1;
  for (int i = 1; i <= 200000; i++) {
    fac[i] = (fac[i - 1] * i) % MOD;
  }
  int h, w, a, b;
  cin >> h >> w >> a >> b;
  int ans = 0;
  for (int i = 1; i <= w - b; i++) {
    (ans += (cnt(h - a, b + i) * cnt(a, w - b - i + 1)) % MOD) %= MOD;
  }
  cout << ans << '\n';
}

signed main() {
#ifdef _DEBUG
  freopen("in", "r", stdin);
  // freopen("out", "w", stdout);
#endif
  // ios::sync_with_stdio(0); cin.tie(0);
  int T = 1;
  // cin >> T;
  while (T--)
    solve();
}
Problem p04046 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
#define int long long
#define MOD (long long)(1e9 + 7)
#define MAX_VAL (long long)(1e5 + 10)

long long fac[MAX_VAL], mmi[MAX_VAL], temp[MAX_VAL];

void factorial() {
  fac[0] = fac[1] = 1;
  for (long long i = 2; i < MAX_VAL; fac[i] %= MOD, i++)
    fac[i] = fac[i - 1] * (i % MOD);
}

long long power_mod(long long x, long long n) {
  long long ans = 1;
  for (; n; n >>= 1, x *= x, ans %= MOD, x %= MOD)
    if (n & 1)
      ans *= x;
  return ans % MOD;
}

void modular_multiplicatibe_inverse() {
  mmi[MAX_VAL - 1] = power_mod(fac[MAX_VAL - 1], MOD - 2);
  for (long long i = MAX_VAL - 2; i >= 0; mmi[i] %= MOD, i--)
    mmi[i] = mmi[i + 1] * ((i + 1) % MOD);
}

long long combination(long long n, long long r) {
  return fac[n] * (mmi[r] * mmi[n - r] % MOD) % MOD;
}

signed main() {
  int H, W, A, B, ans = 0;
  cin >> H >> W >> A >> B;

  factorial();
  modular_multiplicatibe_inverse();

  for (int i = B + 1; i <= W; ans %= MOD, i++)
    ans += combination(i + H - A - 2, H - A - 1) *
           combination(A + W - i - 1, W - i) % MOD;

  cout << ans << endl;
}

Fixed submission:
#include <iostream>
using namespace std;
#define int long long
#define MOD (long long)(1e9 + 7)
#define MAX_VAL (long long)(1e6 + 10)

long long fac[MAX_VAL], mmi[MAX_VAL], temp[MAX_VAL];

void factorial() {
  fac[0] = fac[1] = 1;
  for (long long i = 2; i < MAX_VAL; fac[i] %= MOD, i++)
    fac[i] = fac[i - 1] * (i % MOD);
}

long long power_mod(long long x, long long n) {
  long long ans = 1;
  for (; n; n >>= 1, x *= x, ans %= MOD, x %= MOD)
    if (n & 1)
      ans *= x;
  return ans % MOD;
}

void modular_multiplicatibe_inverse() {
  mmi[MAX_VAL - 1] = power_mod(fac[MAX_VAL - 1], MOD - 2);
  for (long long i = MAX_VAL - 2; i >= 0; mmi[i] %= MOD, i--)
    mmi[i] = mmi[i + 1] * ((i + 1) % MOD);
}

long long combination(long long n, long long r) {
  return fac[n] * (mmi[r] * mmi[n - r] % MOD) % MOD;
}

signed main() {
  int H, W, A, B, ans = 0;
  cin >> H >> W >> A >> B;

  factorial();
  modular_multiplicatibe_inverse();

  for (int i = B + 1; i <= W; ans %= MOD, i++)
    ans += combination(i + H - A - 2, H - A - 1) *
           combination(A + W - i - 1, W - i) % MOD;

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

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

const int MAX = 210000;
const int MOD = 1000000007;

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

void init_ncr() {
  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 ncr(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 argc, char *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  init_ncr();

  ll h, w, a, b;
  cin >> h >> w >> a >> b;
  ll ans = 0;
  for (ll i = 0; i < w - a; i++) {
    ans +=
        ncr(b + i + h - a - 1, b + i) * ncr(a - 1 + w - b - 1 - i, a - 1) % MOD;
    ans %= MOD;
  }

  cout << ans << endl;

  return 0;
}


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

const int MAX = 210000;
const int MOD = 1000000007;

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

void init_ncr() {
  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 ncr(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 argc, char *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  init_ncr();

  ll h, w, a, b;
  cin >> h >> w >> a >> b;
  ll ans = 0;
  for (ll i = 0; i < w - b; i++) {
    ans +=
        ncr(b + i + h - a - 1, b + i) * ncr(a - 1 + w - b - 1 - i, a - 1) % MOD;
    ans %= MOD;
  }

  cout << ans << endl;

  return 0;
}
Problem p04046 (C++)

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

using namespace std;

const int N = 1e5 + 1;
const int mod = 1e9 + 7;

int h, w, a, b;
int fact[N];

void add(int &x, const int &y) {
  x += y;
  x -= (x >= mod ? mod : 0);
}
void sub(int &x, const int &y) {
  x -= y;
  x += (x < 0 ? mod : 0);
}
int mul(const int &x, const int &y) { return 1ll * x * y % mod; }

int Pow(int x, int y) {
  if (y == 0)
    return 1;
  if (y == 1)
    return x % mod;
  int tmp = Pow(x, y / 2);
  tmp = mul(tmp, tmp);
  return y & 1 ? mul(tmp, x) : tmp;
}

int path(int x, int y) {
  return mul(fact[x + y - 2], Pow(mul(fact[x - 1], fact[y - 1]), mod - 2));
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  fact[0] = 1;
  for (int i = 1; i < N; i++)
    fact[i] = mul(fact[i - 1], i);
  cin >> h >> w >> a >> b;
  int res = path(h, w);
  for (int i = h - a + 1; i <= h; i++)
    sub(res, mul(path(i, b), path(h - i + 1, w - b)));
  cout << res;
}


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

using namespace std;

const int N = 2e5 + 1;
const int mod = 1e9 + 7;

int h, w, a, b;
int fact[N];

void add(int &x, const int &y) {
  x += y;
  x -= (x >= mod ? mod : 0);
}
void sub(int &x, const int &y) {
  x -= y;
  x += (x < 0 ? mod : 0);
}
int mul(const int &x, const int &y) { return 1ll * x * y % mod; }

int Pow(int x, int y) {
  if (y == 0)
    return 1;
  if (y == 1)
    return x % mod;
  int tmp = Pow(x, y / 2);
  tmp = mul(tmp, tmp);
  return y & 1 ? mul(tmp, x) : tmp;
}

int path(int x, int y) {
  return mul(fact[x + y - 2], Pow(mul(fact[x - 1], fact[y - 1]), mod - 2));
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  fact[0] = 1;
  for (int i = 1; i < N; i++)
    fact[i] = mul(fact[i - 1], i);
  cin >> h >> w >> a >> b;
  int res = path(h, w);
  for (int i = h - a + 1; i <= h; i++)
    sub(res, mul(path(i, b), path(h - i + 1, w - b)));
  cout << res;
}
Problem p04046 (C++)

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

using namespace std;

#define mod 1000000007
#define ll long long

ll fact[200000 + 5];

long long power(long long a, long long b) {
  long long res = 1;
  a %= mod;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}

ll modInverse(ll n, ll p) { return power(n, p - 2); }

ll nCr(ll n, ll r) {
  if (r == 0 || n - r == 0)
    return 1;
  ll ret = fact[n] * modInverse(fact[n - r], mod);
  ret %= mod;
  ret *= modInverse(fact[r], mod);
  ret %= mod;
  return ret;
}

int main(void) {
  int H, W, a, b;
  cin >> H >> W >> a >> b;

  fact[0] = 1;
  for (int i = 1; i <= 200000; i++)
    fact[i] = (fact[i - 1] * i) % mod;

  int h1 = H - a, w1 = b + 1;
  int h2 = H - h1 + 1, w2 = W - w1 + 1;
  ll ans = 0;
  // cout << nCr(6, 3) << ".\n";
  while (w1 <= W) {
    // cout << h1 << " " << w1 << " " << h2 << " " << w2 << " \n";
    ans += (nCr(h1 + w1 - 2, h1 - 1) * nCr(h2 + w2 - 2, h2 - 1)) % mod;
    ans %= mod;
    w1++;
    w2--;
    h1--;
    h2++;
  }
  cout << ans;
}


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

using namespace std;

#define mod 1000000007
#define ll long long

ll fact[200000 + 5];

long long power(long long a, long long b) {
  long long res = 1;
  a %= mod;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}

ll modInverse(ll n, ll p) { return power(n, p - 2); }

ll nCr(ll n, ll r) {
  if (r == 0 || n - r == 0)
    return 1;
  ll ret = fact[n] * modInverse(fact[n - r], mod);
  ret %= mod;
  ret *= modInverse(fact[r], mod);
  ret %= mod;
  return ret;
}

int main(void) {
  int H, W, a, b;
  cin >> H >> W >> a >> b;

  fact[0] = 1;
  for (int i = 1; i <= 200000; i++)
    fact[i] = (fact[i - 1] * i) % mod;

  int h1 = H - a, w1 = b + 1;
  int h2 = H - h1 + 1, w2 = W - w1 + 1;
  ll ans = 0;
  // cout << nCr(6, 3) << ".\n";
  while (w1 <= W && h2 <= H) {
    // cout << h1 << " " << w1 << " " << h2 << " " << w2 << " \n";
    ans += (nCr(h1 + w1 - 2, h1 - 1) * nCr(h2 + w2 - 2, h2 - 1)) % mod;
    ans %= mod;
    w1++;
    w2--;
    h1--;
    h2++;
  }
  cout << ans;
}
Problem p04046 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
const int mod = 1e9 + 7;
using namespace std;

ll dp[200010];
ll gt[200010];
ll ksm(ll a, ll b) {
  ll ans = 1;
  for (; b; b >>= 1, a = a * a % mod)
    if (b & 1)
      ans = ans * a % mod;
  return ans;
}
ll C(ll m, ll n) {
  return (gt[m] * ksm(gt[n], mod - 2) % mod) * ksm(gt[m - n], mod - 2) % mod;
}
int main() {

  gt[0] = 1;
  for (int i = 1; i <= 200005; i++) {
    gt[i] = gt[i - 1] * i % mod;
  }
  freopen("input.txt", "r", stdin);
  ios::sync_with_stdio(false);
  int H, W, A, B;
  cin >> H >> W >> A >> B;
  ll sum = 0;
  for (int i = 1; i <= W - B; i++) {
    dp[i] = C(H - A - 1 + B + i - 1, B + i - 1);
    sum = (sum + 1ull * (dp[i] - dp[i - 1] + mod) * C(A + W - B - i, A) % mod) %
          mod;
  }
  cout << sum << endl;
}


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

ll dp[200010];
ll gt[200010];
ll ksm(ll a, ll b) {
  ll ans = 1;
  for (; b; b >>= 1, a = a * a % mod)
    if (b & 1)
      ans = ans * a % mod;
  return ans;
}
ll C(ll m, ll n) {
  return (gt[m] * ksm(gt[n], mod - 2) % mod) * ksm(gt[m - n], mod - 2) % mod;
}
int main() {

  gt[0] = 1;
  for (int i = 1; i <= 200005; i++) {
    gt[i] = gt[i - 1] * i % mod;
  }
  // freopen("input.txt","r",stdin);
  ios::sync_with_stdio(false);
  int H, W, A, B;
  cin >> H >> W >> A >> B;
  ll sum = 0;
  for (int i = 1; i <= W - B; i++) {
    dp[i] = C(H - A - 1 + B + i - 1, B + i - 1);
    sum = (sum + 1ull * (dp[i] - dp[i - 1] + mod) * C(A + W - B - i, A) % mod) %
          mod;
  }
  cout << sum << endl;
}
Problem p04046 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define N 100005
using namespace std;
typedef long long ll;
ll mod = 1000000007LL;
ll h, w, a, b;
ll comb1[N], comb2[N];
ll getpw(ll x) {
  ll res = 1LL;
  ll b = mod - 2;
  while (b) {
    if (b & 1)
      res = res * x % mod;
    x = x * x % mod;
    b /= 2;
  }
  return res;
}
void calc(ll *comb, int n) {
  comb[0] = 1LL;
  ll up = 1LL;
  ll down = 1LL;
  for (int i = 1; i <= n; ++i) {
    up = up * (ll)(n - (i - 1)) % mod;
    down = down * (ll)i % mod;
    comb[i] = up * getpw(down) % mod;
  }
}
int main() {
  cin >> h >> w >> a >> b;
  memset(comb1, 0, sizeof(comb1));
  memset(comb2, 0, sizeof(comb2));
  calc(comb1, h - a + b - 1);
  calc(comb2, a - b + w - 1);
  // cout<<comb1[0]<<" "<<comb2[1]<<endl;
  ll ans = 0LL;
  for (int i = 0; i < h - a; ++i) {
    ans = (ans + comb1[i] * comb2[h - 1 - i] % mod) % mod;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define N 300005
using namespace std;
typedef long long ll;
ll mod = 1000000007LL;
ll h, w, a, b;
ll comb1[N], comb2[N];
ll getpw(ll x) {
  ll res = 1LL;
  ll b = mod - 2;
  while (b) {
    if (b & 1)
      res = res * x % mod;
    x = x * x % mod;
    b /= 2;
  }
  return res;
}
void calc(ll *comb, int n) {
  comb[0] = 1LL;
  ll up = 1LL;
  ll down = 1LL;
  for (int i = 1; i <= n; ++i) {
    up = up * (ll)(n - (i - 1)) % mod;
    down = down * (ll)i % mod;
    comb[i] = up * getpw(down) % mod;
  }
}
int main() {
  cin >> h >> w >> a >> b;
  memset(comb1, 0, sizeof(comb1));
  memset(comb2, 0, sizeof(comb2));
  calc(comb1, h - a + b - 1);
  calc(comb2, a - b + w - 1);
  // cout<<comb1[0]<<" "<<comb2[1]<<endl;
  ll ans = 0LL;
  for (int i = 0; i < h - a; ++i) {
    ans = (ans + comb1[i] * comb2[h - 1 - i] % mod) % mod;
  }
  cout << ans << endl;
  return 0;
}
Problem p04046 (C++)

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

const int mod = 1e9 + 7;
const int MOD = 1e9 + 7;
const int MAXN = 1e5 + 10;
typedef long long ll;
using namespace std;

inline ll qpow(ll x, ll n) {
  ll res = 1;
  for (; n; n >>= 1) {
    if (n & 1)
      res = (res * x) % MOD;
    x = (x * x) % MOD;
  }
  return res;
}
ll fac[MAXN];
ll inv[MAXN];

ll Comb(int n, int m) // 需要预处理阶乘f,注f[0]=1;
{
  if (n < m)
    return 0;
  return fac[n] * inv[m] % mod * inv[n - m] % mod;
}

int main() {
  fac[0] = 1;
  inv[0] = qpow(fac[0], mod - 2);
  for (int i = 1; i < MAXN - 5; i++) {
    fac[i] = (i * fac[i - 1]) % mod;
    inv[i] = (qpow(fac[i], mod - 2));
  }
  ll h, w, a, b;

  scanf("%lld %lld %lld %lld", &h, &w, &a, &b);
  ll sum = Comb(h + w - 2, w - 1) % mod;

  for (int i = 1; i <= b; i++) {
    ll temp =
        (Comb(h - a + i - 2, i - 1) * Comb(h - (h - a + 1) + w - i, w - i)) %
        mod;
    while (sum < temp)
      sum = (sum + mod);
    sum = (sum - temp) % mod;
  }
  printf("%lld\n", sum % mod);
  return 0;
}


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

const int mod = 1e9 + 7;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 10;
typedef long long ll;
using namespace std;

inline ll qpow(ll x, ll n) {
  ll res = 1;
  for (; n; n >>= 1) {
    if (n & 1)
      res = (res * x) % MOD;
    x = (x * x) % MOD;
  }
  return res;
}
ll fac[MAXN];
ll inv[MAXN];

ll Comb(int n, int m) // 需要预处理阶乘f,注f[0]=1;
{
  if (n < m)
    return 0;
  return fac[n] * inv[m] % mod * inv[n - m] % mod;
}

int main() {
  fac[0] = 1;
  inv[0] = qpow(fac[0], mod - 2);
  for (int i = 1; i < MAXN - 5; i++) {
    fac[i] = (i * fac[i - 1]) % mod;
    inv[i] = (qpow(fac[i], mod - 2));
  }
  ll h, w, a, b;

  scanf("%lld %lld %lld %lld", &h, &w, &a, &b);
  ll sum = Comb(h + w - 2, w - 1) % mod;

  for (int i = 1; i <= b; i++) {
    ll temp =
        (Comb(h - a + i - 2, i - 1) * Comb(h - (h - a + 1) + w - i, w - i)) %
        mod;
    while (sum < temp)
      sum = (sum + mod);
    sum = (sum - temp) % mod;
  }
  printf("%lld\n", sum % mod);
  return 0;
}
Problem p04046 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
using ll = long long;
static const ll mod = 1e9 + 7;
static const ll INF = 1LL << 50;
using namespace std;

// 繰り返し二乗法
template <typename T> T doubling(T a, T p) {
  if (p == 0)
    return 1;       // 出口
  if (p % 2 == 0) { // pを半分にしていく
    T half = doubling<T>(a, p / 2);
    return (half % mod * half % mod) % mod;
  } else { // pが奇数ならpを偶数にする
    return (a * doubling<T>(a, p - 1)) % mod;
  }
}

int main() {
  ll h, w, a, b;
  cin >> h >> w >> a >> b;
  vector<ll> fav(h + w + 5, 1), inv(h + w + 5);
  rep(i, h + w + 5) fav[i + 1] = (fav[i] * (i + 1)) % mod;
  rep(i, h + w + 5) inv[i] = doubling<ll>(fav[i], mod - 2) % mod;
  ll ans = 0;
  for (int i = b; i < w; ++i) {
    ans += (((fav[h - a - 1 + i] % mod * inv[i] % mod) % mod) * inv[h - a - 1] %
            mod) %
           mod *
           (((fav[a + w - 2 - i] % mod * inv[a - 1] % mod) % mod) *
            inv[w - 1 - i] % mod) %
           mod;
    ans %= mod;
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
using ll = long long;
static const ll mod = 1e9 + 7;
static const ll INF = 1LL << 50;
using namespace std;

// 繰り返し二乗法
template <typename T> T doubling(T a, T p) {
  if (p == 0)
    return 1;       // 出口
  if (p % 2 == 0) { // pを半分にしていく
    T half = doubling<T>(a, p / 2);
    return (half % mod * half % mod) % mod;
  } else { // pが奇数ならpを偶数にする
    return (a * doubling<T>(a, p - 1)) % mod;
  }
}

int main() {
  ll h, w, a, b;
  cin >> h >> w >> a >> b;
  vector<ll> fav(h + w + 5, 1), inv(h + w + 5);
  rep(i, h + w) fav[i + 1] = (fav[i] * (i + 1)) % mod;
  rep(i, h + w) inv[i] = doubling<ll>(fav[i], mod - 2) % mod;
  ll ans = 0;
  for (int i = b; i < w; ++i) {
    ans += (((fav[h - a - 1 + i] % mod * inv[i] % mod) % mod) * inv[h - a - 1] %
            mod) %
           mod *
           (((fav[a + w - 2 - i] % mod * inv[a - 1] % mod) % mod) *
            inv[w - 1 - i] % mod) %
           mod;
    ans %= mod;
  }
  cout << ans << endl;
}
Problem p04046 (C++)

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

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

const int maxn = 100000;
int f[maxn + 1], fi[maxn + 1];
int ncr(int n, int r) { return mul(f[n], mul(fi[r], fi[n - r])); }

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

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

int g(int a, int b) { return ncr(a + b - 2, a - 1); }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  precompute();
  int h, w, a, b;
  cin >> h >> w >> a >> b;
  int sol = sub(g(w, h), mul(g(h - a + 1, b), g(a, w - b + 1)));
  for (int i = 0; i < b - 1; ++i)
    subs(sol, mul(g(h - a + 1, i + 1), g(a - 1, w - i)));
  cout << sol;
}


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

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

const int maxn = 200000;
int f[maxn + 1], fi[maxn + 1];
int ncr(int n, int r) { return mul(f[n], mul(fi[r], fi[n - r])); }

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

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

int g(int a, int b) { return ncr(a + b - 2, a - 1); }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  precompute();
  int h, w, a, b;
  cin >> h >> w >> a >> b;
  int sol = sub(g(w, h), mul(g(h - a + 1, b), g(a, w - b + 1)));
  for (int i = 0; i < b - 1; ++i)
    subs(sol, mul(g(h - a + 1, i + 1), g(a - 1, w - i)));
  cout << sol;
}
Problem p04046 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;

const ll MAX = 100001;
const ll MOD = 1000000007;

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

signed main() {
  // 前処理
  COMinit();
  ll h, w, a, b;
  cin >> h >> w >> a >> b;
  ll ans = 0; // cout << ans << endl;
  ll m = min(a, b);
  for (int i = 1; i <= w - b; i++) {
    // cout << 1 << endl;
    ll ans_sub =
        COM(h - a + b + i - 2, b + i - 1) * COM(w - b + a - i - 1, a - 1) % MOD;
    ans += ans_sub;
    ans %= MOD;
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;

const ll MAX = 200000;
const ll MOD = 1000000007;

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

signed main() {
  // 前処理
  COMinit();
  ll h, w, a, b;
  cin >> h >> w >> a >> b;
  ll ans = 0; // cout << ans << endl;
  ll m = min(a, b);
  for (int i = 1; i <= w - b; i++) {
    // cout << 1 << endl;
    ll ans_sub =
        COM(h - a + b + i - 2, b + i - 1) * COM(w - b + a - i - 1, a - 1) % MOD;
    ans += ans_sub;
    ans %= MOD;
  }
  cout << ans << endl;
}
Problem p04046 (C++)

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

#define LL long long
#define MAXN 100000
#define MOD 1000000007
LL fac[2 * MAXN + 5];
LL inv[2 * MAXN + 5];

LL Pow(LL x, LL p) {
  LL ret = 1;
  while (p) {
    if (p & 1)
      ret = ret * x % MOD;
    x = (x * x) % MOD;
    p >>= 1;
  }
  return ret;
}

void Prep(int N) {
  fac[0] = 1;
  for (int i = 1; i <= N; i++)
    fac[i] = fac[i - 1] * i % MOD;
  // inv[fac[i]]=Pow(fac[i],MOD-2)=Pow(1,MOD-2)...Pow(i,MOD-2)
  // inv[fac[i-1]]=Pow(fac[i-1],MOD-2)=Pow(1,MOD-2)...Pow(i-1,MOD-2)=Pow(fac[i],MOD-2)/Pow(i,MOD-2)=inv[i]*i
  inv[0] = 1;
  inv[N] = Pow(fac[N], MOD - 2);
  for (int i = N - 1; i >= 1; i--)
    inv[i] = inv[i + 1] * (i + 1) % MOD;
}

LL C(int m, int n) { return fac[m] * inv[n] % MOD * inv[m - n] % MOD; }

int main() {
  int H, W, A, B;
  scanf("%d%d%d%d", &H, &W, &A, &B);
  Prep(H + W);
  LL Ans = 0;
  for (int i = B + 1; i <= W; i++) {
    LL tmp1 = C(H - A + i - 2, i - 1);
    // C(m+n-x-y-2,m-x-1)
    //(H-A,i) -> (H,W)
    LL tmp2 = C(A + W - i - 1, W - 1);
    Ans = (Ans + (tmp1 * tmp2) % MOD) % MOD;
  }
  printf("%lld", Ans);
}

Fixed submission:
#include <cstdio>

#define LL long long
#define MAXN 100000
#define MOD 1000000007
LL fac[2 * MAXN + 5];
LL inv[2 * MAXN + 5];

LL Pow(LL x, LL p) {
  LL ret = 1;
  while (p) {
    if (p & 1)
      ret = ret * x % MOD;
    x = (x * x) % MOD;
    p >>= 1;
  }
  return ret;
}

void Prep(int N) {
  fac[0] = 1;
  for (int i = 1; i <= N; i++)
    fac[i] = fac[i - 1] * i % MOD;
  // inv[fac[i]]=Pow(fac[i],MOD-2)=Pow(1,MOD-2)...Pow(i,MOD-2)
  // inv[fac[i-1]]=Pow(fac[i-1],MOD-2)=Pow(1,MOD-2)...Pow(i-1,MOD-2)=Pow(fac[i],MOD-2)/Pow(i,MOD-2)=inv[i]*i
  inv[0] = 1;
  inv[N] = Pow(fac[N], MOD - 2);
  for (int i = N - 1; i >= 1; i--)
    inv[i] = inv[i + 1] * (i + 1) % MOD;
}

LL C(int m, int n) { return fac[m] * inv[n] % MOD * inv[m - n] % MOD; }

int main() {
  int H, W, A, B;
  scanf("%d%d%d%d", &H, &W, &A, &B);
  Prep(H + W);
  LL Ans = 0;
  for (int i = B + 1; i <= W; i++) {
    LL tmp1 = C(H - A + i - 2, i - 1);
    // C(m+n-x-y-2,m-x-1)
    //(H-A,i) -> (H,W)
    LL tmp2 = C(A + W - i - 1, W - i);
    Ans = (Ans + (tmp1 * tmp2) % MOD) % MOD;
  }
  printf("%lld", Ans);
}
Problem p04046 (C++)

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

using namespace std;

class Solution {

private:
  const long long MOD = 1e9 + 7;
  vector<long long> fac, inv_fac;

public:
  int ways(int H, int W, int A, int B) {

    init(max(H, W));

    long long res = 0ll;
    for (int j = B + 1; j <= W; j++) {
      long long a = C(H - A + j - 2, j - 1);
      long long b = C(W - j + 1 + A - 2, A - 1);
      res += a * b % MOD;
      res %= MOD;
    }
    return res;
  }

private:
  void init(int N) {

    fac = {1ll, 1ll};
    for (int i = 2; i <= N; i++)
      fac.push_back(fac.back() * i % MOD);

    inv_fac = {1ll, 1ll};
    for (int i = 2; i <= N; i++)
      inv_fac.push_back(mypow(fac[i], MOD - 2));
  }

  long long mypow(int x, int k) {

    if (k == 0)
      return 1ll;

    long long t = mypow(x, k / 2);
    long long res = t * t % MOD;
    if (k % 2)
      res = res * x % MOD;
    return res;
  }

  long long C(int n, int k) {
    return fac[n] * inv_fac[k] % MOD * inv_fac[n - k] % MOD;
  }
};

int main() {

  //    cout << Solution().ways(2, 3, 1, 1) << endl;
  //    // 2
  //
  //    cout << Solution().ways(10, 7, 3, 4) << endl;
  //    // 3570
  //
  //    cout << Solution().ways(100000, 100000, 99999, 99999) << endl;
  //    // 1

  int H, W, A, B;
  scanf("%d%d%d%d", &H, &W, &A, &B);
  printf("%d\n", Solution().ways(H, W, A, B));

  return 0;
}


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

using namespace std;

class Solution {

private:
  const long long MOD = 1e9 + 7;
  vector<long long> fac, inv_fac;

public:
  int ways(int H, int W, int A, int B) {

    init(2 * max(H, W));

    long long res = 0ll;
    for (int j = B + 1; j <= W; j++) {
      long long a = C(H - A + j - 2, j - 1);
      long long b = C(W - j + 1 + A - 2, A - 1);
      res += a * b % MOD;
      res %= MOD;
    }
    return res;
  }

private:
  void init(int N) {

    fac = {1ll, 1ll};
    for (int i = 2; i <= N; i++)
      fac.push_back(fac.back() * i % MOD);

    inv_fac = {1ll, 1ll};
    for (int i = 2; i <= N; i++)
      inv_fac.push_back(mypow(fac[i], MOD - 2));
  }

  long long mypow(int x, int k) {

    if (k == 0)
      return 1ll;

    long long t = mypow(x, k / 2);
    long long res = t * t % MOD;
    if (k % 2)
      res = res * x % MOD;
    return res;
  }

  long long C(int n, int k) {
    return fac[n] * inv_fac[k] % MOD * inv_fac[n - k] % MOD;
  }
};

int main() {

  //    cout << Solution().ways(2, 3, 1, 1) << endl;
  //    // 2
  //
  //    cout << Solution().ways(10, 7, 3, 4) << endl;
  //    // 3570
  //
  //    cout << Solution().ways(100000, 100000, 99999, 99999) << endl;
  //    // 1

  int H, W, A, B;
  scanf("%d%d%d%d", &H, &W, &A, &B);
  printf("%d\n", Solution().ways(H, W, A, B));

  return 0;
}
Problem p04046 (C++)

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

#define boost                                                                  \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);
#define int long long
const int N = 2e5 + 10;
const int MOD = 1e9 + 7;

int fact[N], inv[N];
int n, w, a, b;

int fastpow(int a, int b) {
  int ans = 1;
  a %= MOD;
  while (b) {
    if (b & 1)
      ans = (ans * a) % MOD;
    b >>= 1;
    a = (a * a) % MOD;
  }
  return ans;
}

void pre() {
  fact[0] = 1;
  for (int i = 1; i < N; ++i) {
    fact[i] = (fact[i - 1] * i) % MOD;
  }

  for (int i = 0; i < N; --i) {
    inv[i] = fastpow(fact[i], MOD - 2);
  }
}

int ways(int n, int r) {
  return (fact[n + r - 2] * (inv[r - 1] % MOD * inv[n - 1] % MOD) % MOD) % MOD;
}

int32_t main() {
  // boost;
  // cin >> n >> w >> a >> b;
  scanf("%lld%lld%lld%lld", &n, &w, &a, &b);
  pre();

  int out = 0;
  for (int i = b + 1; i <= w; ++i) {

    // Number of ways till (1,1) to (n-a , i) * Number of ways the remaining
    // till last ( (a,w-i+1 ))
    out += ((ways(n - a, i) % MOD) * ways(a, w - i + 1) % MOD) % MOD;
    out %= MOD;
  }

  // cout << out;
  printf("%lld\n", out);
  return 0;
} // Read my code it is awesome.


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

#define boost                                                                  \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);
#define int long long
const int N = 2e5 + 10;
const int MOD = 1e9 + 7;

int fact[N], inv[N];
int n, w, a, b;

int fastpow(int a, int b) {
  int ans = 1;
  a %= MOD;
  while (b) {
    if (b & 1)
      ans = (ans * a) % MOD;
    b >>= 1;
    a = (a * a) % MOD;
  }
  return ans;
}

void pre() {
  fact[0] = 1;
  for (int i = 1; i < N; ++i) {
    fact[i] = (fact[i - 1] * i) % MOD;
  }

  for (int i = 0; i < N; ++i) {
    inv[i] = fastpow(fact[i], MOD - 2);
  }
}

int ways(int n, int r) {
  return (fact[n + r - 2] * (inv[r - 1] % MOD * inv[n - 1] % MOD) % MOD) % MOD;
}

int32_t main() {
  // boost;
  // cin >> n >> w >> a >> b;
  scanf("%lld%lld%lld%lld", &n, &w, &a, &b);
  pre();

  int out = 0;
  for (int i = b + 1; i <= w; ++i) {

    // Number of ways till (1,1) to (n-a , i) * Number of ways the remaining
    // till last ( (a,w-i+1 ))
    out += ((ways(n - a, i) % MOD) * ways(a, w - i + 1) % MOD) % MOD;
    out %= MOD;
  }

  // cout << out;
  printf("%lld\n", out);
  return 0;
} // Read my code it is awesome.
Problem p04047 (C++)

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

#define int long long
#define PB push_back
#define PPB pop_back
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<string, int> psi;

static const int INF = 1LL << 60;
// static const int MOD = 1000000007;

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

  int n, l[105], ans = 0;
  cin >> n;
  for (int i = 0; i < 2 * n; ++i)
    cin >> l[i];
  sort(l, l + 2 * n);
  for (int i = 2 * n - 2; 0 <= i; i -= 2)
    ans += l[i];
  cout << ans << endl;
}

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

#define int long long
#define PB push_back
#define PPB pop_back
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<string, int> psi;

static const int INF = 1LL << 60;
// static const int MOD = 1000000007;

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

  int n, l[205], ans = 0;
  cin >> n;
  for (int i = 0; i < 2 * n; ++i)
    cin >> l[i];
  sort(l, l + 2 * n);
  for (int i = 2 * n - 2; 0 <= i; i -= 2)
    ans += l[i];
  cout << ans << endl;
}
Problem p04047 (C++)

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

int main() {
  int N, L[110], sum = 0;
  cin >> N;
  for (int i = 0; i < 2 * N; i++) {
    cin >> L[i];
  }
  sort(L, L + 2 * N);
  for (int i = 0; i < 2 * N; i++) {
    if (i % 2 == 0) {
      sum = sum + L[i];
    }
  }
  cout << sum << endl;
  return 0;
}

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

int main() {
  int N, L[220], sum = 0;
  cin >> N;
  for (int i = 0; i < 2 * N; i++) {
    cin >> L[i];
  }
  sort(L, L + 2 * N);
  for (int i = 0; i < 2 * N; i++) {
    if (i % 2 == 0) {
      sum = sum + L[i];
    }
  }
  cout << sum << endl;
  return 0;
}