Problem p03466 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, b) for (int i = (a); i <= (b); ++i)
#define REPD(i, a, b) for (int i = (a); i >= (b); --i)
#define FORI(i, n) REP(i, 1, n)
#define FOR(i, n) REP(i, 0, int(n) - 1)
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define ll long long
#define SZ(x) int((x).size())
#define DBG(v) cerr << #v << " = " << (v) << endl;
#define FOREACH(i, t) for (typeof(t.begin()) i = t.begin(); i != t.end(); i++)
#define fi first
#define se second

void test() {
  int a, b, c, d;
  scanf("%d%d%d%d", &a, &b, &c, &d);
  int m = a < b ? (a + b) / (a + 1) : (a + b) / (b + 1);
  int pl = 0, ph = min((a + m - 1) / m, b) + 1;
  while (pl + 1 < ph) {
    int pm = (pl + ph) / 2;
    if (a - pm * m >= (b - pm + m - 1) / m)
      pl = pm;
    else
      ph = pm;
  }
  int p = pl, q = (b - p - 1) / m, w = a - p * m - q, u = b - p - q * m;
  // printf("m=%d, p=%d, q=%d, w=%d, u=%d\n",m,p,q,w,u);
  assert(w >= 0);
  assert(w <= m);
  assert(u >= 0);
  assert(u <= m);

  c--;
  d -= c;
  int pp = min(p, c / (m + 1));
  c -= pp * (m + 1);
  p -= pp;
  while (p > 0) {
    if (c + d <= m) {
      for (int i = 0; i < d; i++)
        printf("A");
      printf("\n");
      return;
    }
    for (int i = c; i < m; i++) {
      printf("A");
      d--;
    }
    printf("B");
    d--;
    c = 0;
    p--;
  }
  int ww = min(w, c);
  w -= ww;
  c -= ww;
  while (w > 0 && d > 0) {
    printf("A");
    w--;
    d--;
  }
  int uu = min(u, c);
  u -= uu;
  c -= uu;
  while (u > 0 && d > 0) {
    printf("B");
    u--;
    d--;
  }
  int qq = min(q, c / (m + 1));
  c -= qq * (m + 1);
  q -= qq;
  while (q > 0) {
    if (c + d <= m + 1) {
      if (c == 0 && d > 0) {
        printf("A");
        d--;
      }
      for (int i = 0; i < d; i++)
        printf("B");
      printf("\n");
      return;
    }
    if (c == 0 && d > 0) {
      printf("A");
      d--;
    }
    for (int i = max(c, 1); i < m + 1; i++) {
      printf("B");
      d--;
    }
    c = 0;
    q--;
  }
  printf("\n");
}

int main() {
  int ttn;
  scanf("%d", &ttn);
  while (ttn--)
    test();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, b) for (int i = (a); i <= (b); ++i)
#define REPD(i, a, b) for (int i = (a); i >= (b); --i)
#define FORI(i, n) REP(i, 1, n)
#define FOR(i, n) REP(i, 0, int(n) - 1)
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define ll long long
#define SZ(x) int((x).size())
#define DBG(v) cerr << #v << " = " << (v) << endl;
#define FOREACH(i, t) for (typeof(t.begin()) i = t.begin(); i != t.end(); i++)
#define fi first
#define se second

void test() {
  int a, b, c, d;
  scanf("%d%d%d%d", &a, &b, &c, &d);
  int m = a < b ? (a + b) / (a + 1) : (a + b) / (b + 1);
  int pl = 0, ph = min((a + m - 1) / m, b) + 1;
  while (pl + 1 < ph) {
    int pm = (pl + ph) / 2;
    if (a - pm * m >= (b - pm + m - 1) / m)
      pl = pm;
    else
      ph = pm;
  }
  int p = pl, q = max(0, (b - p) / m - ((b - p) % m ? 0 : 1)),
      w = a - p * m - q, u = b - p - q * m;
  // printf("m=%d, p=%d, q=%d, w=%d, u=%d\n",m,p,q,w,u);
  assert(w >= 0);
  assert(w <= m);
  assert(u >= 0);
  assert(u <= m);

  c--;
  d -= c;
  int pp = min(p, c / (m + 1));
  c -= pp * (m + 1);
  p -= pp;
  while (p > 0) {
    if (c + d <= m) {
      for (int i = 0; i < d; i++)
        printf("A");
      printf("\n");
      return;
    }
    for (int i = c; i < m; i++) {
      printf("A");
      d--;
    }
    printf("B");
    d--;
    c = 0;
    p--;
  }
  int ww = min(w, c);
  w -= ww;
  c -= ww;
  while (w > 0 && d > 0) {
    printf("A");
    w--;
    d--;
  }
  int uu = min(u, c);
  u -= uu;
  c -= uu;
  while (u > 0 && d > 0) {
    printf("B");
    u--;
    d--;
  }
  int qq = min(q, c / (m + 1));
  c -= qq * (m + 1);
  q -= qq;
  while (q > 0) {
    if (c + d <= m + 1) {
      if (c == 0 && d > 0) {
        printf("A");
        d--;
      }
      for (int i = 0; i < d; i++)
        printf("B");
      printf("\n");
      return;
    }
    if (c == 0 && d > 0) {
      printf("A");
      d--;
    }
    for (int i = max(c, 1); i < m + 1; i++) {
      printf("B");
      d--;
    }
    c = 0;
    q--;
  }
  printf("\n");
}

int main() {
  int ttn;
  scanf("%d", &ttn);
  while (ttn--)
    test();
  return 0;
}