Problem p03689 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#define ws ws_____________________
#define y1 y1_____________________
#define y0 y0_____________________
#define left left_________________
#define right right_______________
#define next next_________________
#define prev prev_________________
#define hash hash_________________

#define pb push_back
#define fst first
#define snd second
#define mp make_pair
#define sz(C) ((int)(C).size())
#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define ford(i, n) for (int i = int(n) - 1; i >= 0; --i)
#define all(C) begin(C), end(C)

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef long double ld;
typedef complex<double> cd;

#define FILE_NAME "a"

int H, W, h, w;

bool read() {
  if (scanf("%d%d%d%d", &H, &W, &h, &w) < 4) {
    return 0;
  }
  return 1;
}

const int MAXN = 500 + 10;
const int MAX = int(1e9);

int a[MAXN][MAXN];

bool solve() {
  memset(a, 0, sizeof a);

  const int x = (MAX - 1) / (h * w - 1);
  const int y = x * (h * w - 1) + 1;
  assert(0 <= x && x <= MAX);
  assert(0 <= y && y <= MAX);

  forn(i, H) {
    forn(j, W) {
      if ((i + 1) % h == 0 && (j + 1) % w == 0) {
        a[i][j] = -y;
      } else {
        a[i][j] = x;
      }
    }
  }

  ll sum = 0;
  forn(i, H) {
    forn(j, W) { sum += a[i][j]; }
  }

  if (sum <= 0) {
    return 0;
  }

  puts("Yes");
  forn(i, H) {
    forn(j, W) { printf("%d ", a[i][j]); }
    puts("");
  }
  puts("");
  return 1;
}

int main() {
#ifdef LOCAL
  freopen(FILE_NAME ".in", "r", stdin);
  // freopen(FILE_NAME ".out", "w", stdout);
#endif

  while (read()) {
    if (!solve()) {
      puts("No");
    }
  }

#ifdef LOCAL
  cerr.precision(5);
  cerr << "Time: " << fixed << (double)clock() / CLOCKS_PER_SEC << endl;
#endif
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#define ws ws_____________________
#define y1 y1_____________________
#define y0 y0_____________________
#define left left_________________
#define right right_______________
#define next next_________________
#define prev prev_________________
#define hash hash_________________

#define pb push_back
#define fst first
#define snd second
#define mp make_pair
#define sz(C) ((int)(C).size())
#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define ford(i, n) for (int i = int(n) - 1; i >= 0; --i)
#define all(C) begin(C), end(C)

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef long double ld;
typedef complex<double> cd;

#define FILE_NAME "a"

int H, W, h, w;

bool read() {
  if (scanf("%d%d%d%d", &H, &W, &h, &w) < 4) {
    return 0;
  }
  return 1;
}

const int MAXN = 500 + 10;
const int MAX = int(1e9);

int a[MAXN][MAXN];

bool solve() {
  if (H % h == 0 && W % w == 0) {
    return 0;
  }

  memset(a, 0, sizeof a);

  const int x = (MAX - 1) / (h * w - 1);
  const int y = x * (h * w - 1) + 1;
  assert(0 <= x && x <= MAX);
  assert(0 <= y && y <= MAX);

  forn(i, H) {
    forn(j, W) {
      if ((i + 1) % h == 0 && (j + 1) % w == 0) {
        a[i][j] = -y;
      } else {
        a[i][j] = x;
      }
    }
  }

  ll sum = 0;
  forn(i, H) {
    forn(j, W) { sum += a[i][j]; }
  }

  if (sum <= 0) {
    return 0;
  }

  puts("Yes");
  forn(i, H) {
    forn(j, W) { printf("%d ", a[i][j]); }
    puts("");
  }
  puts("");
  return 1;
}

int main() {
#ifdef LOCAL
  freopen(FILE_NAME ".in", "r", stdin);
  // freopen(FILE_NAME ".out", "w", stdout);
#endif

  while (read()) {
    if (!solve()) {
      puts("No");
    }
  }

#ifdef LOCAL
  cerr.precision(5);
  cerr << "Time: " << fixed << (double)clock() / CLOCKS_PER_SEC << endl;
#endif
  return 0;
}