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

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

#pragma GCC optimize("O2,no-stack-protector,unroll-loops,fast-math")

#define F first
#define S second
#define pb push_back
#define SZ(x) (ll)(x.size())
#define all(x) x.begin(), x.end()
#define MP make_pair

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const ll maxn = 2e3 + 10, maxm = 5e4 + 10, lg = 21, mod = 1e9 + 7, inf = 1e18;

string s, t;
ll n, L[maxn], R[maxn];

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

  memset(L, 69, sizeof(L)), memset(R, 69, sizeof(R));
  cin >> s >> t;
  n = SZ(s);
  t = t + t + t;
  for (int i = 0; i < 3 * n; i++)
    L[i] = (t[i] == '1' ? 0 : L[i - 1] + 1);
  for (int i = 3 * n - 1; i >= 0; i--)
    R[i] = (t[i] == '1' ? 0 : R[i + 1] + 1);
  if (R[0] >= inf) {
    for (int i = 0; i < n; i++)
      if (s[i] == '1')
        return cout << -1, 0;
    return cout << 0, 0;
  }
  ll ans = inf;
  for (int d = -n; d <= n; d++) {
    vector<pll> bb;
    ll ek = 0;
    for (int i = n; i < 2 * n; i++)
      if (s[i - n] != t[i + d])
        bb.pb({max(L[i] + min(d, 0), 0ll), max(R[i] - max(d, 0), 0ll)}), ek++;
    sort(all(bb));
    ll mx = 0;
    for (int i = SZ(bb) - 1; i >= 0; i--) {
      ans = min(ans, ek + 2 * (bb[i].F + mx) + abs(d));
      mx = max(mx, bb[i].S);
    }
    ans = min(ans, ek + 2 * mx + abs(d));
  }
  cout << ans;

  return 0;
}


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

#pragma GCC optimize("O2,no-stack-protector,unroll-loops,fast-math")

#define F first
#define S second
#define pb push_back
#define SZ(x) (ll)(x.size())
#define all(x) x.begin(), x.end()
#define MP make_pair

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const ll maxn = 2e3 + 10, maxm = 5e4 + 10, lg = 21, mod = 1e9 + 7, inf = 1e18;

string s, t;
ll n, L[3 * maxn], R[3 * maxn];

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

  memset(L, 69, sizeof(L)), memset(R, 69, sizeof(R));
  cin >> s >> t;
  n = SZ(s);
  t = t + t + t;
  for (int i = 0; i < 3 * n; i++)
    L[i] = (t[i] == '1' ? 0 : L[i - 1] + 1);
  for (int i = 3 * n - 1; i >= 0; i--)
    R[i] = (t[i] == '1' ? 0 : R[i + 1] + 1);
  if (R[0] >= inf) {
    for (int i = 0; i < n; i++)
      if (s[i] == '1')
        return cout << -1, 0;
    return cout << 0, 0;
  }
  ll ans = inf;
  for (int d = -n; d <= n; d++) {
    vector<pll> bb;
    ll ek = 0;
    for (int i = n; i < 2 * n; i++)
      if (s[i - n] != t[i + d])
        bb.pb({max(L[i] + min(d, 0), 0ll), max(R[i] - max(d, 0), 0ll)}), ek++;
    sort(all(bb));
    ll mx = 0;
    for (int i = SZ(bb) - 1; i >= 0; i--) {
      ans = min(ans, ek + 2 * (bb[i].F + mx) + abs(d));
      mx = max(mx, bb[i].S);
    }
    ans = min(ans, ek + 2 * mx + abs(d));
  }
  cout << ans;

  return 0;
}
Problem p03620 (C++)

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

#define fir first
#define sec second
using namespace std;
typedef long long LL;
typedef unsigned long long u64;

template <typename T> inline bool cmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline bool cmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
int inp() {
  int x = 0, f = 1;
  char ch;
  for (ch = getchar(); !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
    ;
  return x * f;
}

const int MaxN = 2333;

char a[MaxN], b[MaxN];
int n, ans = ~0U >> 1, x[MaxN], y[MaxN];
int A[MaxN], B[MaxN];

void solve() {
  int i, j;
  for (i = 0; i < n; i++) {
    for (A[i] = 0; b[(i + A[i]) % n] != '1'; ++A[i])
      ;
    for (B[i] = 0; b[(i + B[i] + n) % n] != '1'; --B[i])
      ;
  }
  for (int d = 0; d < n; d++) {
    for (i = 0; i < n; i++)
      x[i] = 0;
    for (i = j = 0; i < n; i++)
      if (a[i] != b[(i + d) % n]) {
        cmax(x[-B[i]], A[i] - d);
        ++j;
      }
    int L, R, res = 1e9;
    for (L = 0, R = n - 1; ~R; R--)
      cmin(res, L + R), cmax(L, x[R]);
    cmin(ans, 2 * res + d + j);
  }
}

int main() {
  int i;
  scanf("%s%s", a, b);
  n = strlen(a);
  solve();
  reverse(a, a + n);
  reverse(b, b + n);
  solve();

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


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

#define fir first
#define sec second
using namespace std;
typedef long long LL;
typedef unsigned long long u64;

template <typename T> inline bool cmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline bool cmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
int inp() {
  int x = 0, f = 1;
  char ch;
  for (ch = getchar(); !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
    ;
  return x * f;
}

const int MaxN = 2333;

char a[MaxN], b[MaxN];
int n, ans = ~0U >> 1, x[MaxN], y[MaxN];
int A[MaxN], B[MaxN];

void solve() {
  int i, j;
  for (i = 0; i < n; i++) {
    for (A[i] = 0; b[(i + A[i]) % n] != '1'; ++A[i])
      ;
    for (B[i] = 0; b[(i + B[i] + n) % n] != '1'; --B[i])
      ;
  }
  for (int d = 0; d < n; d++) {
    for (i = 0; i < n; i++)
      x[i] = 0;
    for (i = j = 0; i < n; i++)
      if (a[i] != b[(i + d) % n]) {
        cmax(x[-B[i]], A[i] - d);
        ++j;
      }
    int L, R, res = 1e9;
    for (L = 0, R = n - 1; ~R; R--)
      cmin(res, L + R), cmax(L, x[R]);
    cmin(ans, 2 * res + d + j);
  }
}

int main() {
  int i;
  scanf("%s%s", a, b);
  n = strlen(a);
  if (*max_element(b, b + n) != '1') {
    cout << (*max_element(a, a + n) != '1' ? 0 : -1) << endl;
    return 0;
  }
  solve();
  reverse(a, a + n);
  reverse(b, b + n);
  solve();

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

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

const int N = 2005;

char s[N];
int n, r[N], l[N], f[N], ans = 1e9;
bool a[N], b[N];
vector<int> p[N];

int gr(int x) {
  if (r[x] != -1)
    return r[x];
  return r[x] = b[x] ? 0 : gr((x + 1) % n) + 1;
}

int gl(int x) {
  if (l[x] != -1)
    return l[x];
  return l[x] = b[x] ? 0 : gl((x - 1 + n) % n) + 1;
}

void Max(int &x, int y) {
  if (x < y)
    x = y;
}
void Min(int &x, int y) {
  if (x > y)
    x = y;
}

void Solve() {
  memset(l, -1, sizeof l);
  memset(r, -1, sizeof r);
  for (int i = 0; i < n; ++i)
    gr(i), gl(i);
  f[n - 1] = 0;
  for (int i = 0; i < n; ++i) {
    int cnt = 0;
    for (int j = 0; j < n; ++j)
      p[j].clear();
    for (int j = 0; j < n; ++j)
      if ((a[j] ^ b[(j + i) % n])) {
        ++cnt;
        p[r[j]].emplace_back(l[j]);
      }
    for (int j = n - 1; j; --j) {
      f[j - 1] = f[j];
      for (auto v : p[j])
        Max(f[j - 1], v);
    }
    for (int j = i; j < n; ++j)
      Min(ans, cnt + j + j - i + f[j] * 2);
  }
}

int main() {
  scanf("%s", s);
  n = strlen(s);
  for (int i = 0; i < n; ++i)
    a[i] = s[i] - '0';
  scanf("%s", s);
  for (int i = 0; i < n; ++i)
    b[i] = s[i] - '0';
  if (*max_element(b, b + n) == 0 && *max_element(a, a + n) == 1)
    return puts("-1"), 0;
  Solve();
  reverse(a, a + n);
  reverse(b, b + n);
  Solve();
  printf("%d\n", ans);
  return 0;
}

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

const int N = 2005;

char s[N];
int n, r[N], l[N], f[N], ans = 1e9;
bool a[N], b[N];
vector<int> p[N];

int gr(int x) {
  if (r[x] != -1)
    return r[x];
  return r[x] = b[x] ? 0 : gr((x + 1) % n) + 1;
}

int gl(int x) {
  if (l[x] != -1)
    return l[x];
  return l[x] = b[x] ? 0 : gl((x - 1 + n) % n) + 1;
}

void Max(int &x, int y) {
  if (x < y)
    x = y;
}
void Min(int &x, int y) {
  if (x > y)
    x = y;
}

void Solve() {
  memset(l, -1, sizeof l);
  memset(r, -1, sizeof r);
  for (int i = 0; i < n; ++i)
    gr(i), gl(i);
  f[n - 1] = 0;
  for (int i = 0; i < n; ++i) {
    int cnt = 0;
    for (int j = 0; j < n; ++j)
      p[j].clear();
    for (int j = 0; j < n; ++j)
      if ((a[j] ^ b[(j + i) % n])) {
        ++cnt;
        p[r[j]].emplace_back(l[j]);
      }
    for (int j = n - 1; j; --j) {
      f[j - 1] = f[j];
      for (auto v : p[j])
        Max(f[j - 1], v);
    }
    for (int j = i; j < n; ++j)
      Min(ans, cnt + j + j - i + f[j] * 2);
  }
}

int main() {
  scanf("%s", s);
  n = strlen(s);
  for (int i = 0; i < n; ++i)
    a[i] = s[i] - '0';
  scanf("%s", s);
  for (int i = 0; i < n; ++i)
    b[i] = s[i] - '0';
  if (*max_element(b, b + n) == 0) {
    if (*max_element(a, a + n) == 1)
      return puts("-1"), 0;
    return puts("0"), 0;
  }
  Solve();
  reverse(a, a + n);
  reverse(b, b + n);
  Solve();
  printf("%d\n", ans);
  return 0;
}
Problem p03620 (C++)

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

const int N = 2010;
string A, B;
int n;
int ans = 1e9;
int ml[N], mr[N], mx[N];

void solve() {
  for (int i = 0; i < n; ++i) {
    ml[i] = mr[i] = 0;
    while (B[(i + mr[i]) % n] != '1')
      ++mr[i];
    while (B[(i - ml[i] + n) % n] != '1')
      ++ml[i];
  }
  for (int s = 0; s < n; ++s) {
    for (int i = 0; i < n; ++i)
      mx[i] = 0;
    int dif = 0;
    for (int i = 0; i < n; ++i)
      if (A[i] != B[(i + s) % n]) {
        ++dif;
        mx[ml[i]] = max(mx[ml[i]], mr[i] - s);
      }

    int add = 1e9, cur = 0;
    for (int i = n - 1; i >= 0; --i) {
      cur = max(cur, mx[i + 1]);
      add = min(add, i + cur);
    }

    ans = min(ans, 2 * add + s + dif);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> A >> B;
  n = (int)A.size();

  bool full0 = 1;
  for (int i = 0; i < (int)B.size(); ++i) {
    full0 &= (B[i] == '0');
  }
  if (full0) {
    for (int i = 0; i < (int)A.size(); ++i)
      full0 &= (A[i] == '0');
    if (!full0)
      return printf("-1\n"), 0;
  }

  solve();

  reverse(A.begin(), A.end());
  reverse(B.begin(), B.end());
  solve();

  printf("%d\n", ans);
}

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

const int N = 2010;
string A, B;
int n;
int ans = 1e9;
int ml[N], mr[N], mx[N];

void solve() {
  for (int i = 0; i < n; ++i) {
    ml[i] = mr[i] = 0;
    while (B[(i + mr[i]) % n] != '1')
      ++mr[i];
    while (B[(i - ml[i] + n) % n] != '1')
      ++ml[i];
  }
  for (int s = 0; s < n; ++s) {
    for (int i = 0; i < n; ++i)
      mx[i] = 0;
    int dif = 0;
    for (int i = 0; i < n; ++i)
      if (A[i] != B[(i + s) % n]) {
        ++dif;
        mx[ml[i]] = max(mx[ml[i]], mr[i] - s);
      }

    int add = 1e9, cur = 0;
    for (int i = n - 1; i >= 0; --i) {
      cur = max(cur, mx[i + 1]);
      add = min(add, i + cur);
    }

    ans = min(ans, 2 * add + s + dif);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> A >> B;
  n = (int)A.size();

  bool full0 = 1;
  for (int i = 0; i < (int)B.size(); ++i) {
    full0 &= (B[i] == '0');
  }
  if (full0) {
    for (int i = 0; i < (int)A.size(); ++i)
      full0 &= (A[i] == '0');
    if (!full0)
      return printf("-1\n"), 0;
    else
      return printf("0\n"), 0;
  }

  solve();

  reverse(A.begin(), A.end());
  reverse(B.begin(), B.end());
  solve();

  printf("%d\n", ans);
}
Problem p03620 (C++)

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

const int maxn = 2010;

char s[maxn];
int n, A[maxn], B[maxn], C[maxn];
int L[maxn], R[maxn], mx[maxn], Ans = 0x7fffffff;

void solve() {
  int i, p;
  for (i = 0; i < n; i++) {
    for (R[i] = 0, p = i; !B[p]; p = (p + 1) % n)
      R[i]++;
    for (L[i] = 0, p = i; !B[p]; p = (p + n - 1) % n)
      L[i]++;
  }
  for (p = 0; p < n; p++) {
    int Sum = p;
    for (i = 0; i < n; i++)
      Sum += C[i] = A[i] ^ B[(i + n - p) % n];
    memset(mx, 0, sizeof(mx));
    for (i = 0; i < n; i++)
      if (C[i] && L[i] > p)
        mx[L[i] - p - 1] = max(mx[L[i] - p - 1], R[i]);
    for (i = n - 1; i >= 0; i--)
      mx[i] = max(mx[i], mx[i + 1]);
    for (i = 0; i < n; i++)
      Ans = min(Ans, Sum + 2 * (i + mx[i]));
  }
}

int main() {
#ifdef h10
  freopen("D.in", "r", stdin);
  freopen("D.out", "w", stdout);
#endif
  int i, Acnt = 0, Bcnt = 0;
  scanf("%s", s);
  n = strlen(s);
  for (i = 0; i < n; i++)
    Acnt += A[i] = s[i] == '1';
  scanf("%s", s);
  for (i = 0; i < n; i++)
    Bcnt += B[i] = s[i] == '1';
  if (Acnt && !Bcnt) {
    puts("-1");
    return 0;
  }
  solve();

  for (i = 0; i < n; i++)
    if (i < n - 1 - i) {
      swap(A[i], A[n - 1 - i]);
      swap(B[i], B[n - 1 - i]);
    }
  solve();
  printf("%d\n", Ans);
}


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

const int maxn = 2010;

char s[maxn];
int n, A[maxn], B[maxn], C[maxn];
int L[maxn], R[maxn], mx[maxn], Ans = 0x7fffffff;

void solve() {
  int i, p;
  for (i = 0; i < n; i++) {
    for (R[i] = 0, p = i; !B[p]; p = (p + 1) % n)
      R[i]++;
    for (L[i] = 0, p = i; !B[p]; p = (p + n - 1) % n)
      L[i]++;
  }
  for (p = 0; p < n; p++) {
    int Sum = p;
    for (i = 0; i < n; i++)
      Sum += C[i] = A[i] ^ B[(i + n - p) % n];
    memset(mx, 0, sizeof(mx));
    for (i = 0; i < n; i++)
      if (C[i] && L[i] > p)
        mx[L[i] - p - 1] = max(mx[L[i] - p - 1], R[i]);
    for (i = n - 1; i >= 0; i--)
      mx[i] = max(mx[i], mx[i + 1]);
    for (i = 0; i < n; i++)
      Ans = min(Ans, Sum + 2 * (i + mx[i]));
  }
}

int main() {
#ifdef h10
  freopen("D.in", "r", stdin);
  freopen("D.out", "w", stdout);
#endif
  int i, Acnt = 0, Bcnt = 0;
  scanf("%s", s);
  n = strlen(s);
  for (i = 0; i < n; i++)
    Acnt += A[i] = s[i] == '1';
  scanf("%s", s);
  for (i = 0; i < n; i++)
    Bcnt += B[i] = s[i] == '1';
  if (!Bcnt) {
    puts(Acnt ? "-1" : "0");
    return 0;
  }
  solve();

  for (i = 0; i < n; i++)
    if (i < n - 1 - i) {
      swap(A[i], A[n - 1 - i]);
      swap(B[i], B[n - 1 - i]);
    }
  solve();
  printf("%d\n", Ans);
}
Problem p03622 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#define repu(i, x, y) for (int i = x; i <= y; ++i)
using namespace std;

typedef long long LL;
const int p = 998244353;
int n, m, fac[1000100], inv[1000100], ans;

int power(int x, int k) {
  int ret = 1;
  for (; k; k >>= 1, x = LL(x) * x % p)
    if (k & 1)
      ret = LL(ret) * x % p;
  return ret;
}

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

int main() {
  scanf("%d%d", &n, &m);
  fac[0] = fac[1] = inv[0] = inv[1] = 1;
  repu(i, 2, n + m) inv[i] = LL(p - p / i) * inv[p % i] % p;
  repu(i, 2, n + m) inv[i] = LL(inv[i - 1]) * inv[i] % p,
                    fac[i] = LL(fac[i - 1]) * i % p;
  repu(i, 1, n + m) ans = (ans + LL(c(i * 2, i)) * c(n + m - i * 2, n - i)) % p;
  ans = LL(ans) * power(c(n + m, n) * 2 % p, p - 2) % p;
  printf("%d\n", ans + max(n, m));
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#define repu(i, x, y) for (int i = x; i <= y; ++i)
using namespace std;

typedef long long LL;
const int p = 998244353;
int n, m, fac[1000100], inv[1000100], ans;

int power(int x, int k) {
  int ret = 1;
  for (; k; k >>= 1, x = LL(x) * x % p)
    if (k & 1)
      ret = LL(ret) * x % p;
  return ret;
}

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

int main() {
  scanf("%d%d", &n, &m);
  fac[0] = fac[1] = inv[0] = inv[1] = 1;
  repu(i, 2, n + m) inv[i] = LL(p - p / i) * inv[p % i] % p;
  repu(i, 2, n + m) inv[i] = LL(inv[i - 1]) * inv[i] % p,
                    fac[i] = LL(fac[i - 1]) * i % p;
  repu(i, 1, min(n, m)) ans =
      (ans + LL(c(i * 2, i)) * c(n + m - i * 2, n - i)) % p;
  ans = LL(ans) * power(c(n + m, n) * 2 % p, p - 2) % p;
  printf("%d\n", ans + max(n, m));
  return 0;
}
Problem p03622 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define MAXN 100005
#define P 998244353
#define INF 0x3f3f3f3f
#define rint register int
#define LL long long
#define LD long double
using namespace std;

int n, m, ans, fac[MAXN * 2], ifac[MAXN * 2];

inline int add(int x, int y) { return x + y >= P ? x + y - P : x + y; }

inline int ksm(int x, int y) {
  int sum = 1;
  while (y) {
    if (y & 1)
      sum = 1LL * sum * x % P;
    x = 1LL * x * x % P, y >>= 1;
  }
  return sum;
}

void init() {
  ifac[0] = ifac[1] = fac[0] = fac[1] = 1;
  for (rint i = 2; i <= n + m; ++i)
    ifac[i] = 1LL * ifac[P % i] * (P - P / i) % P;
  for (rint i = 2; i <= n + m; ++i) {
    ifac[i] = 1LL * ifac[i - 1] * ifac[i] % P;
    fac[i] = 1LL * fac[i - 1] * i % P;
  }
}

int comb(int x, int y) { return 1LL * fac[x] * ifac[y] % P * ifac[x - y] % P; }

int main() {
  scanf("%d%d", &n, &m);
  if (n < m)
    swap(n, m);
  init();
  for (rint i = 1; i <= m; ++i)
    ans = add(ans, 1LL * comb(i * 2, i) * comb(n + m - i * 2, n - i) % P);
  ans = 1LL * ans * ksm(2LL * comb(n + m, m) % P, P - 2) % P;
  printf("%d\n", add(ans, n));
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define MAXN 500005
#define P 998244353
#define INF 0x3f3f3f3f
#define rint register int
#define LL long long
#define LD long double
using namespace std;

int n, m, ans, fac[MAXN * 2], ifac[MAXN * 2];

inline int add(int x, int y) { return x + y >= P ? x + y - P : x + y; }

inline int ksm(int x, int y) {
  int sum = 1;
  while (y) {
    if (y & 1)
      sum = 1LL * sum * x % P;
    x = 1LL * x * x % P, y >>= 1;
  }
  return sum;
}

void init() {
  ifac[0] = ifac[1] = fac[0] = fac[1] = 1;
  for (rint i = 2; i <= n + m; ++i)
    ifac[i] = 1LL * ifac[P % i] * (P - P / i) % P;
  for (rint i = 2; i <= n + m; ++i) {
    ifac[i] = 1LL * ifac[i - 1] * ifac[i] % P;
    fac[i] = 1LL * fac[i - 1] * i % P;
  }
}

int comb(int x, int y) { return 1LL * fac[x] * ifac[y] % P * ifac[x - y] % P; }

int main() {
  scanf("%d%d", &n, &m);
  if (n < m)
    swap(n, m);
  init();
  for (rint i = 1; i <= m; ++i)
    ans = add(ans, 1LL * comb(i * 2, i) * comb(n + m - i * 2, n - i) % P);
  ans = 1LL * ans * ksm(2LL * comb(n + m, m) % P, P - 2) % P;
  printf("%d\n", add(ans, n));
  return 0;
}
Problem p03622 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define MAXN 500000
#define MOD 998244353
#define LL long long
int n, m;
int inv[MAXN + 5], fac[MAXN + 5];
int ans;
int fst_pow(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      res = (1LL * res * a) % MOD;
    a = (1LL * a * a) % MOD;
    b >>= 1;
  }
  return res;
}
void prepare() {
  fac[0] = 1;
  for (int i = 1; i <= n + m; i++)
    fac[i] = (1LL * fac[i - 1] * i) % MOD;
  inv[n + m] = fst_pow(fac[n + m], MOD - 2);
  for (int i = n + m - 1; i >= 0; i--)
    inv[i] = (1LL * inv[i + 1] * (i + 1)) % MOD;
}
int Comb(int a, int b) {
  return 1LL * (1LL * fac[a] * inv[a - b] % MOD) * inv[b] % MOD;
}
int main() {
  scanf("%d%d", &n, &m);
  if (n < m)
    swap(n, m);
  prepare();
  for (int i = 1; i <= m; i++)
    ans = (ans + 1LL * Comb(2 * i, i) * Comb(n + m - 2 * i, n - i) % MOD) % MOD;
  ans = 1LL * ans * fst_pow(Comb(m + n, n), MOD - 2) % MOD;
  ans = 1LL * ans * fst_pow(2, MOD - 2) % MOD;
  printf("%d", ans + n);
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define MAXN 1000000
#define MOD 998244353
#define LL long long
int n, m;
int inv[MAXN + 5], fac[MAXN + 5];
int ans;
int fst_pow(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      res = (1LL * res * a) % MOD;
    a = (1LL * a * a) % MOD;
    b >>= 1;
  }
  return res;
}
void prepare() {
  fac[0] = 1;
  for (int i = 1; i <= n + m; i++)
    fac[i] = (1LL * fac[i - 1] * i) % MOD;
  inv[n + m] = fst_pow(fac[n + m], MOD - 2);
  for (int i = n + m - 1; i >= 0; i--)
    inv[i] = (1LL * inv[i + 1] * (i + 1)) % MOD;
}
int Comb(int a, int b) {
  return 1LL * (1LL * fac[a] * inv[a - b] % MOD) * inv[b] % MOD;
}
int main() {
  scanf("%d%d", &n, &m);
  if (n < m)
    swap(n, m);
  prepare();
  for (int i = 1; i <= m; i++)
    ans = (ans + 1LL * Comb(2 * i, i) * Comb(n + m - 2 * i, n - i) % MOD) % MOD;
  ans = 1LL * ans * fst_pow(Comb(m + n, n), MOD - 2) % MOD;
  ans = 1LL * ans * fst_pow(2, MOD - 2) % MOD;
  printf("%d", ans + n);
}
Problem p03622 (C++)

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

const int MN = 401234, MOD = 998244353;
int fac[MN], ivf[MN];

int tav(int a, int b) {
  if (!b)
    return 1;
  int x = tav(a, b / 2);
  return 1ll * x * x % MOD * (b % 2 ? a : 1) % MOD;
}

int C(int k, int n) { return 1ll * fac[n] * ivf[k] % MOD * ivf[n - k] % MOD; }

void pre() {
  fac[0] = 1;
  ivf[0] = 1;
  for (int i = 1; i < MN; i++) {
    fac[i] = 1ll * fac[i - 1] * i % MOD;
    ivf[i] = tav(fac[i], MOD - 2);
  }
}

int main() {
  pre();
  int n, m;
  cin >> n >> m;
  if (n > m)
    swap(n, m);
  int ans = 0;
  for (int i = 1; i <= n; i++)
    (ans += 1ll * C(i, 2 * i) * C(n - i, n + m - 2 * i) % MOD) %= MOD;
  ans = 1ll * ans * tav(C(n, n + m) * 2 % MOD, MOD - 2) % MOD;
  cout << ans + m;
}


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

const int MN = 4012345, MOD = 998244353;
int fac[MN], ivf[MN];

int tav(int a, int b) {
  if (!b)
    return 1;
  int x = tav(a, b / 2);
  return 1ll * x * x % MOD * (b % 2 ? a : 1) % MOD;
}

int C(int k, int n) { return 1ll * fac[n] * ivf[k] % MOD * ivf[n - k] % MOD; }

void pre() {
  fac[0] = 1;
  ivf[0] = 1;
  for (int i = 1; i < MN; i++) {
    fac[i] = 1ll * fac[i - 1] * i % MOD;
    ivf[i] = tav(fac[i], MOD - 2);
  }
}

int main() {
  pre();
  int n, m;
  cin >> n >> m;
  if (n > m)
    swap(n, m);
  int ans = 0;
  for (int i = 1; i <= n; i++)
    (ans += 1ll * C(i, 2 * i) * C(n - i, n + m - 2 * i) % MOD) %= MOD;
  ans = 1ll * ans * tav(C(n, n + m) * 2 % MOD, MOD - 2) % MOD;
  cout << ans + m;
}
Problem p03622 (C++)

Buggy submission (status: Time Limit Exceeded):
// Code by ajcxsu
// Problem: fft

#include <bits/stdc++.h>
#define MOD (998244353)
using namespace std;

template <typename T> inline void gn(T &x) {
  char ch = getchar(), pl = 0;
  x = 0;
  while (!isdigit(ch))
    pl = (ch == '-'), ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  x *= pl ? -1 : 1;
}

int qpow(int x, int y) {
  int ret = 1;
  while (y) {
    if (y & 1)
      ret = 1ll * ret * x % MOD;
    x = 1ll * x * x % MOD, y >>= 1;
  }
  return ret;
}

const int N = 2e6 + 10;
int fac[N], finv[N];
int C(int x, int y) { return 1ll * fac[x] * finv[y] % MOD * finv[x - y] % MOD; }

int main() {
  finv[0] = fac[0] = fac[1] = finv[1] = 1;
  for (int i = 2; i < N; i++)
    finv[i] = MOD - 1ll * MOD / i * finv[MOD % i] % MOD,
    fac[i] = 1ll * i * fac[i - 1] % MOD;
  for (int i = 2; i < N; i++)
    finv[i] = 1ll * finv[i] * finv[i - 1] % MOD;
#ifndef LOCAL
  freopen("fft.in", "r", stdin);
  freopen("fft.out", "w", stdout);
#endif
  int n, m;
  gn(n), gn(m);
  if (n < m)
    swap(n, m);
  int ans = 0;
  for (int i = 1; i <= m; i++)
    ans = (ans + 1ll * C(2 * i, i) * C(n + m - 2 * i, n - i)) % MOD;
  ans = 1ll * ans * qpow(C(n + m, m) * 2, MOD - 2) % MOD;
  (ans += n) %= MOD;
  // printf("%d\n", (n+m-ans+MOD)%MOD);
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
// Code by ajcxsu
// Problem: fft

#include <bits/stdc++.h>
#define MOD (998244353)
using namespace std;

template <typename T> inline void gn(T &x) {
  char ch = getchar(), pl = 0;
  x = 0;
  while (!isdigit(ch))
    pl = (ch == '-'), ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  x *= pl ? -1 : 1;
}

int qpow(int x, int y) {
  int ret = 1;
  while (y) {
    if (y & 1)
      ret = 1ll * ret * x % MOD;
    x = 1ll * x * x % MOD, y >>= 1;
  }
  return ret;
}

const int N = 2e6 + 10;
int fac[N], finv[N];
int C(int x, int y) { return 1ll * fac[x] * finv[y] % MOD * finv[x - y] % MOD; }

int main() {
  finv[0] = fac[0] = fac[1] = finv[1] = 1;
  for (int i = 2; i < N; i++)
    finv[i] = MOD - 1ll * MOD / i * finv[MOD % i] % MOD,
    fac[i] = 1ll * i * fac[i - 1] % MOD;
  for (int i = 2; i < N; i++)
    finv[i] = 1ll * finv[i] * finv[i - 1] % MOD;
  int n, m;
  gn(n), gn(m);
  if (n < m)
    swap(n, m);
  int ans = 0;
  for (int i = 1; i <= m; i++)
    ans = (ans + 1ll * C(2 * i, i) * C(n + m - 2 * i, n - i)) % MOD;
  ans = 1ll * ans * qpow(C(n + m, m) * 2, MOD - 2) % MOD;
  (ans += n) %= MOD;
  // printf("%d\n", (n+m-ans+MOD)%MOD);
  printf("%d\n", ans);
  return 0;
}
Problem p03622 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
using namespace std;
const long long md = 998244353;
const int maxn = 200100;
long long fac[maxn], inv[maxn];
long long powd(long long x, long long y) {
  long long res = 1;
  while (y) {
    if (y & 1)
      res = res * x % md;
    x = x * x % md;
    y >>= 1;
  }
  return res;
}
long long C(int n, int m) { return fac[n] * inv[m] % md * inv[n - m] % md; }
long long Max(long long x, long long y) { return x > y ? x : y; }
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  if (n < m)
    swap(n, m);
  fac[0] = 1;
  for (int i = 1; i < maxn; i++)
    fac[i] = fac[i - 1] * i % md;
  inv[maxn - 1] = powd(fac[maxn - 1], md - 2);
  for (int i = maxn - 1; i >= 1; i--)
    inv[i - 1] = inv[i] * i % md;
  long long ans = 0;
  for (int i = 1; i <= m; i++)
    ans = (ans + C(i * 2, i) * C(n + m - i * 2, n - i)) % md;
  ans = ans * inv[2] % md * powd(C(n + m, n), md - 2) % md;
  printf("%lld\n", (ans + n) % md);
  return 0;
}


Fixed submission:
#include <cstdio>
#include <iostream>
using namespace std;
const long long md = 998244353;
const int maxn = 1000100;
long long fac[maxn], inv[maxn];
long long powd(long long x, long long y) {
  long long res = 1;
  while (y) {
    if (y & 1)
      res = res * x % md;
    x = x * x % md;
    y >>= 1;
  }
  return res;
}
long long C(int n, int m) { return fac[n] * inv[m] % md * inv[n - m] % md; }
long long Max(long long x, long long y) { return x > y ? x : y; }
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  if (n < m)
    swap(n, m);
  fac[0] = 1;
  for (int i = 1; i < maxn; i++)
    fac[i] = fac[i - 1] * i % md;
  inv[maxn - 1] = powd(fac[maxn - 1], md - 2);
  for (int i = maxn - 1; i >= 1; i--)
    inv[i - 1] = inv[i] * i % md;
  long long ans = 0;
  for (int i = 1; i <= m; i++)
    ans = (ans + C(i * 2, i) * C(n + m - i * 2, n - i)) % md;
  ans = ans * inv[2] % md * powd(C(n + m, n), md - 2) % md;
  printf("%lld\n", (ans + n) % md);
  return 0;
}
Problem p03622 (C++)

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

typedef long long LL;
const int N = 1000005, mod = 998244353;

int n, m, factor[N], ifactor[N], ans;
void reduce(int &x) { x += x >> 31 & mod; }
int pow(int x, int y, int ans = 1) {
  for (; y; y >>= 1, x = (LL)x * x % mod)
    if (y & 1)
      ans = (LL)ans * x % mod;
  return ans;
}
void init(int n) {
  factor[0] = 1;
  for (int i = 1; i <= n; ++i)
    factor[i] = (LL)factor[i - 1] * i % mod;
  ifactor[n] = pow(factor[n], mod - 2);
  for (int i = n; i; --i)
    ifactor[i - 1] = (LL)ifactor[i] * i % mod;
}
int c(int x, int y) {
  return (LL)factor[x + y] * ifactor[x] % mod * ifactor[y] % mod;
}

int main() {
  std::ios::sync_with_stdio(0), std::cin.tie(0);
  std::cin >> n >> m, init(n + m);
  if (n < m)
    std::swap(n, m);
  for (int i = 1; i <= n; ++i)
    ans = (ans + (LL)c(i, i) * c(n - i, m - i)) % mod;
  std::cout << (n + pow(c(n, m) * 2, mod - 2, ans)) % mod << '\n';
  return 0;
}

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

typedef long long LL;
const int N = 1000005, mod = 998244353;

int n, m, factor[N], ifactor[N], ans;
void reduce(int &x) { x += x >> 31 & mod; }
int pow(int x, int y, int ans = 1) {
  for (; y; y >>= 1, x = (LL)x * x % mod)
    if (y & 1)
      ans = (LL)ans * x % mod;
  return ans;
}
void init(int n) {
  factor[0] = 1;
  for (int i = 1; i <= n; ++i)
    factor[i] = (LL)factor[i - 1] * i % mod;
  ifactor[n] = pow(factor[n], mod - 2);
  for (int i = n; i; --i)
    ifactor[i - 1] = (LL)ifactor[i] * i % mod;
}
int c(int x, int y) {
  return (LL)factor[x + y] * ifactor[x] % mod * ifactor[y] % mod;
}

int main() {
  std::ios::sync_with_stdio(0), std::cin.tie(0);
  std::cin >> n >> m, init(n + m);
  if (n < m)
    std::swap(n, m);
  for (int i = 1; i <= m; ++i)
    ans = (ans + (LL)c(i, i) * c(n - i, m - i)) % mod;
  std::cout << (n + pow(c(n, m) * 2, mod - 2, ans)) % mod << '\n';
  return 0;
}
Problem p03622 (C++)

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

using namespace std;

const int Mod = 998244353;
const int maxn = 5e5 + 10;

void Chkadd(int &x, int y) {
  if ((x += y) >= Mod)
    x -= Mod;
}

int Power(int a, int k) {
  int x = 1;
  for (; k; k >>= 1) {
    if (k & 1)
      x = 1ll * x * a % Mod;
    a = 1ll * a * a % Mod;
  }
  return x;
}

int fac[maxn], inv[maxn];

int Count(int n, int m) {
  if (n < 0 || m < 0)
    return 0;
  return 1ll * fac[n + m] * inv[n] % Mod * inv[m] % Mod;
}

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  if (n < m)
    swap(n, m);
  int tot(n + m);
  fac[0] = 1;
  for (int i = 1; i <= tot; ++i)
    fac[i] = 1ll * fac[i - 1] * i % Mod;
  inv[tot] = Power(fac[tot], Mod - 2);
  for (int i = tot; i; --i)
    inv[i - 1] = 1ll * inv[i] * i % Mod;
  int ans = 0;
  for (int i = 1; i <= m; ++i)
    Chkadd(ans, 1ll * Count(i, i) * Count(n - i, m - i) % Mod);
  ans = 1ll * ans * Power(2 * Count(n, m), Mod - 2) % Mod;
  printf("%d\n", (ans + n) % Mod);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>

using namespace std;

const int Mod = 998244353;
const int maxn = 1e6 + 10;

void Chkadd(int &x, int y) {
  if ((x += y) >= Mod)
    x -= Mod;
}

int Power(int a, int k) {
  int x = 1;
  for (; k; k >>= 1) {
    if (k & 1)
      x = 1ll * x * a % Mod;
    a = 1ll * a * a % Mod;
  }
  return x;
}

int fac[maxn], inv[maxn];

int Count(int n, int m) {
  if (n < 0 || m < 0)
    return 0;
  return 1ll * fac[n + m] * inv[n] % Mod * inv[m] % Mod;
}

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  if (n < m)
    swap(n, m);
  int tot(n + m);
  fac[0] = 1;
  for (int i = 1; i <= tot; ++i)
    fac[i] = 1ll * fac[i - 1] * i % Mod;
  inv[tot] = Power(fac[tot], Mod - 2);
  for (int i = tot; i; --i)
    inv[i - 1] = 1ll * inv[i] * i % Mod;
  int ans = 0;
  for (int i = 1; i <= m; ++i)
    Chkadd(ans, 1ll * Count(i, i) * Count(n - i, m - i) % Mod);
  ans = 1ll * ans * Power(2 * Count(n, m), Mod - 2) % Mod;
  printf("%d\n", (ans + n) % Mod);
  return 0;
}
Problem p03622 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#define fo(i, a, b) for (ll i = a; i <= b; i++)
#define N 501000
#define mo 998244353
#define ll long long
using namespace std;
ll n, m, ans = 0, jc[N], ny[N];
ll mi(ll a, ll b) {
  ll c = 1;
  for (; b; b /= 2, a = (a * a) % mo)
    if (b % 2 == 1)
      c = (c * a) % mo;
  return c;
}
ll C(ll m, ll n) { return jc[m] * ny[n] % mo * ny[m - n] % mo; }
int main() {
  scanf("%lld%lld", &n, &m);
  if (n > m)
    swap(n, m);
  jc[0] = ny[0] = 1;
  fo(i, 1, n + m) jc[i] = jc[i - 1] * i % mo, ny[i] = mi(jc[i], mo - 2);
  fo(i, 1, n) ans = (ans + C(i + i, i) * C(n - i + m - i, n - i)) % mo;
  ans = ans * mi(2 * C(n + m, n) % mo, mo - 2) % mo;
  ans = (ans + m) % mo;
  printf("%lld\n", ans);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#define fo(i, a, b) for (ll i = a; i <= b; i++)
#define N 1001000
#define mo 998244353
#define ll long long
using namespace std;
ll n, m, ans = 0, jc[N], ny[N];
ll mi(ll a, ll b) {
  ll c = 1;
  for (; b; b /= 2, a = (a * a) % mo)
    if (b % 2 == 1)
      c = (c * a) % mo;
  return c;
}
ll C(ll m, ll n) { return jc[m] * ny[n] % mo * ny[m - n] % mo; }
int main() {
  scanf("%lld%lld", &n, &m);
  if (n > m)
    swap(n, m);
  jc[0] = ny[0] = 1;
  fo(i, 1, n + m) jc[i] = jc[i - 1] * i % mo, ny[i] = mi(jc[i], mo - 2);
  fo(i, 1, n) ans = (ans + C(i + i, i) * C(n - i + m - i, n - i)) % mo;
  ans = ans * mi(2 * C(n + m, n) % mo, mo - 2) % mo;
  ans = (ans + m) % mo;
  printf("%lld\n", ans);
}
Problem p03622 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
using namespace std;
#define LL long long
#define mod 998244353
const int NN = 1000000 + 117;
LL fact[NN] = {};
LL rev[NN] = {};
LL comp(int m, int n) { return fact[n] * rev[m] % mod * rev[n - m] % mod; }
LL ksm(LL a, LL b) {
  LL ret = 1;
  while (b) {
    if (b & 1) {
      (ret *= a) %= mod;
    }
    (a *= a) %= mod;
    b >>= 1;
  }
  return ret;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  int top = n + m;
  fact[0] = 1;
  for (int i = 1; i <= top; ++i)
    fact[i] = fact[i - 1] * i % mod;
  rev[top] = ksm(fact[top], mod - 2);
  for (int i = top; i >= 1; --i)
    rev[i - 1] = rev[i] * i % mod;
  if (n < m)
    m ^= n ^= m ^= n;
  LL ans = 0;
  for (int i = 1; i <= m; ++i) {
    ans += comp(i, 2 * i) * comp(n + m - 2 * i, m - i) % mod;
    ans %= mod;
  }
  ans = ans * rev[2] % mod * rev[m + n] % mod * fact[n] % mod * fact[m] % mod;
  ans += n;
  ans %= mod;
  printf("%lld\n", ans);
}

Fixed submission:
#include <cstdio>
using namespace std;
#define LL long long
#define mod 998244353
const int NN = 1000000 + 117;
LL fact[NN] = {};
LL rev[NN] = {};
LL comp(int m, int n) { return fact[n] * rev[m] % mod * rev[n - m] % mod; }
LL ksm(LL a, LL b) {
  LL ret = 1;
  while (b) {
    if (b & 1) {
      (ret *= a) %= mod;
    }
    (a *= a) %= mod;
    b >>= 1;
  }
  return ret;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  int top = n + m;
  fact[0] = 1;
  for (int i = 1; i <= top; ++i)
    fact[i] = fact[i - 1] * i % mod;
  rev[top] = ksm(fact[top], mod - 2);
  for (int i = top; i >= 1; --i)
    rev[i - 1] = rev[i] * i % mod;
  if (n < m)
    m ^= n ^= m ^= n;
  LL ans = 0;
  for (int i = 1; i <= m; ++i) {
    ans += comp(i, 2 * i) * comp(m - i, n + m - 2 * i) % mod;
    ans %= mod;
  }
  ans = ans * rev[2] % mod * rev[m + n] % mod * fact[n] % mod * fact[m] % mod;
  ans += n;
  ans %= mod;
  printf("%lld\n", ans);
}
Problem p03622 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define mod 998244353
typedef long long ll;
ll gi() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch))
    f ^= ch == '-', ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  return f ? x : -x;
}
int pow(int x, int y) {
  int ret = 1;
  while (y) {
    if (y & 1)
      ret = 1ll * ret * x % mod;
    x = 1ll * x * x % mod;
    y >>= 1;
  }
  return ret;
}
int ans, fact[1000010], ifact[1000010];
int C(int n, int m) {
  return 1ll * fact[n] * ifact[m] % mod * ifact[n - m] % mod;
}
int main() {
  int T = gi(), n = gi(), m = gi();
  fact[0] = 1;
  for (int i = 1; i <= n + m; ++i)
    fact[i] = 1ll * fact[i - 1] * i % mod;
  ifact[n + m] = pow(fact[n + m], mod - 2);
  for (int i = n + m; i; --i)
    ifact[i - 1] = 1ll * ifact[i] * i % mod;
  if (n < m)
    std::swap(n, m);
  int ans = 0;
  for (int i = 1; i <= m; ++i)
    ans = (ans + 1ll * C(n + m - i - i, n - i) * C(i + i, i)) % mod;
  printf("%d\n", (1ll * ans * ifact[2] % mod * ifact[n + m] % mod * fact[n] %
                      mod * fact[m] +
                  n) %
                     mod);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define mod 998244353
typedef long long ll;
ll gi() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch))
    f ^= ch == '-', ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  return f ? x : -x;
}
int pow(int x, int y) {
  int ret = 1;
  while (y) {
    if (y & 1)
      ret = 1ll * ret * x % mod;
    x = 1ll * x * x % mod;
    y >>= 1;
  }
  return ret;
}
int ans, fact[1000010], ifact[1000010];
int C(int n, int m) {
  return 1ll * fact[n] * ifact[m] % mod * ifact[n - m] % mod;
}
int main() {
  int n = gi(), m = gi();
  fact[0] = 1;
  for (int i = 1; i <= n + m; ++i)
    fact[i] = 1ll * fact[i - 1] * i % mod;
  ifact[n + m] = pow(fact[n + m], mod - 2);
  for (int i = n + m; i; --i)
    ifact[i - 1] = 1ll * ifact[i] * i % mod;
  if (n < m)
    std::swap(n, m);
  int ans = 0;
  for (int i = 1; i <= m; ++i)
    ans = (ans + 1ll * C(n + m - i - i, n - i) * C(i + i, i)) % mod;
  printf("%d\n", (1ll * ans * ifact[2] % mod * ifact[n + m] % mod * fact[n] %
                      mod * fact[m] +
                  n) %
                     mod);
  return 0;
}
Problem p03622 (C++)

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

typedef long long LL;
const int N = 5e5 + 50;
const int Mod = 998244353;
int n, m, ans, fac[N], inv[N];

inline void swap(int &a, int &b) {
  int t = a;
  a = b, b = t;
}
template <class T> inline void in(T &x) {
  x = 0;
  int f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9';) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  for (; ch >= '0' && ch <= '9';)
    x = x * 10 + ch - '0', ch = getchar();
  x *= f;
}

inline int qpow(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;
}

inline int C(int n, int m) {
  if (n < m || m < 0)
    return 0;
  return 1LL * fac[n] * inv[m] % Mod * inv[n - m] % Mod;
}

int main() {
  in(n), in(m);
  if (n < m)
    swap(n, m);
  fac[0] = 1;
  for (int i = 1; i <= n * 2; ++i)
    fac[i] = 1LL * fac[i - 1] * i % Mod;
  inv[n << 1] = qpow(fac[n * 2], Mod - 2);
  for (int i = n * 2 - 1; ~i; --i)
    inv[i] = 1LL * inv[i + 1] * (i + 1) % Mod;
  for (int i = 1; i <= m; ++i)
    ans = (ans + 1LL * C(i << 1, i) * C(n + m - 2 * i, n - i) % Mod) % Mod;
  ans = 1LL * ans * qpow(C(n + m, n) * 2 % Mod, Mod - 2) % Mod;
  ans = (ans + n) % Mod;
  printf("%d\n", (ans + Mod) % Mod);
  return 0;
}

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

typedef long long LL;
const int N = 1e6 + 50;
const int Mod = 998244353;
int n, m, ans, fac[N], inv[N];

inline void swap(int &a, int &b) {
  int t = a;
  a = b, b = t;
}
template <class T> inline void in(T &x) {
  x = 0;
  int f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9';) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  for (; ch >= '0' && ch <= '9';)
    x = x * 10 + ch - '0', ch = getchar();
  x *= f;
}

inline int qpow(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;
}

inline int C(int n, int m) {
  if (n < m || m < 0)
    return 0;
  return 1LL * fac[n] * inv[m] % Mod * inv[n - m] % Mod;
}

int main() {
  in(n), in(m);
  if (n < m)
    swap(n, m);
  fac[0] = 1;
  for (int i = 1; i <= n * 2; ++i)
    fac[i] = 1LL * fac[i - 1] * i % Mod;
  inv[n << 1] = qpow(fac[n * 2], Mod - 2);
  for (int i = n * 2 - 1; ~i; --i)
    inv[i] = 1LL * inv[i + 1] * (i + 1) % Mod;
  for (int i = 1; i <= m; ++i)
    ans = (ans + 1LL * C(i << 1, i) * C(n + m - 2 * i, n - i) % Mod) % Mod;
  ans = 1LL * ans * qpow(C(n + m, n) * 2 % Mod, Mod - 2) % Mod;
  ans = (ans + n) % Mod;
  printf("%d\n", (ans + Mod) % Mod);
  return 0;
}
Problem p03622 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define int long long
const int inf = 0x3f3f3f3f, mod = 998244353, N = 500005;
int fac[N], inv[N];

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-')
      f = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = (x << 1) + (x << 3) + ch - '0';
  return x * f;
}

int power(int x, int t) {
  int res = 1;
  while (t) {
    if (t & 1)
      (res *= x) %= mod;
    (x *= x) %= mod;
    t >>= 1;
  }
  return res;
}

int C(const int &n, const int &m) {
  return (fac[n] * inv[m] % mod) * inv[n - m] % mod;
}
int invC(const int &n, const int &m) {
  return (inv[n] * fac[m] % mod) * fac[n - m] % mod;
}

signed main() {
  int n = read(), m = read(), k = min(n, m), ans = 0;
  fac[0] = 1;
  for (int i = 1; i <= N; i++)
    fac[i] = (fac[i - 1] * i) % mod;
  inv[N] = power(fac[N], mod - 2);
  for (int i = N - 1; i >= 0; i--)
    inv[i] = (inv[i + 1] * (i + 1)) % mod;
  for (int i = 1; i <= k; i++)
    (ans += C(2 * i, i) * C(n + m - 2 * i, n - i)) %= mod;
  (ans *= (invC(n + m, n) * inv[2] % mod)) %= mod;
  printf("%d\n", (ans + max(n, m)) % mod);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define int long long
const int inf = 0x3f3f3f3f, mod = 998244353, N = 1000005;
int fac[N], inv[N];

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-')
      f = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = (x << 1) + (x << 3) + ch - '0';
  return x * f;
}

int power(int x, int t) {
  int res = 1;
  while (t) {
    if (t & 1)
      (res *= x) %= mod;
    (x *= x) %= mod;
    t >>= 1;
  }
  return res;
}

int C(const int &n, const int &m) {
  return (fac[n] * inv[m] % mod) * inv[n - m] % mod;
}
int invC(const int &n, const int &m) {
  return (inv[n] * fac[m] % mod) * fac[n - m] % mod;
}

signed main() {
  int n = read(), m = read(), k = min(n, m), ans = 0;
  fac[0] = 1;
  for (int i = 1; i <= N; i++)
    fac[i] = (fac[i - 1] * i) % mod;
  inv[N] = power(fac[N], mod - 2);
  for (int i = N - 1; i >= 0; i--)
    inv[i] = (inv[i + 1] * (i + 1)) % mod;
  for (int i = 1; i <= k; i++)
    (ans += C(2 * i, i) * C(n + m - 2 * i, n - i)) %= mod;
  (ans *= (invC(n + m, n) * inv[2] % mod)) %= mod;
  printf("%d\n", (ans + max(n, m)) % mod);
  return 0;
}
Problem p03622 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mod 998244353
#define maxn 500005
using namespace std;
typedef long long LL;
int read() {
  char c = getchar();
  int f = 1, sum = 0;
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  return sum * f;
}
int n, m;
LL fac[maxn], inv[maxn], ans;
LL ksm(LL a, LL b) {
  LL ret = 1;
  while (b) {
    if (b & 1)
      ret = ret * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ret;
}
void init(int x) {
  fac[0] = 1;
  for (int i = 1; i <= x; i++)
    fac[i] = fac[i - 1] * i % mod;
  inv[x] = ksm(fac[x], mod - 2);
  for (int i = x; i >= 1; i--)
    inv[i - 1] = inv[i] * i % mod;
}
LL calc(LL x, LL y) { return fac[x + y] * inv[x] % mod * inv[y] % mod; }
int main() {
  n = read();
  m = read();
  if (n > m)
    swap(n, m);
  init(n + m);
  for (int i = 1; i <= n; i++)
    ans += calc(i, i) * calc(n - i, m - i) % mod;
  ans = ans % mod * ksm(calc(n, m) << 1, mod - 2) % mod;
  printf("%lld\n", (ans + m) % mod);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define mod 998244353
#define maxn 1000005
using namespace std;
typedef long long LL;
int read() {
  char c = getchar();
  int f = 1, sum = 0;
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  return sum * f;
}
int n, m;
LL fac[maxn], inv[maxn], ans;
LL ksm(LL a, LL b) {
  LL ret = 1;
  while (b) {
    if (b & 1)
      ret = ret * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ret;
}
void init(int x) {
  fac[0] = 1;
  for (int i = 1; i <= x; i++)
    fac[i] = fac[i - 1] * i % mod;
  inv[x] = ksm(fac[x], mod - 2);
  for (int i = x; i >= 1; i--)
    inv[i - 1] = inv[i] * i % mod;
}
LL calc(LL x, LL y) { return fac[x + y] * inv[x] % mod * inv[y] % mod; }
int main() {
  n = read();
  m = read();
  if (n > m)
    swap(n, m);
  init(n + m);
  for (int i = 1; i <= n; i++)
    ans += calc(i, i) * calc(n - i, m - i) % mod;
  ans = ans % mod * ksm(calc(n, m) << 1, mod - 2) % mod;
  printf("%lld\n", (ans + m) % mod);
  return 0;
}
Problem p03622 (C++)

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

const int N = 500 * 1000 + 5, mod = 998244353;

long long fac[N];

int po(int x, int y) {
  if (y == 0) {
    return 1;
  }
  long long ans = po(x, y / 2);
  ans *= ans;
  ans %= mod;
  if (y % 2) {
    ans *= x;
    ans %= mod;
  }
  return ans;
}

int chs(int x, int y) {
  if (y > x) {
    return 0;
  }
  long long ans = 1ll * fac[x] * po(fac[x - y], mod - 2);
  ans %= mod;
  ans *= po(fac[y], mod - 2);
  ans %= mod;
  return ans;
}

int main() {
  fac[0] = 1;
  for (int i = 1; i < N; i++) {
    fac[i] = fac[i - 1] * i;
    fac[i] %= mod;
  }
  int n, m;
  cin >> n >> m;
  long long ans = 0;
  for (int i = 1; i <= n && i <= m; i++) {
    long long x = 1ll * chs(i * 2, i) * chs(n + m - 2 * i, n - i);
    x %= mod;
    ans += x;
  }
  ans %= mod;
  ans *= po(chs(n + m, n) * 2, mod - 2);
  ans %= mod;
  ans += max(n, m);
  ans %= mod;
  cout << ans;

  return 0;
}


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

const int N = 1000 * 1000 + 5, mod = 998244353;

long long fac[N];

int po(int x, int y) {
  if (y == 0) {
    return 1;
  }
  long long ans = po(x, y / 2);
  ans *= ans;
  ans %= mod;
  if (y % 2) {
    ans *= x;
    ans %= mod;
  }
  return ans;
}

int chs(int x, int y) {
  if (y > x) {
    return 0;
  }
  long long ans = 1ll * fac[x] * po(fac[x - y], mod - 2);
  ans %= mod;
  ans *= po(fac[y], mod - 2);
  ans %= mod;
  return ans;
}

int main() {
  fac[0] = 1;
  for (int i = 1; i < N; i++) {
    fac[i] = fac[i - 1] * i;
    fac[i] %= mod;
  }
  int n, m;
  cin >> n >> m;
  long long ans = 0;
  for (int i = 1; i <= n && i <= m; i++) {
    long long x = 1ll * chs(i * 2, i) * chs(n + m - 2 * i, n - i);
    x %= mod;
    ans += x;
  }
  ans %= mod;
  ans *= po(chs(n + m, n) * 2, mod - 2);
  ans %= mod;
  ans += max(n, m);
  ans %= mod;
  cout << ans;

  return 0;
}
Problem p03622 (C++)

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

using namespace std;
typedef long long LL;

#define re register

const int N = 500005;
const LL mod = 998244353;

int n, m;
LL fac[N], ifac[N];

LL Pow(LL x, int y) {
  LL res = 1;
  x %= mod;
  while (y) {
    if (y & 1)
      res *= x, res %= mod;
    x = x * x % mod;
    y >>= 1;
  }
  return res;
}

void Prepare() {
  fac[0] = 1;
  for (re int i = 1; i <= N; i++)
    fac[i] = fac[i - 1] * i % mod;

  ifac[N] = Pow(fac[N], mod - 2);
  for (re int i = N - 1; i >= 0; i--)
    ifac[i] = ifac[i + 1] * (i + 1) % mod;
}
inline LL C(int x, int y) { return fac[x] * ifac[y] % mod * ifac[x - y] % mod; }
inline LL Cinv(int x, int y) {
  return ifac[x] * fac[y] % mod * fac[x - y] % mod;
}

int main() {
  Prepare();

  scanf("%d %d", &n, &m);

  if (n < m)
    swap(n, m);
  LL ans = 0;
  for (re int i = 1; i <= m; i++)
    ans = (ans +
           C(m + n - i * 2, n - i) * C(2 * i, i) % mod * Cinv(m + n, n) % mod) %
          mod;

  ans = ans * ifac[2] % mod;
  printf("%lld\n", (ans + n) % mod);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <queue>

using namespace std;
typedef long long LL;

#define re register

const int N = 1000005;
const LL mod = 998244353;

int n, m;
LL fac[N], ifac[N];

LL Pow(LL x, int y) {
  LL res = 1;
  x %= mod;
  while (y) {
    if (y & 1)
      res *= x, res %= mod;
    x = x * x % mod;
    y >>= 1;
  }
  return res;
}

void Prepare() {
  fac[0] = 1;
  for (re int i = 1; i <= N; i++)
    fac[i] = fac[i - 1] * i % mod;

  ifac[N] = Pow(fac[N], mod - 2);
  for (re int i = N - 1; i >= 0; i--)
    ifac[i] = ifac[i + 1] * (i + 1) % mod;
}
inline LL C(int x, int y) { return fac[x] * ifac[y] % mod * ifac[x - y] % mod; }
inline LL Cinv(int x, int y) {
  return ifac[x] * fac[y] % mod * fac[x - y] % mod;
}

int main() {
  Prepare();

  scanf("%d %d", &n, &m);

  if (n < m)
    swap(n, m);
  LL ans = 0;
  for (re int i = 1; i <= m; i++)
    ans = (ans +
           C(m + n - i * 2, n - i) * C(2 * i, i) % mod * Cinv(m + n, n) % mod) %
          mod;

  ans = ans * ifac[2] % mod;
  printf("%lld\n", (ans + n) % mod);
  return 0;
}