Problem p03689 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
#define REP(i, a, n) for (int i = (a); i < (int)(n); ++i)
#define REM(i, a, n) for (int i = ((n)-1); i >= (a); --i)
#define EPS 1e-11
#define INF 0x3FFFFFFF
#define INFLL 0x3FFFFFFF3FFFFFFF
#define INFD 1.0e+308
#define FLOAT setprecision(16)
const double PI = 3.1415926535897932;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<LL, LL> PP;

template <class T, class U>
istream &operator>>(istream &ist, pair<T, U> &right) {
  return ist >> right.first >> right.second;
}
template <class T, class U>
ostream &operator<<(ostream &ost, pair<T, U> &right) {
  return ost << right.first << ' ' << right.second;
}
template <class T, class TCompatible, size_t N>
void Fill(T (&dest)[N], const TCompatible &val) {
  fill(begin(dest), end(dest), val);
}
template <class T, class TCompatible, size_t M, size_t N>
void Fill(T (&dest)[M][N], const TCompatible &val) {
  for (int i = 0; i < M; ++i)
    Fill(dest[i], val);
}
// sum, 累積和
//  負のときの割り算に注意
#if 1
#include <array>
#include <random>
#include <unordered_map>
#include <unordered_set>
template <class T>
using PriorityQ = priority_queue<T, vector<T>, greater<T>>; // コスト小を優先
#endif

#ifdef ONLY_MY_ENVIR
#include "Graph.h"
#include "IntMod.h"
#include "Matrix.h"
#include "Suffix_Array.h"
#include "Union_Find.h"
#endif

#ifdef __GNUC__
typedef __int128 LLL;
istream &operator>>(istream &ist, __int128 &val) {
  LL tmp;
  ist >> tmp;
  val = tmp;
  return ist;
}
ostream &operator<<(ostream &ost, __int128 val) {
  LL tmp = val;
  ost << tmp;
  return ost;
}
#endif

int H, W, h, w;
LL A[500][500];
bool solve() {
  LL sum = 0;

  LL plus = 999999999 / (h * w - 1);
  LL minus = (h * w - 1) * plus + 1;
  REP(i, 0, H) {
    REP(j, 0, W) {
      A[i][j] = (i % h == h - 1 && j % w == w - 1 ? -minus : plus);
      sum += A[i][j];
    }
  }
  return sum > 0;
}

int main() {
  cin >> H >> W >> h >> w;
  int ok = solve();
  cout << (ok ? "Yes" : "No") << endl;
  if (ok) {
    REP(i, 0, H) {
      REP(j, 0, W) { cout << A[i][j] << (j == W - 1 ? '\n' : ' '); }
    }
  }

  return 0;
}


Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
#define REP(i, a, n) for (int i = (a); i < (int)(n); ++i)
#define REM(i, a, n) for (int i = ((n)-1); i >= (a); --i)
#define EPS 1e-11
#define INF 0x3FFFFFFF
#define INFLL 0x3FFFFFFF3FFFFFFF
#define INFD 1.0e+308
#define FLOAT setprecision(16)
const double PI = 3.1415926535897932;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<LL, LL> PP;

template <class T, class U>
istream &operator>>(istream &ist, pair<T, U> &right) {
  return ist >> right.first >> right.second;
}
template <class T, class U>
ostream &operator<<(ostream &ost, pair<T, U> &right) {
  return ost << right.first << ' ' << right.second;
}
template <class T, class TCompatible, size_t N>
void Fill(T (&dest)[N], const TCompatible &val) {
  fill(begin(dest), end(dest), val);
}
template <class T, class TCompatible, size_t M, size_t N>
void Fill(T (&dest)[M][N], const TCompatible &val) {
  for (int i = 0; i < M; ++i)
    Fill(dest[i], val);
}
// sum, 累積和
//  負のときの割り算に注意
#if 1
#include <array>
#include <random>
#include <unordered_map>
#include <unordered_set>
template <class T>
using PriorityQ = priority_queue<T, vector<T>, greater<T>>; // コスト小を優先
#endif

#ifdef ONLY_MY_ENVIR
#include "Graph.h"
#include "IntMod.h"
#include "Matrix.h"
#include "Suffix_Array.h"
#include "Union_Find.h"
#endif

#ifdef __GNUC__
typedef __int128 LLL;
istream &operator>>(istream &ist, __int128 &val) {
  LL tmp;
  ist >> tmp;
  val = tmp;
  return ist;
}
ostream &operator<<(ostream &ost, __int128 val) {
  LL tmp = val;
  ost << tmp;
  return ost;
}
#endif

int H, W, h, w;
LL A[500][500];
bool solve() {
  if (h == 1 && w == 1)
    return false;
  LL sum = 0;

  LL plus = 999999999 / (h * w - 1);
  LL minus = (h * w - 1) * plus + 1;
  REP(i, 0, H) {
    REP(j, 0, W) {
      A[i][j] = (i % h == h - 1 && j % w == w - 1 ? -minus : plus);
      sum += A[i][j];
    }
  }
  return sum > 0;
}

int main() {
  cin >> H >> W >> h >> w;
  int ok = solve();
  cout << (ok ? "Yes" : "No") << endl;
  if (ok) {
    REP(i, 0, H) {
      REP(j, 0, W) { cout << A[i][j] << (j == W - 1 ? '\n' : ' '); }
    }
  }

  return 0;
}