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

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

using namespace std;

int main(void) {

  int n, l;
  int k[100002];
  int i;
  cin >> n >> l;
  int p, c;
  int max = 0, imax = 0;
  cin >> c;
  for (i = 1; i <= n - 1; i++) {
    p = c;
    cin >> c;
    k[i] = p + c;
    if (max < p + c) {
      max = p + c;
      imax = i;
    }
  }

  if (max < l)
    cout << "Impossible";
  else {
    cout << "Possible\n";
    for (i = 1; i < imax; i++)
      cout << i << "\n";
    for (i = n - 1; i > imax; i++)
      cout << i << "\n";
    cout << imax << "\n";
  }

  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

int main(void) {

  int n, l;
  int k[100002];
  int i;
  cin >> n >> l;
  int p, c;
  int max = 0, imax = 0;
  cin >> c;
  for (i = 1; i <= n - 1; i++) {
    p = c;
    cin >> c;
    k[i] = p + c;
    if (max < p + c) {
      max = p + c;
      imax = i;
    }
  }

  if (max < l)
    cout << "Impossible";
  else {
    cout << "Possible\n";
    for (i = 1; i < imax; i++)
      cout << i << "\n";
    for (i = n - 1; i > imax; i--)
      cout << i << "\n";
    cout << imax << "\n";
  }

  return 0;
}
Problem p04035 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include "bits/stdc++.h"
#define REP(i, a, b) for (i = a; i < b; ++i)
#define rep(i, n) REP(i, 0, n)
#define ll long long
#define ull unsigned ll
typedef long double ld;
#define ALL(a) (a).begin(), (a).end()
#define ifnot(a) if (not a)
#define dump(x) cerr << #x << " = " << (x) << endl
using namespace std;

// #define int ll
bool test = 0;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
#define INF (1 << 28)
ull mod = (int)1e9 + 7;
//.....................
#define MAX (int)0.3e5 + 5

int a[MAX];

int main() {
  ll i, j, k, l;
  int N, L;
  cin >> N >> L;
  rep(i, N) cin >> a[i];
  bool possible = false;
  int first;
  rep(i, N - 1) {
    if (a[i] + a[i + 1] >= L) {
      possible = true;
      first = i;
      break;
    }
  }
  if (!possible) {
    puts("Impossible");
    return 0;
  }
  puts("Possible");
  vector<int> v;
  for (i = first; i < N - 1; ++i) {
    v.push_back(i);
  }
  for (i = first - 1; i >= 0; --i) {
    v.push_back(i);
  }
  reverse(ALL(v));
  auto itr = v.begin();
  for (; itr != v.end(); itr++) {
    cout << *itr + 1 << endl;
  }
  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include "bits/stdc++.h"
#define REP(i, a, b) for (i = a; i < b; ++i)
#define rep(i, n) REP(i, 0, n)
#define ll long long
#define ull unsigned ll
typedef long double ld;
#define ALL(a) (a).begin(), (a).end()
#define ifnot(a) if (not a)
#define dump(x) cerr << #x << " = " << (x) << endl
using namespace std;

// #define int ll
bool test = 0;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
#define INF (1 << 28)
ull mod = (int)1e9 + 7;
//.....................
#define MAX (int)3e5 + 5

int a[MAX];

int main() {
  ll i, j, k, l;
  int N, L;
  cin >> N >> L;
  rep(i, N) cin >> a[i];
  bool possible = false;
  int first;
  rep(i, N - 1) {
    if (a[i] + a[i + 1] >= L) {
      possible = true;
      first = i;
      break;
    }
  }
  if (!possible) {
    puts("Impossible");
    return 0;
  }
  puts("Possible");
  vector<int> v;
  for (i = first; i < N - 1; ++i) {
    v.push_back(i);
  }
  for (i = first - 1; i >= 0; --i) {
    v.push_back(i);
  }
  reverse(ALL(v));
  auto itr = v.begin();
  for (; itr != v.end(); itr++) {
    cout << *itr + 1 << endl;
  }
  return 0;
}
Problem p04037 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define N 100001
int t, a[N], n;
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i)
      scanf("%d", &a[i]);
    sort(a + 1, a + n + 1, greater<int>());
    int add = 1, adds = 1;
    while (add <= n && a[add] >= add)
      add++;
    while (adds <= n && a[adds] > add)
      adds++;
    int ans = ((a[add] - add) & 1) || ((adds - add) & 1);
    puts(ans ? "First" : "Second");
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define N 100001
int t, a[N], n;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  sort(a + 1, a + n + 1, greater<int>());
  int add = 1, adds = 1;
  while (add < n && a[add + 1] >= add + 1)
    add++;
  while (adds < n && a[adds + 1] >= add)
    adds++;
  int ans = ((a[add] - add) & 1) || ((adds - add) & 1);
  puts(ans ? "First" : "Second");

  return 0;
}
Problem p04037 (C++)

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

const int N = 1e3 + 3;
int n, a[N]; //, sg[N][N];

// int mex(set<int> st){
//     for(int i = 0; ; i++) if(st.find(i) == st.end()) return i;
// }
// int SG(int x, int y){
//     return mex(set<int>{sg[x][y + 1], sg[x + 1][y]});
// }

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  sort(a + 1, a + 1 + n);
  reverse(a + 1, a + 1 + n);
  for (int i = 0; i <= n; i++)
    if (i + 1 > a[i + 1]) {
      int r = i;
      while (a[r + 1] == i)
        r++;
      puts((((a[i] - i) & 1) || ((r - i) & 1)) ? "First" : "Second");
      break;
    }
  return 0;
}

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

const int N = 1e5 + 3;
int n, a[N]; //, sg[N][N];

// int mex(set<int> st){
//     for(int i = 0; ; i++) if(st.find(i) == st.end()) return i;
// }
// int SG(int x, int y){
//     return mex(set<int>{sg[x][y + 1], sg[x + 1][y]});
// }

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  sort(a + 1, a + 1 + n);
  reverse(a + 1, a + 1 + n);
  for (int i = 0; i <= n; i++)
    if (i + 1 > a[i + 1]) {
      int r = i;
      while (a[r + 1] == i)
        r++;
      puts((((a[i] - i) & 1) || ((r - i) & 1)) ? "First" : "Second");
      break;
    }
  return 0;
}
Problem p04037 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define F(i, l, r) for (int i = (l), _end_ = (int)(r); i <= _end_; ++i)
#define f(i, r, l) for (int i = (r), _end_ = (int)(l); i >= _end_; --i)
#define Set(a, v) memset(a, v, sizeof(a))
#define file(a) freopen(a ".in", "r", stdin), freopen(a ".out", "w", stdout)
using namespace std;

bool chkmin(int &a, int b) { return b < a ? a = b, 1 : 0; }
bool chkmax(int &a, int b) { return b > a ? a = b, 1 : 0; }

inline int read() {
  int x = 0, fh = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fh = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x << 1) + (x << 3) + (ch ^ '0');
  return x * fh;
}
int n;
int a[100005];
bool cmp(int x, int y) { return x > y; }
int main() {
#ifndef ONLINE_JUDGE
  file("agc002e");
#endif
  n = read();
  F(i, 1, n) a[i] = read();
  sort(a + 1, a + n + 1, cmp);
  bool ans = 0;
  F(i, 1, n) {
    if (i + 1 > a[i + 1]) {
      int j = i + 1;
      while (j <= n && a[j] == i) {
        ans ^= 1;
        ++j;
      }
      ans |= (a[i] - i) & 1;
      break;
    }
  }
  if (ans)
    puts("First");
  else
    puts("Second");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define F(i, l, r) for (int i = (l), _end_ = (int)(r); i <= _end_; ++i)
#define f(i, r, l) for (int i = (r), _end_ = (int)(l); i >= _end_; --i)
#define Set(a, v) memset(a, v, sizeof(a))
#define file(a) freopen(a ".in", "r", stdin), freopen(a ".out", "w", stdout)
using namespace std;

bool chkmin(int &a, int b) { return b < a ? a = b, 1 : 0; }
bool chkmax(int &a, int b) { return b > a ? a = b, 1 : 0; }

inline int read() {
  int x = 0, fh = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fh = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x << 1) + (x << 3) + (ch ^ '0');
  return x * fh;
}
int n;
int a[100005];
bool cmp(int x, int y) { return x > y; }
int main() {
  n = read();
  F(i, 1, n) a[i] = read();
  sort(a + 1, a + n + 1, cmp);
  bool ans = 0;
  F(i, 1, n) {
    if (i + 1 > a[i + 1]) {
      int j = i + 1;
      while (j <= n && a[j] == i) {
        ans ^= 1;
        ++j;
      }
      ans |= (a[i] - i) & 1;
      break;
    }
  }
  if (ans)
    puts("First");
  else
    puts("Second");
  return 0;
}
Problem p04037 (C++)

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

using namespace std;

const int Nmax = 1e6 + 5;

int n;
int a[Nmax];

bool bad(int l, int c) { return a[l] <= c; }

int main() {
  freopen("input", "r", stdin);
  cin.sync_with_stdio(false);
  cin.tie(0);

  int i, j;

  cin >> n;
  for (i = 1; i <= n; ++i)
    cin >> a[i];
  sort(a + 1, a + n + 1);

  for (i = n; i; --i)
    if (bad(i, n - i))
      break;
  ++i;

  /// vreau sa aflu (i, n-i)

  for (j = i - 1; j; --j)
    if (bad(j, n - i))
      break;

  int ans1 = (j - i) % 2;
  int ans2 = (a[i] - (n - i)) % 2;

  if (ans1 && ans2)
    cout << "Second\n";
  else
    cout << "First\n";

  return 0;
}


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

using namespace std;

const int Nmax = 1e6 + 5;

int n;
int a[Nmax];

bool bad(int l, int c) { return a[l] <= c; }

int main() {
  //   freopen("input", "r", stdin);
  cin.sync_with_stdio(false);
  cin.tie(0);

  int i, j;

  cin >> n;
  for (i = 1; i <= n; ++i)
    cin >> a[i];
  sort(a + 1, a + n + 1);

  for (i = n; i; --i)
    if (bad(i, n - i))
      break;
  ++i;

  /// vreau sa aflu (i, n-i)

  for (j = i - 1; j; --j)
    if (bad(j, n - i))
      break;

  int ans1 = (j - i) % 2;
  int ans2 = (a[i] - (n - i)) % 2;

  if (ans1 && ans2)
    cout << "Second\n";
  else
    cout << "First\n";

  return 0;
}
Problem p04037 (C++)

Buggy submission (status: Runtime Error):
// atcoder 002 E Candy Piles 博弈论
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int a[10010];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  sort(a + 1, a + n + 1);
  reverse(a + 1, a + n + 1); // 逆转元素，从大到小
  bool flag = 0;
  for (int i = 1; i <= n; i++)
    if (i + 1 > a[i + 1]) {
      for (int j = i + 1; a[j] == i; j++)
        flag ^= 1;
      flag |= (a[i] - i) & 1;
      if (flag)
        printf("First\n");
      else
        printf("Second\n");
      return 0;
    }
}

Fixed submission:
// atcoder 002 E Candy Piles 博弈论
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int a[100010];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  sort(a + 1, a + n + 1);
  reverse(a + 1, a + n + 1); // 逆转元素，从大到小
  bool flag = 0;
  for (int i = 1; i <= n; i++)
    if (i + 1 > a[i + 1]) {
      for (int j = i + 1; a[j] == i; j++)
        flag ^= 1;
      flag |= (a[i] - i) & 1;
      if (flag)
        printf("First\n");
      else
        printf("Second\n");
      return 0;
    }
}
Problem p04037 (C++)

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

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

  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.end());

  deque<bool> d;
  for (int x : a) {
    int sz = d.size();
    bool up = (x - sz + 1) % 2;
    bool right = d.front();
    d.push_front((!up) | (!right));
    for (int i = 1; i <= x - sz; i++) {
      d.push_front((x - sz - i) % 2);
    }
    d.pop_back();
  }

  bool res = d.back();
  if (res) {
    cout << "First\n";
  } else {
    cout << "Second\n";
  }

  return 0;
}


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

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

  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.end());

  deque<bool> d;
  for (int x : a) {
    int sz = d.size();
    bool up = (x - sz + 1) % 2;
    bool right = d.front();
    d.push_front((!up) | (!right));
    int ub = min(x, n) - sz;
    for (int i = 1; i <= ub; i++) {
      d.push_front((x - sz - i) % 2);
    }
    d.pop_back();
  }

  bool res = d.back();
  if (res) {
    cout << "First\n";
  } else {
    cout << "Second\n";
  }

  return 0;
}
Problem p04037 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int MAXN = 100005;
int a[MAXN], n;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d", a + i);
  sort(a + 1, a + n + 1, greater<int>());
  int p = 0;
  for (int i = 1; i <= n; ++i) {
    if (a[i + 1] < i + 1) {
      p = i;
      break;
    }
  }
  int rr = p;
  while (a[rr + 1] <= p)
    ++rr;
  int k1 = rr - p, k2 = a[p] - p;
  if ((k1 & 1) + (k2 & 1))
    puts("First");
  else
    puts("Second");
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int MAXN = 100005;
int a[MAXN], n;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d", a + i);
  sort(a + 1, a + n + 1, greater<int>());
  int p = 0;
  for (int i = 1; i <= n; ++i) {
    if (a[i + 1] < i + 1) {
      p = i;
      break;
    }
  }
  int rr = p;
  while (a[rr + 1] >= p)
    ++rr;
  int k1 = rr - p, k2 = a[p] - p;
  if ((k1 & 1) + (k2 & 1))
    puts("First");
  else
    puts("Second");
  return 0;
}
Problem p04037 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pp;
typedef pair<ll, ll> pll;
void read(int &x) { scanf("%d", &x); }
void read(ll &x) { scanf("%lld", &x); }
template <typename T, typename... Args> void read(T &a, Args &...b) {
  read(a);
  read(b...);
}
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define x first
#define y second

int n;
int a[100010];

int main() {
  read(n);
  for (int i = 0; i < n; ++i)
    read(a[i]), --a[i];
  sort(a, a + n);
  reverse(a, a + n);
  int p = 0;
  while (p + 1 < n && p + 1 <= a[p + 1])
    ++p;
  const char *win = "First";
  const char *lose = "Second";
  if (a[p] == p && a[p + 1] < p) {
    puts(lose);
  } else if (a[p] > p && a[p] % 2 != p % 2) {
    puts(win);
  } else {
    int q = p;
    while (a[q + 1] >= p)
      ++q;
    if (p % 2 != q % 2)
      puts(win);
    else
      puts(lose);
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pp;
typedef pair<ll, ll> pll;
void read(int &x) { scanf("%d", &x); }
void read(ll &x) { scanf("%lld", &x); }
template <typename T, typename... Args> void read(T &a, Args &...b) {
  read(a);
  read(b...);
}
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define x first
#define y second

int n;
int a[100010];

int main() {
  read(n);
  for (int i = 0; i < n; ++i)
    read(a[i]), --a[i];
  sort(a, a + n);
  reverse(a, a + n);
  int p = 0;
  while (p + 1 < n && p + 1 <= a[p + 1])
    ++p;
  const char *win = "First";
  const char *lose = "Second";
  if (a[p] == p && a[p + 1] < p) {
    puts(lose);
  } else if (a[p] > p && a[p] % 2 != p % 2) {
    puts(win);
  } else {
    int q = p;
    while (a[q + 1] >= p && q + 1 < n)
      ++q;
    if (p % 2 != q % 2)
      puts(win);
    else
      puts(lose);
  }
  return 0;
}
Problem p04037 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rint register int
inline int rf() {
  int r;
  int s = 0, c;
  for (; !isdigit(c = getchar()); s = c)
    ;
  for (r = c ^ 48; isdigit(c = getchar()); (r *= 10) += c ^ 48)
    ;
  return s ^ 45 ? r : -r;
}
int a[100005], n;
bool W(int i) {
  bool U = (a[i + 1] - i) & 1, R = 0;
  for (rint j = i + 1; j <= n && a[j] > i; R ^= 1, j++)
    ;
  return U & R;
}
int main() {
  n = rf();
  generate(a + 1, a + n + 1, rf);
  sort(a + 1, a + n + 1, greater<int>());
  for (rint i = 2; i <= n + 1; i++)
    if (a[i] < i)
      return puts(W(i - 2) ? "Second" : "First");
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rint register int
inline int rf() {
  int r;
  int s = 0, c;
  for (; !isdigit(c = getchar()); s = c)
    ;
  for (r = c ^ 48; isdigit(c = getchar()); (r *= 10) += c ^ 48)
    ;
  return s ^ 45 ? r : -r;
}
int a[100005], n;
bool W(int i) {
  bool U = (a[i + 1] - i) & 1, R = 0;
  for (rint j = i + 1; j <= n && a[j] > i; R ^= 1, j++)
    ;
  return U & R;
}
int main() {
  n = rf();
  generate(a + 1, a + n + 1, rf);
  sort(a + 1, a + n + 1, greater<int>());
  for (rint i = 2; i <= n + 1; i++)
    if (a[i] < i)
      return puts(W(i - 2) ? "Second" : "First"), 0;
}
Problem p04037 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
const int N = 100007;
int n, a[N];
bool d[N];
#define ret(c) return puts((c) ? "First" : "Second")
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d", a + i);
  std::sort(a + 1, a + n + 1);
  for (int i = 1, z = n + 1; i <= n; ++i) {
    if (a[i] == a[i - 1])
      d[i] = !d[i - 1];
    else {
      int l = i + a[i - 1], r = i + a[i];
      if (l < z && z <= r)
        ret(r - z & 1);
      if (l == z)
        ret(!(r - z - 1 & d[i - 1]));
    }
    if (i + a[i] == z)
      ret(d[i]);
  }
}

Fixed submission:
#include <bits/stdc++.h>
const int N = 100007;
int n, a[N];
bool d[N];
#define ret(c) return puts((c) ? "First" : "Second"), 0
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d", a + i);
  std::sort(a + 1, a + n + 1);
  for (int i = 1, z = n + 1; i <= n; ++i) {
    if (a[i] == a[i - 1])
      d[i] = !d[i - 1];
    else {
      int l = i + a[i - 1], r = i + a[i];
      if (l < z && z <= r)
        ret(r - z & 1);
      if (l == z)
        ret(!(r - z - 1 & d[i - 1]));
    }
    if (i + a[i] == z)
      ret(d[i]);
  }
}
Problem p04037 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
const int maxn = 1e5 + 5, inf = 1e9 + 10;
int a[maxn];
int main() {
  int i, n, p, q;
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", &a[i]);
  sort(a, a + n);
  q = a[n - 1];
  for (i = n - 1; i; i--)
    a[i] = a[i] - a[i - 1] + 1;
  a[0] = inf;
  for (i = n - 2; i; i--)
    if (a[i] == 1 && a[i + 1] == 1) {
      a[i]--;
      a[i + 1]++;
    }
  p = n - 1;
  while (q > a[p])
    q -= a[p--];
  if ((a[p] & 1) && q == a[p])
    puts("First");
  else
    puts((q & 1) ? "Second" : "First");
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
const int maxn = 1e5 + 5, inf = 1e9 + 10;
int a[maxn];
int main() {
  int i, n, p, q;
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", &a[i]);
  sort(a, a + n);
  q = a[n - 1];
  for (i = n - 1; i; i--)
    a[i] = a[i] - a[i - 1] + 1;
  a[0] = inf;
  for (i = 1; i < n - 1; i++)
    if (a[i] == 1 && (a[i + 1] & 1)) {
      a[i]++;
      a[i + 1]--;
    }
  p = n - 1;
  while (q > a[p])
    q -= a[p--];
  if ((a[p] & 1) && q == a[p])
    puts("First");
  else
    puts((q & 1) ? "Second" : "First");
  return 0;
}
Problem p04037 (C++)

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

const int N = 1e5 + 10;

int a[N];

int main() {
  int n, x, y, p, len1, len2;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  sort(a + 1, a + n + 1, [&](int x, int y) { return x > y; });
  x = y = 0;
  while (a[x + 2] <= y + 1)
    x++, y++;
  len1 = 0;
  p = x;
  while (y < a[p + 2])
    p++;
  len1 = p - x;
  len2 = a[x + 1] - y - 1;
  if (len1 % 2 == 0 && len2 % 2 == 0)
    printf("Second\n");
  else
    printf("First\n");
  return 0;
}

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

const int N = 1e5 + 10;

int a[N];

int main() {
  int n, x, y, p, len1, len2;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  sort(a + 1, a + n + 1, [&](int x, int y) { return x > y; });
  x = y = 0;
  while (a[x + 2] > y + 1)
    x++, y++;
  len1 = 0;
  p = x;
  while (y < a[p + 2])
    p++;
  len1 = p - x;
  len2 = a[x + 1] - y - 1;
  if (len1 % 2 == 0 && len2 % 2 == 0)
    printf("Second\n");
  else
    printf("First\n");
  return 0;
}
Problem p04037 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define N 100100
using namespace std;
template <typename T> inline void read(T &x) {
  x = 0;
  static int p;
  p = 1;
  static char c;
  c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      p = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 1) + (x << 3) + (c - 48);
    c = getchar();
  }
  x *= p;
}
int n, m;
int a[N];
bool cmp(int x, int y) { return x > y; }
int main() {
  //	freopen("mnth.in","r",stdin);
  //	freopen("mnth.out","w",stdout);
  read(n);
  read(m);
  for (int i = 1; i <= n; i++)
    read(a[i]);
  sort(a + 1, a + n + 1, cmp);
  int stx = 1, sty = 1;
  while (sty + 1 <= a[stx + 1])
    sty++, stx++;
  int now = stx + 1;
  while (a[now] == sty)
    now++;
  if ((now - stx) % 2 == 1 && (a[stx] + 1 - sty) % 2 == 1)
    puts("Second");
  else
    puts("First");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define N 100100
using namespace std;
template <typename T> inline void read(T &x) {
  x = 0;
  static int p;
  p = 1;
  static char c;
  c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      p = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 1) + (x << 3) + (c - 48);
    c = getchar();
  }
  x *= p;
}
int n, m;
int a[N];
bool cmp(int x, int y) { return x > y; }
int main() {
  //	freopen("mnth.in","r",stdin);
  //	freopen("mnth.out","w",stdout);
  read(n);
  for (int i = 1; i <= n; i++)
    read(a[i]);
  sort(a + 1, a + n + 1, cmp);
  int stx = 1, sty = 1;
  while (sty + 1 <= a[stx + 1])
    sty++, stx++;
  int now = stx + 1;
  while (a[now] == sty)
    now++;
  if ((now - stx) % 2 == 1 && (a[stx] + 1 - sty) % 2 == 1)
    puts("Second");
  else
    puts("First");
  return 0;
}
Problem p04037 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ts cout << "ok" << endl
#define ll long long
#define hh puts("")
#define pc putchar
// #define getchar()
// (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++) char
// buf[1<<21],*p1=buf,*p2=buf;
using namespace std;
const int N = 10005;
int n, a[N];
inline int read() {
  int ret = 0, ff = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      ff = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    ret = ret * 10 + (ch ^ 48);
    ch = getchar();
  }
  return ret * ff;
}
void write(int x) {
  if (x < 0) {
    x = -x, pc('-');
  }
  if (x > 9)
    write(x / 10);
  pc(x % 10 + 48);
}
void writeln(int x) { write(x), hh; }
void writesp(int x) { write(x), pc(' '); }
signed main() {
  n = read();
  for (int i = 1; i <= n; i++)
    a[i] = read();
  sort(a + 1, a + n + 1, greater<int>());
  for (int i = 1; i <= n; i++) {
    if (i + 1 > a[i + 1]) {
      int now = i + 1;
      while (a[now] == i)
        now++;
      if ((a[i] - i) % 2 || (now - i - 1) % 2)
        puts("First");
      else
        puts("Second");
      break;
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ts cout << "ok" << endl
#define ll long long
#define hh puts("")
#define pc putchar
// #define getchar()
// (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++) char
// buf[1<<21],*p1=buf,*p2=buf;
using namespace std;
const int N = 100005;
int n, a[N];
inline int read() {
  int ret = 0, ff = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      ff = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    ret = ret * 10 + (ch ^ 48);
    ch = getchar();
  }
  return ret * ff;
}
void write(int x) {
  if (x < 0) {
    x = -x, pc('-');
  }
  if (x > 9)
    write(x / 10);
  pc(x % 10 + 48);
}
void writeln(int x) { write(x), hh; }
void writesp(int x) { write(x), pc(' '); }
signed main() {
  n = read();
  for (int i = 1; i <= n; i++)
    a[i] = read();
  sort(a + 1, a + n + 1, greater<int>());
  for (int i = 1; i <= n; i++) {
    if (i + 1 > a[i + 1]) {
      int now = i + 1;
      while (a[now] == i)
        now++;
      if ((a[i] - i) % 2 || (now - i - 1) % 2)
        puts("First");
      else
        puts("Second");
      break;
    }
  }
  return 0;
}
Problem p04038 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll n, m, f[2010][2010], jc[4000010], ny[4000010], p = 1e9 + 7;
ll C(ll x, ll y) { return jc[x] * ny[y] % p * ny[x - y] % p; }
ll ksm(ll x, ll y) {
  ll xlh = 1;
  while (y) {
    if (y & 1)
      xlh = xlh * x % p;
    x = x * x % p;
    y /= 2;
  }
  return xlh;
}
int main() {
  ll i, j;
  scanf("%lld%lld", &n, &m);
  if (m == 1)
    return puts("1"), 0;
  jc[0] = 1;
  ny[0] = ny[1] = 1;
  for (i = 1; i <= n * m; i++)
    jc[i] = jc[i - 1] * i % p;
  for (i = 2; i <= n * m; i++) {
    ny[i] = ksm(jc[i], p - 2) % p;
    // printf("%lld %lld %lld\n",i,jc[i],ny[i]);
  }
  for (i = 0; i <= n; i++)
    f[i][0] = 1;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= i; j++) {
      f[i][j] = f[i - 1][j];
      f[i][j] = (f[i][j] + f[i][j - 1] * (n - j + 1) % p *
                               C(n * m - i - (j - 1) * (m - 1) - 1, m - 2)) %
                p;
      // printf("%lld %lld %lld %lld %lld
      // %lld\n",i,j,f[i][j],f[i-1][j],n*m-i-(j-1)*(m-1)-1,C(n*m-i-(j-1)*(m-1)-1,m-2));
    }
  }
  printf("%lld", f[n][n]);
}
/*
没有想到把当前放的固定在原序列上
*/

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll n, m, f[2010][2010], jc[4000010], ny[4000010], p = 1e9 + 7;
ll C(ll x, ll y) { return jc[x] * ny[y] % p * ny[x - y] % p; }
ll ksm(ll x, ll y) {
  ll xlh = 1;
  while (y) {
    if (y & 1)
      xlh = xlh * x % p;
    x = x * x % p;
    y /= 2;
  }
  return xlh;
}
int main() {
  ll i, j;
  scanf("%lld%lld", &n, &m);
  if (m == 1)
    return puts("1"), 0;
  jc[0] = 1;
  ny[0] = ny[1] = 1;
  for (i = 1; i <= n * m; i++)
    jc[i] = jc[i - 1] * i % p;
  ny[n * m] = ksm(jc[n * m], p - 2) % p;
  for (i = n * m - 1; i >= 2; i--) {
    ny[i] = ny[i + 1] * (i + 1) % p;
    // printf("%lld %lld %lld\n",i,jc[i],ny[i]);
  }
  for (i = 0; i <= n; i++)
    f[i][0] = 1;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= i; j++) {
      f[i][j] = f[i - 1][j];
      f[i][j] = (f[i][j] + f[i][j - 1] * (n - j + 1) % p *
                               C(n * m - i - (j - 1) * (m - 1) - 1, m - 2)) %
                p;
      // printf("%lld %lld %lld %lld %lld
      // %lld\n",i,j,f[i][j],f[i-1][j],n*m-i-(j-1)*(m-1)-1,C(n*m-i-(j-1)*(m-1)-1,m-2));
    }
  }
  printf("%lld", f[n][n]);
}
/*
没有想到把当前放的固定在原序列上
*/
Problem p04038 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#include <stdio.h>
#define mod 1000000007
#define maxn 40000095
using namespace std;
int f[2005][2005], fac[maxn], inv[maxn];
int add(int a, int b) {
  a += b;
  return a >= mod ? a - mod : a;
}
int ksm(int a, int b) {
  int s = 1;
  for (; b; b >>= 1, a = 1ll * a * a % mod)
    if (b & 1)
      s = 1ll * s * a % mod;
  return s;
}
int n, k;
void Pre() {
  fac[0] = 1;
  for (int i = 1; i < maxn; i++)
    fac[i] = 1ll * fac[i - 1] * i % mod;
  inv[maxn - 1] = ksm(fac[maxn - 1], mod - 2);
  for (int i = maxn - 2; i >= 0; i--)
    inv[i] = 1ll * inv[i + 1] * (i + 1) % mod;
}
int C(int n, int m) { return 1ll * fac[n] * inv[m] % mod * inv[n - m] % mod; }
int main() {
  Pre();
  scanf("%d%d", &n, &k);
  if (k == 1)
    return puts("1"), 0;
  f[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= i; j++) {
      f[i][j] = f[i - 1][j];
      if (j)
        f[i][j] =
            add(f[i][j], 1ll * C(n * k - i - (j - 1) * (k - 1) - 1, k - 2) *
                             (n - j + 1) % mod * f[i][j - 1] % mod);
    }
  }
  printf("%d\n", f[n][n]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <stdio.h>
#define mod 1000000007
#define maxn 4000095
using namespace std;
int f[2005][2005], fac[maxn], inv[maxn];
int add(int a, int b) {
  a += b;
  return a >= mod ? a - mod : a;
}
int ksm(int a, int b) {
  int s = 1;
  for (; b; b >>= 1, a = 1ll * a * a % mod)
    if (b & 1)
      s = 1ll * s * a % mod;
  return s;
}
int n, k;
void Pre() {
  fac[0] = 1;
  for (int i = 1; i < maxn; i++)
    fac[i] = 1ll * fac[i - 1] * i % mod;
  inv[maxn - 1] = ksm(fac[maxn - 1], mod - 2);
  for (int i = maxn - 2; i >= 0; i--)
    inv[i] = 1ll * inv[i + 1] * (i + 1) % mod;
}
int C(int n, int m) { return 1ll * fac[n] * inv[m] % mod * inv[n - m] % mod; }
int main() {
  Pre();
  scanf("%d%d", &n, &k);
  if (k == 1)
    return puts("1"), 0;
  f[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= i; j++) {
      f[i][j] = f[i - 1][j];
      if (j)
        f[i][j] =
            add(f[i][j], 1ll * C(n * k - i - (j - 1) * (k - 1) - 1, k - 2) *
                             (n - j + 1) % mod * f[i][j - 1] % mod);
    }
  }
  printf("%d\n", f[n][n]);
  return 0;
}
Problem p04038 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
#define int unsigned long long
const int mxn = 8000005;
const int Mod = 1e9 + 7;
int frac[mxn], inv[mxn];
int f[2005][2005], n, k;
/*
设f(i,j)表示在这些位置上已经放了i个白球，j种其他颜色的球。(i<j)
f(i,j) = f(i-1, j)+f(i ,j-1)*(n-j+1)*C(k-2, n*k-i-(j-1)*(k-1)-1)
*/
int power(int a, int b) {
  int res = 1, car = a;
  while (b) {
    if (b & 1)
      (res *= car) %= Mod;
    (car *= car) %= Mod;
    b >>= 1;
  }
  return res;
}
void init() {
  frac[0] = 1;
  for (int i = 1; i < mxn; ++i)
    (frac[i] = frac[i - 1] * i) %= Mod;
  inv[mxn - 1] = power(frac[mxn - 1], Mod - 2);
  for (int i = mxn - 2; i > 0; --i)
    inv[i] = (inv[i + 1] * (i + 1)) % Mod;
  inv[0] = 1;
}
int C(int n, int k) { return ((frac[n] * inv[k] % Mod) * inv[n - k]) % Mod; }
signed main() {
  init();
  cin >> n >> k;
  if (k == 1)
    return puts("1");
  f[0][0] = 1;
  for (int i = 1; i <= n; ++i)
    for (int j = 0; j <= i; ++j)
      (f[i][j] =
           f[i - 1][j] + (j ? ((((f[i][j - 1] * (n - j + 1)) % Mod) *
                                C(n * k - i - (j - 1) * (k - 1) - 1, k - 2)) %
                               Mod)
                            : (0))) %= Mod;
  cout << f[n][n] << endl;
}

Fixed submission:
#include <iostream>
using namespace std;
#define int unsigned long long
const int mxn = 8000005;
const int Mod = 1e9 + 7;
int frac[mxn], inv[mxn];
int f[2005][2005], n, k;
/*
设f(i,j)表示在这些位置上已经放了i个白球，j种其他颜色的球。(i<j)
f(i,j) = f(i-1, j)+f(i ,j-1)*(n-j+1)*C(k-2, n*k-i-(j-1)*(k-1)-1)
*/
int power(int a, int b) {
  int res = 1, car = a;
  while (b) {
    if (b & 1)
      (res *= car) %= Mod;
    (car *= car) %= Mod;
    b >>= 1;
  }
  return res;
}
void init() {
  frac[0] = 1;
  for (int i = 1; i < mxn; ++i)
    (frac[i] = frac[i - 1] * i) %= Mod;
  inv[mxn - 1] = power(frac[mxn - 1], Mod - 2);
  for (int i = mxn - 2; i > 0; --i)
    inv[i] = (inv[i + 1] * (i + 1)) % Mod;
  inv[0] = 1;
}
int C(int n, int k) { return ((frac[n] * inv[k] % Mod) * inv[n - k]) % Mod; }
signed main() {
  init();
  cin >> n >> k;
  if (k == 1)
    return puts("1"), 0;
  f[0][0] = 1;
  for (int i = 1; i <= n; ++i)
    for (int j = 0; j <= i; ++j)
      (f[i][j] =
           f[i - 1][j] + (j ? ((((f[i][j - 1] * (n - j + 1)) % Mod) *
                                C(n * k - i - (j - 1) * (k - 1) - 1, k - 2)) %
                               Mod)
                            : (0))) %= Mod;
  cout << f[n][n] << endl;
}
Problem p04038 (C++)

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

#define MOD 1000000007

using namespace std;

typedef long long LL;

const int MAXN = 2000 + 10;
const int MAXM = 4e06 + 10;

int N, K;

LL fact[MAXM] = {0}, invfact[MAXM] = {0};
void init() {
  fact[0] = fact[1] = invfact[0] = invfact[1] = 1;
  for (int i = 2; i <= N * K; i++) {
    fact[i] = fact[i - 1] * i % MOD;
    invfact[i] = (MOD - MOD / i) * invfact[MOD % i] % MOD;
  }
  for (int i = 1; i <= N * K; i++)
    invfact[i] = invfact[i - 1] * invfact[i] % MOD;
}
inline LL C(int n, int m) {
  if (n < 0)
    return 0;
  if (m == 0)
    return 1;
  return fact[n] * invfact[m] % MOD * invfact[n - m] % MOD;
}

LL f[MAXN][MAXN] = {0};

int main() {
  scanf("%d%d", &N, &K);
  if (K == 1) {
    puts("1");
    return 0;
  }
  init();
  f[0][0] = 1;
  for (int i = 1; i <= N; i++)
    for (int j = 0; j <= i; j++) {
      LL delta = f[i][j - 1] * (N - j + 1) % MOD *
                 C(N * K - i - (K - 1) * (j - 1) - 1, K - 2) % MOD;
      if (j == 0)
        delta = 0;
      f[i][j] = (f[i][j] + f[i - 1][j] + delta) % MOD;
    }
  cout << f[N][N] << endl;

  return 0;
}

/*
2 2
*/

/*
3 1
*/

/*
2 3
*/

/*
2000 2000
*/

Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>

#define MOD 1000000007

using namespace std;

typedef long long LL;

const int MAXN = 2000 + 10;
const int MAXM = MAXN * MAXN;

int N, K;

LL fact[MAXM] = {0}, invfact[MAXM] = {0};
void init() {
  fact[0] = fact[1] = invfact[0] = invfact[1] = 1;
  for (int i = 2; i <= N * K; i++) {
    fact[i] = fact[i - 1] * i % MOD;
    invfact[i] = (MOD - MOD / i) * invfact[MOD % i] % MOD;
  }
  for (int i = 1; i <= N * K; i++)
    invfact[i] = invfact[i - 1] * invfact[i] % MOD;
}
inline LL C(int n, int m) {
  if (n < 0)
    return 0;
  if (m == 0)
    return 1;
  return fact[n] * invfact[m] % MOD * invfact[n - m] % MOD;
}

LL f[MAXN][MAXN] = {0};

int main() {
  scanf("%d%d", &N, &K);
  if (K == 1) {
    puts("1");
    return 0;
  }
  init();
  f[0][0] = 1;
  for (int i = 1; i <= N; i++)
    for (int j = 0; j <= i; j++) {
      LL delta = f[i][j - 1] * (N - j + 1) % MOD *
                 C(N * K - i - (K - 1) * (j - 1) - 1, K - 2) % MOD;
      if (j == 0)
        delta = 0;
      f[i][j] = (f[i][j] + f[i - 1][j] + delta) % MOD;
    }
  cout << f[N][N] << endl;

  return 0;
}

/*
2 2
*/

/*
3 1
*/

/*
2 3
*/

/*
2000 2000
*/
Problem p04038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mod 1000000007
using namespace std;
int read();
int M(int x) { return x >= mod ? x - mod : x; }
int fsp(int bs, int p) {
  int rt = 1;
  while (p) {
    if (p & 1)
      rt = 1ll * rt * bs % mod;
    bs = 1ll * bs * bs % mod, p >>= 1;
  }
  return rt;
}

int n, k;
int fac[4000006], caf[4000006];
void init() {
  int lim = 4000006;
  fac[0] = 1;
  for (int i = 1; i <= lim; ++i)
    fac[i] = 1ll * fac[i - 1] * i % mod;
  caf[lim] = fsp(fac[lim], mod - 2);
  for (int i = lim; i >= 1; --i)
    caf[i - 1] = 1ll * caf[i] * i % mod;
}
int C(int x, int y) { return 1ll * fac[x] * caf[y] % mod * caf[x - y] % mod; }
int f[2003][2003];

int main() {
  n = read(), k = read(), init();
  if (k == 1)
    return puts("1"), 0;
  for (int i = 1; i <= n; ++i)
    f[i][0] = 1;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= i; ++j)
      f[i][j] = M(f[i - 1][j] +
                  1ll * f[i][j - 1] * (n - j + 1) % mod *
                      C(n - i + (n - j + 1) * (k - 1) - 1, k - 2) % mod);
  printf("%d\n", f[n][n]);
  return 0;
}

int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9')
    f = (c == '-') ? -1 : f, c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x * f;
}


Fixed submission:
#include <bits/stdc++.h>
#define mod 1000000007
using namespace std;
int read();
int M(int x) { return x >= mod ? x - mod : x; }
int fsp(int bs, int p) {
  int rt = 1;
  while (p) {
    if (p & 1)
      rt = 1ll * rt * bs % mod;
    bs = 1ll * bs * bs % mod, p >>= 1;
  }
  return rt;
}

int n, k;
int fac[4000006], caf[4000006];
void init() {
  int lim = 4000000;
  fac[0] = 1;
  for (int i = 1; i <= lim; ++i)
    fac[i] = 1ll * fac[i - 1] * i % mod;
  caf[lim] = fsp(fac[lim], mod - 2);
  for (int i = lim; i >= 1; --i)
    caf[i - 1] = 1ll * caf[i] * i % mod;
}
int C(int x, int y) { return 1ll * fac[x] * caf[y] % mod * caf[x - y] % mod; }
int f[2003][2003];

int main() {
  n = read(), k = read(), init();
  if (k == 1)
    return puts("1"), 0;
  for (int i = 1; i <= n; ++i)
    f[i][0] = 1;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= i; ++j)
      f[i][j] = M(f[i - 1][j] +
                  1ll * f[i][j - 1] * (n - j + 1) % mod *
                      C(n - i + (n - j + 1) * (k - 1) - 1, k - 2) % mod);
  printf("%d\n", f[n][n]);
  return 0;
}

int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9')
    f = (c == '-') ? -1 : f, c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x * f;
}
Problem p04038 (C++)

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

ll n, k;
const int MAXN = 2000 * 2000 + 1;
long long kai[MAXN];
long long inv[MAXN];
long long mod = 1e9 + 7;
long long mp(long long a, long long b) {
  long long res = 1;
  for (; b; b >>= 1) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
  }
  return res;
}
void init() {
  kai[0] = 1;
  for (long long i = 1; i <= MAXN; i++)
    kai[i] = kai[i - 1] * i % mod;
  inv[MAXN] = mp(kai[MAXN], mod - 2);
  for (long long i = MAXN; i >= 1; i--)
    inv[i - 1] = inv[i] * i % mod;
}
long long calc(long long n, long long k) {
  if (n < k)
    return 0;
  return (kai[n] * inv[k]) % mod * inv[n - k] % mod;
}

ll dp[2010][2010];
bool ok[2010][2010];

ll dfs(int a, int b) {
  if (ok[a][b])
    return dp[a][b];
  ll ret = 0;
  if (a - 1 >= 0)
    ret += dfs(a - 1, b);
  if (b - 1 >= a)
    ret += dfs(a, b - 1) * calc(a + b * (k - 1) - 1, k - 2) % mod;
  ok[a][b] = 1;
  return dp[a][b] = ret % mod;
}

int main() {
  cin >> n >> k;
  init();
  if (k == 1) {
    cout << 1 << endl;
    return 0;
  }
  dp[0][0] = 1;
  ok[0][0] = 1;
  ll ret = dfs(n, n);
  for (ll i = 1; i <= n; i++)
    ret = ret * i % mod;
  cout << ret << endl;
  return 0;
}

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

ll n, k;
const int MAXN = 2000 * 2000;
long long kai[MAXN + 1];
long long inv[MAXN + 1];
long long mod = 1e9 + 7;
long long mp(long long a, long long b) {
  long long res = 1;
  for (; b; b >>= 1) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
  }
  return res;
}
void init() {
  kai[0] = 1;
  for (long long i = 1; i <= MAXN; i++)
    kai[i] = kai[i - 1] * i % mod;
  inv[MAXN] = mp(kai[MAXN], mod - 2);
  for (long long i = MAXN; i >= 1; i--)
    inv[i - 1] = inv[i] * i % mod;
}
long long calc(long long n, long long k) {
  if (n < k)
    return 0;
  return (kai[n] * inv[k]) % mod * inv[n - k] % mod;
}

ll dp[2010][2010];
bool ok[2010][2010];

ll dfs(int a, int b) {
  if (ok[a][b])
    return dp[a][b];
  ll ret = 0;
  if (a - 1 >= 0)
    ret += dfs(a - 1, b);
  if (b - 1 >= a)
    ret += dfs(a, b - 1) * calc(a + b * (k - 1) - 1, k - 2) % mod;
  ok[a][b] = 1;
  return dp[a][b] = ret % mod;
}

int main() {
  cin >> n >> k;
  init();
  if (k == 1) {
    cout << 1 << endl;
    return 0;
  }
  dp[0][0] = 1;
  ok[0][0] = 1;
  ll ret = dfs(n, n);
  for (ll i = 1; i <= n; i++)
    ret = ret * i % mod;
  cout << ret << endl;
  return 0;
}
Problem p04038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int f[2010][2010], fac[4000010], inv[4000010];
inline int qpow(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1)
      ans = 1ll * ans * a % mod;
    a = 1ll * a * a % mod;
    b >>= 1;
  }
  return ans;
}
inline int C(int n, int m) {
  return 1ll * fac[n] * inv[m] % mod * inv[n - m] % mod;
}
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  if (k == 1) {
    puts("1");
    return 0;
  }
  fac[0] = 1;
  for (int i = 1; i <= n * k; i++)
    fac[i] = 1ll * fac[i - 1] * i % mod;
  inv[n * k] = qpow(fac[n * k], mod - 2);
  for (int i = n * k - 1; i >= 0; i--)
    inv[i] = 1ll * inv[i + 1] * (i + 1) % mod;
  f[0][0] = 1;
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= i; j++) {
      f[i + 1][j] = (f[i + 1][j] + f[i][j]) % mod;
      f[i][j + 1] = (f[i][j + 1] +
                     1ll * C(n * k - i - j * (k - 1) - 1, k - 2) * f[i][j]) %
                    mod;
    }
  printf("%lld\n", 1ll * f[n][n] * fac[n] % mod);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int f[2010][2010], fac[4000010], inv[4000010];
inline int qpow(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1)
      ans = 1ll * ans * a % mod;
    a = 1ll * a * a % mod;
    b >>= 1;
  }
  return ans;
}
inline int C(int n, int m) {
  if (n < m)
    return 0;
  return 1ll * fac[n] * inv[m] % mod * inv[n - m] % mod;
}
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  if (k == 1) {
    puts("1");
    return 0;
  }
  fac[0] = 1;
  for (int i = 1; i <= n * k; i++)
    fac[i] = 1ll * fac[i - 1] * i % mod;
  inv[n * k] = qpow(fac[n * k], mod - 2);
  for (int i = n * k - 1; i >= 0; i--)
    inv[i] = 1ll * inv[i + 1] * (i + 1) % mod;
  f[0][0] = 1;
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= i; j++) {
      f[i + 1][j] = (f[i + 1][j] + f[i][j]) % mod;
      f[i][j + 1] = (f[i][j + 1] +
                     1ll * C(n * k - i - j * (k - 1) - 1, k - 2) * f[i][j]) %
                    mod;
    }
  printf("%lld\n", 1ll * f[n][n] * fac[n] % mod);
  return 0;
}
Problem p04039 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;

int n, k;
vector<int> d(9);

int check(int m) {

  while (m > 0) {

    int flag = 0;
    rep(i, k) {
      if (m % 10 == d[i]) {
        flag = 1;
        break;
      }
    }

    if (flag) {
      return 0;
    }
  }

  return 1;
}

int main() {

  cin >> n >> k;

  rep(i, k) { cin >> d[i]; }

  int x = n;
  while (1) {

    if (check(x)) {
      cout << x << endl;
      return 0;
    }
    x++;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;

int n, k;
vector<int> d(9);

int check(int m) {

  while (m > 0) {

    int flag = 0;
    rep(i, k) {
      if (m % 10 == d[i]) {
        flag = 1;
        break;
      }
    }

    if (flag) {
      return 0;
    }

    m /= 10;
  }

  return 1;
}

int main() {

  cin >> n >> k;

  rep(i, k) { cin >> d[i]; }

  int x = n;
  while (1) {

    if (check(x)) {
      cout << x << endl;
      return 0;
    }
    x++;
  }
}
Problem p04039 (C++)

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

using namespace std;

typedef long long ll;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> bad(k, false);
  for (int i = 0; i < k; i++) {
    int x;
    cin >> x;
    bad[x] = true;
  }
  auto ok = [&](int x) {
    while (x > 0) {
      if (bad[x % 10]) {
        return false;
      }
      x /= 10;
    }
    return true;
  };
  while (true) {
    if (ok(n)) {
      cout << n << '\n';
      break;
    }
    n++;
  }
  return 0;
}

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

using namespace std;

typedef long long ll;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> bad(10, false);
  for (int i = 0; i < k; i++) {
    int x;
    cin >> x;
    bad[x] = true;
  }
  auto ok = [&](int x) {
    while (x > 0) {
      if (bad[x % 10]) {
        return false;
      }
      x /= 10;
    }
    return true;
  };
  while (true) {
    if (ok(n)) {
      cout << n << '\n';
      break;
    }
    n++;
  }
  return 0;
}
Problem p04039 (C++)

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

using namespace std;
#define sqr(x) ((x) * (x))
#define cube(x) ((x) * (x) * (x))
#define GCD(a, b) __gcd(a, b)
#define LCM(a, b) ((a) * (b) / GCD(a, b))
#define MP make_pair
#define n1 first
#define n2 second
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define SI set<int>
#define SL set<ll>
#define MS multiset
#define MSI multiset<int>
#define MSLL multiset<ll>
#define PB push_back
#define PF push_front
#define VI vector<int>
#define VPI vector<pair<int, int>>
#define VLL vector<ll>
#define SZ(x) ((int)x.size())

typedef long long ll;

typedef unsigned long long ull;
typedef long double ld;
typedef double db;
const int INF = 1e9;
const int MOD = 1000000007;
const int N = 202055;
const int M = 1211;

void solve() {
  int n, k;
  cin >> k >> n;

  set<int> s;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    s.insert(x);
  }

  for (int i = k; i <= 1e6; i++) {
    int x = i;
    string str;
    while (x > 0) {
      str += x % 10 + '0';
      x /= 10;
    }
    int cnt = 0;
    for (int j = 0; j < SZ(str); j++) {
      int y = str[j] - '0';
      if (!s.count(y))
        cnt++;
    }
    if (cnt == SZ(str)) {
      cout << i;
      return;
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int T = 1;
  // cin >> T;

  while (T--) {
    solve();
  }

  return 0;
}

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

using namespace std;
#define sqr(x) ((x) * (x))
#define cube(x) ((x) * (x) * (x))
#define GCD(a, b) __gcd(a, b)
#define LCM(a, b) ((a) * (b) / GCD(a, b))
#define MP make_pair
#define n1 first
#define n2 second
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define SI set<int>
#define SL set<ll>
#define MS multiset
#define MSI multiset<int>
#define MSLL multiset<ll>
#define PB push_back
#define PF push_front
#define VI vector<int>
#define VPI vector<pair<int, int>>
#define VLL vector<ll>
#define SZ(x) ((int)x.size())

typedef long long ll;

typedef unsigned long long ull;
typedef long double ld;
typedef double db;
const int INF = 1e9;
const int MOD = 1000000007;
const int N = 202055;
const int M = 1211;

void solve() {
  int n, k;
  cin >> k >> n;

  set<int> s;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    s.insert(x);
  }

  for (int i = k; i <= 1e6; i++) {
    int x = i;
    string str;
    while (x > 0) {
      str += x % 10 + '0';
      x /= 10;
    }
    int cnt = 0;
    for (int j = 0; j < SZ(str); j++) {
      int y = str[j] - '0';
      if (!s.count(y))
        cnt++;
    }
    if (cnt == SZ(str)) {
      cout << i;
      return;
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);

  int T = 1;
  // cin >> T;

  while (T--) {
    solve();
  }

  return 0;
}