Problem p03083 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define LL long long
#define mp(x, y) make_pair(x, y)
#define pll pair<long long, long long>
#define pii pair<int, int>
using namespace std;
inline int read() {
  int f = 1, x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int stack[20];
inline void write(int x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (!x) {
    putchar('0');
    return;
  }
  int top = 0;
  while (x)
    stack[++top] = x % 10, x /= 10;
  while (top)
    putchar(stack[top--] + '0');
}
inline void pr1(int x) {
  write(x);
  putchar(' ');
}
inline void pr2(int x) {
  write(x);
  putchar('\n');
}
const int MAXN = 100005;
const int mod = 1e9 + 7;
int pow_mod(int a, int b) {
  int ret = 1;
  while (b) {
    if (b & 1)
      ret = 1LL * ret * a % mod;
    a = 1LL * a * a % mod;
    b >>= 1;
  }
  return ret;
}
int pre[MAXN], inv[MAXN], bin[MAXN], invb[MAXN];
int C(int n, int m) { return 1LL * pre[n] * inv[m] % mod * inv[n - m] % mod; }
int B, W;
int ans;
void ad(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
}
void dl(int &x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
}
int main() {
  bin[0] = 1;
  for (int i = 1; i < MAXN; i++)
    bin[i] = 1LL * bin[i - 1] * 2 % mod;
  invb[MAXN - 1] = pow_mod(bin[MAXN - 1], mod - 2);
  for (int i = MAXN - 2; i >= 0; i--)
    invb[i] = 1LL * invb[i + 1] * 2 % mod;
  pre[0] = 1;
  for (int i = 1; i < MAXN; i++)
    pre[i] = 1LL * pre[i - 1] * i % mod;
  inv[MAXN - 1] = pow_mod(pre[MAXN - 1], mod - 2);
  for (int i = MAXN - 2; i >= 0; i--)
    inv[i] = 1LL * inv[i + 1] * (i + 1) % mod;
  B = read();
  W = read();
  if (B == 1) {
    for (int i = 1; i < B + W; i++)
      pr2(pow_mod(pow_mod(2, i), mod - 2));
    pr2(pow_mod(pow_mod(2, W), mod - 2));
    return 0;
  }
  if (W == 1) {
    for (int i = 1; i < B + W; i++)
      pr2((1 - pow_mod(pow_mod(2, i), mod - 2) + mod) % mod);
    pr2((1 - pow_mod(pow_mod(2, B), mod - 2) + mod) % mod);
    return 0;
  }
  for (int i = 1; i <= W; i++)
    ad(ans, 1LL * C(B + W - i - 2, W - i) * invb[B + W - i] % mod);
  for (int i = 1; i < B; i++)
    ad(ans, 1LL * C(B + W - i - 2, W - 1) * invb[B + W - i] % mod);
  pr2(ans);
  for (int i = 2; i <= B + W; i++) {
    int ps = B + W - i + 1;
    if (ps <= W + 1) {
      dl(ans, 1LL * C(i - 2, W - ps) * invb[i - 1] % mod);
      if (ps - 1)
        dl(ans, 1LL * C(B + W - (ps - 1) - 2, W - (ps - 1)) *
                    invb[B + W - (ps - 1)] % mod);
      if (ps - 1)
        ad(ans, 1LL * C(i - 1, W - ps + 1) * invb[i] % mod);
    }
    if (ps == B) {
      dl(ans,
         1LL * C(B + W - (B - 1) - 2, W - 1) * invb[B + W - (B - 1)] % mod);
      ad(ans, invb[i - 1]);
    }
    if (ps < B) {
      if (ps - 1)
        dl(ans,
           1LL * C(B + W - (ps - 1) - 2, W - 1) * invb[B + W - (ps - 1)] % mod);
      ad(ans, 1LL * invb[i - 1] * C(i - 2, W - 1) % mod);
    }
    pr2(ans);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define LL long long
#define mp(x, y) make_pair(x, y)
#define pll pair<long long, long long>
#define pii pair<int, int>
using namespace std;
inline int read() {
  int f = 1, x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int stack[20];
inline void write(int x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (!x) {
    putchar('0');
    return;
  }
  int top = 0;
  while (x)
    stack[++top] = x % 10, x /= 10;
  while (top)
    putchar(stack[top--] + '0');
}
inline void pr1(int x) {
  write(x);
  putchar(' ');
}
inline void pr2(int x) {
  write(x);
  putchar('\n');
}
const int MAXN = 200005;
const int mod = 1e9 + 7;
int pow_mod(int a, int b) {
  int ret = 1;
  while (b) {
    if (b & 1)
      ret = 1LL * ret * a % mod;
    a = 1LL * a * a % mod;
    b >>= 1;
  }
  return ret;
}
int pre[MAXN], inv[MAXN], bin[MAXN], invb[MAXN];
int C(int n, int m) { return 1LL * pre[n] * inv[m] % mod * inv[n - m] % mod; }
int B, W;
int ans;
void ad(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
}
void dl(int &x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
}
int main() {
  bin[0] = 1;
  for (int i = 1; i < MAXN; i++)
    bin[i] = 1LL * bin[i - 1] * 2 % mod;
  invb[MAXN - 1] = pow_mod(bin[MAXN - 1], mod - 2);
  for (int i = MAXN - 2; i >= 0; i--)
    invb[i] = 1LL * invb[i + 1] * 2 % mod;
  pre[0] = 1;
  for (int i = 1; i < MAXN; i++)
    pre[i] = 1LL * pre[i - 1] * i % mod;
  inv[MAXN - 1] = pow_mod(pre[MAXN - 1], mod - 2);
  for (int i = MAXN - 2; i >= 0; i--)
    inv[i] = 1LL * inv[i + 1] * (i + 1) % mod;
  B = read();
  W = read();
  if (B == 1) {
    for (int i = 1; i < B + W; i++)
      pr2(pow_mod(pow_mod(2, i), mod - 2));
    pr2(pow_mod(pow_mod(2, W), mod - 2));
    return 0;
  }
  if (W == 1) {
    for (int i = 1; i < B + W; i++)
      pr2((1 - pow_mod(pow_mod(2, i), mod - 2) + mod) % mod);
    pr2((1 - pow_mod(pow_mod(2, B), mod - 2) + mod) % mod);
    return 0;
  }
  for (int i = 1; i <= W; i++)
    ad(ans, 1LL * C(B + W - i - 2, W - i) * invb[B + W - i] % mod);
  for (int i = 1; i < B; i++)
    ad(ans, 1LL * C(B + W - i - 2, W - 1) * invb[B + W - i] % mod);
  pr2(ans);
  for (int i = 2; i <= B + W; i++) {
    int ps = B + W - i + 1;
    if (ps <= W + 1) {
      dl(ans, 1LL * C(i - 2, W - ps) * invb[i - 1] % mod);
      if (ps - 1)
        dl(ans, 1LL * C(B + W - (ps - 1) - 2, W - (ps - 1)) *
                    invb[B + W - (ps - 1)] % mod);
      if (ps - 1)
        ad(ans, 1LL * C(i - 1, W - ps + 1) * invb[i] % mod);
    }
    if (ps == B) {
      dl(ans,
         1LL * C(B + W - (B - 1) - 2, W - 1) * invb[B + W - (B - 1)] % mod);
      ad(ans, invb[i - 1]);
    }
    if (ps < B) {
      if (ps - 1)
        dl(ans,
           1LL * C(B + W - (ps - 1) - 2, W - 1) * invb[B + W - (ps - 1)] % mod);
      ad(ans, 1LL * invb[i - 1] * C(i - 2, W - 1) % mod);
    }
    pr2(ans);
  }
  return 0;
}