Problem p03689 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <unistd.h>
#define ll long long
#define ull unsigned long long
#define inf 1000000007
#define inf16 0x3f3f3f3f
#define INF 1000000000000000007LL
#define VI vector<int>
#define VPII vector<pair<int, int>>
#define VLL vector<ll>
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\n'
#define ALL(c) (c).begin(), (c).end()
#define flush fflush(stdout)
using namespace std;

#define N 1007
#define int long long

int H, W, h, w, zle, dobre;

bitset<N> vis[N];

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

  cin >> H >> W >> h >> w;

  for (int i = 1; i + h - 1 <= H; i += h) {
    for (int j = 1; j + w - 1 <= W; j += w) {
      vis[i + h - 1][j + w - 1] = 1;
    }
  }

  for (int i = 1; i <= H; ++i) {
    zle += vis[i].count();
  }

  dobre = H * W - zle;

  if (-zle * h * w + dobre > 0ll) {
    cout << "Yes" << endl;

    for (int i = 1; i <= H; ++i) {
      for (int j = 1; j <= W; ++j) {
        if (vis[i][j])
          cout << -h * w << " ";
        else
          cout << 1 << " ";
      }

      cout << endl;
    }
  } else {
    int d = (1000ll * 1000 * 1000 - 1) / (h * w - 1);
    int u = -d * (h * w - 1) - 1;

    if (zle * u + dobre * d > 0ll) {
      cout << "Yes" << endl;

      for (int i = 1; i <= H; ++i) {
        for (int j = 1; j <= W; ++j) {
          if (vis[i][j])
            cout << u << " ";
          else
            cout << d << " ";
        }

        cout << endl;
      }
    } else
      cout << "No";
  }
}


Fixed submission:
#include <bits/stdc++.h>
#include <unistd.h>
#define ll long long
#define ull unsigned long long
#define inf 1000000007
#define inf16 0x3f3f3f3f
#define INF 1000000000000000007LL
#define VI vector<int>
#define VPII vector<pair<int, int>>
#define VLL vector<ll>
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\n'
#define ALL(c) (c).begin(), (c).end()
#define flush fflush(stdout)
using namespace std;

#define N 1007
#define int long long

int H, W, h, w, zle, dobre;

bitset<N> vis[N];

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

  cin >> H >> W >> h >> w;

  if (h * w == 1) {
    cout << "No";
    return 0;
  }

  for (int i = 1; i + h - 1 <= H; i += h) {
    for (int j = 1; j + w - 1 <= W; j += w) {
      vis[i + h - 1][j + w - 1] = 1;
    }
  }

  for (int i = 1; i <= H; ++i) {
    zle += vis[i].count();
  }

  dobre = H * W - zle;

  if (-zle * h * w + dobre > 0ll) {
    cout << "Yes" << endl;

    for (int i = 1; i <= H; ++i) {
      for (int j = 1; j <= W; ++j) {
        if (vis[i][j])
          cout << -h * w << " ";
        else
          cout << 1 << " ";
      }

      cout << endl;
    }
  } else {
    int d = (1000ll * 1000 * 1000 - 1) / (h * w - 1);
    int u = -d * (h * w - 1) - 1;

    if (zle * u + dobre * d > 0ll) {
      cout << "Yes" << endl;

      for (int i = 1; i <= H; ++i) {
        for (int j = 1; j <= W; ++j) {
          if (vis[i][j])
            cout << u << " ";
          else
            cout << d << " ";
        }

        cout << endl;
      }
    } else
      cout << "No";
  }
}