Problem p02903 (C++)

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

using namespace std;

using ll = long long;

bool judge(vector<string> &ans, int A, int B) {
  int H = ans.size();
  int W = ans[0].size();
  for (int i = 0; i < H; i++) {
    int cnt = 0;
    for (int j = 0; j < W; j++) {
      cnt += ans[i][j] - '0';
    }
    cnt = min(cnt, W - cnt);
    if (cnt != A)
      return false;
  }

  for (int j = 0; j < W; j++) {
    int cnt = 0;
    for (int i = 0; i < H; i++) {
      cnt += ans[i][j] - '0';
    }
    cnt = min(cnt, H - cnt);
    if (cnt != B)
      return false;
  }
  return true;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int H, W, A, B;
  cin >> H >> W >> A >> B;
  vector<string> ans(H, string(W, '0'));
  if (H == 2 * B) {
    if (B * W == A * H) {
      string s1 = string(A, '1') + string(A, '0');
      string s2 = string(A, '0') + string(A, '1');
      for (int i = 0; i < B; i++) {
        ans[i] = s1;
      }

      for (int i = B; i < H; i++) {
        ans[i] = s2;
      }
    } else {
      cout << "No\n";
      return 0;
    }
  } else {
    int hoge = H - 2 * B;
    int bar = -1;
    int ai = -1;
    for (int i = 0; i <= H; i++) {
      int anum = A * i + (W - A) * (H - i);
      if (anum < B * W)
        continue;
      int foo = anum - B * W;
      if (foo % hoge != 0)
        continue;
      int next_bar = W - foo / hoge;
      if (next_bar <= 0 || next_bar > W)
        continue;
      bar = next_bar;
      ai = i;
      break;
    }

    if (bar == -1) {
      cout << "No\n";
      return 0;
    }

    // cerr << bar << " " << ai << endl;
    vector<int> rest(H, W - A);
    for (int i = 0; i < ai; i++) {
      rest[i] = A;
    }

    for (int j = 0; j < bar; j++) {
      int cnt = 0;
      for (int i = 0; i < H; i++) {
        if (cnt == B)
          break;
        if (rest[i] > 0) {
          ans[i][j] = '1';
          rest[i]--;
          cnt++;
        }
      }
    }

    for (int j = bar; j < W; j++) {
      int cnt = 0;
      for (int i = 0; i < H; i++) {
        if (cnt == H - B)
          break;
        if (rest[i] > 0) {
          ans[i][j] = '1';
          rest[i]--;
          cnt++;
        }
      }
    }
  }
  assert(judge(ans, A, B));
  for (int i = 0; i < H; i++) {
    cout << ans[i] << "\n";
  }
  return 0;
}

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

using namespace std;

using ll = long long;

bool judge(vector<string> &ans, int A, int B) {
  int H = ans.size();
  int W = ans[0].size();
  for (int i = 0; i < H; i++) {
    int cnt = 0;
    for (int j = 0; j < W; j++) {
      cnt += ans[i][j] - '0';
    }
    cnt = min(cnt, W - cnt);
    if (cnt != A)
      return false;
  }

  for (int j = 0; j < W; j++) {
    int cnt = 0;
    for (int i = 0; i < H; i++) {
      cnt += ans[i][j] - '0';
    }
    cnt = min(cnt, H - cnt);
    if (cnt != B)
      return false;
  }
  return true;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int H, W, A, B;
  cin >> H >> W >> A >> B;
  vector<string> ans(H, string(W, '0'));
  if (H == 2 * B) {
    if (B * W == A * H) {
      string s1 = string(A, '1') + string(A, '0');
      string s2 = string(A, '0') + string(A, '1');
      for (int i = 0; i < B; i++) {
        ans[i] = s1;
      }

      for (int i = B; i < H; i++) {
        ans[i] = s2;
      }
    } else {
      cout << "No\n";
      return 0;
    }
  } else {
    int hoge = H - 2 * B;
    int bar = -1;
    int ai = -1;
    for (int i = 0; i <= H; i++) {
      int anum = A * i + (W - A) * (H - i);
      if (anum < B * W)
        continue;
      int foo = anum - B * W;
      if (foo % hoge != 0)
        continue;
      int next_bar = W - foo / hoge;
      if (next_bar < 0 || next_bar > W)
        continue;
      bar = next_bar;
      ai = i;
      break;
    }

    if (bar == -1) {
      cout << "No\n";
      return 0;
    }

    // cerr << bar << " " << ai << endl;
    vector<int> rest(H, W - A);
    for (int i = 0; i < ai; i++) {
      rest[i] = A;
    }

    for (int j = 0; j < bar; j++) {
      int cnt = 0;
      for (int i = 0; i < H; i++) {
        if (cnt == B)
          break;
        if (rest[i] > 0) {
          ans[i][j] = '1';
          rest[i]--;
          cnt++;
        }
      }
    }

    for (int j = bar; j < W; j++) {
      int cnt = 0;
      for (int i = 0; i < H; i++) {
        if (cnt == H - B)
          break;
        if (rest[i] > 0) {
          ans[i][j] = '1';
          rest[i]--;
          cnt++;
        }
      }
    }
  }
  assert(judge(ans, A, B));
  for (int i = 0; i < H; i++) {
    cout << ans[i] << "\n";
  }
  return 0;
}