Problem p03689 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define _repargs(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep(...) _repargs(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define mod 1000000007
#define inf 2000000007
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;
template <typename T> inline void output(T a, int p = 0) {
  if (p)
    cout << fixed << setprecision(p) << a << "\n";
  else
    cout << a << "\n";
}

template <typename T> inline void voutput(T &v) {
  rep(i, v.size()) {
    if (i)
      cout << " " << v[i];
    else
      cout << v[i];
  }
  cout << endl;
}
// end of template
ll A[1010][1010];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  // source code
  ll big = 1000000000;
  ll H, W, h, w;
  cin >> H >> W >> h >> w;

  ll hh = H / h, ww = W / w;
  ll mul = (big - 1) / (h * w - 1);

  //    vector<vector<ll>> A(H, vector<ll>(W, mul));
  rep(i, H) rep(j, W) { A[i][j] = mul; }
  rep(i, hh) rep(j, ww) {
    A[(i + 1) * h - 1][(j + 1) * w - 1] = -mul * (h * w - 1) - 1;
  }

  ll sum = 0;
  rep(i, H) rep(j, W) { sum += A[i][j]; }
  if (sum > 0) {
    output("Yes");
    rep(i, H) {
      rep(j, W) {
        if (j)
          cout << " " << A[i][j];
        else
          cout << A[i][j];
      }
      cout << endl;
    }
  } else {
    output("No");
    //        rep(i, H){
    //            voutput(A[i]);
    //        }
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define _repargs(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep(...) _repargs(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define mod 1000000007
#define inf 2000000007
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;
template <typename T> inline void output(T a, int p = 0) {
  if (p)
    cout << fixed << setprecision(p) << a << "\n";
  else
    cout << a << "\n";
}

template <typename T> inline void voutput(T &v) {
  rep(i, v.size()) {
    if (i)
      cout << " " << v[i];
    else
      cout << v[i];
  }
  cout << endl;
}
// end of template
ll A[1010][1010];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  // source code
  ll big = 1000000000;
  ll H, W, h, w;
  cin >> H >> W >> h >> w;

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

  ll hh = H / h, ww = W / w;
  ll mul = (big - 1) / (h * w - 1);

  //    vector<vector<ll>> A(H, vector<ll>(W, mul));
  rep(i, H) rep(j, W) { A[i][j] = mul; }
  rep(i, hh) rep(j, ww) {
    A[(i + 1) * h - 1][(j + 1) * w - 1] = -mul * (h * w - 1) - 1;
  }

  ll sum = 0;
  rep(i, H) rep(j, W) { sum += A[i][j]; }
  if (sum > 0) {
    output("Yes");
    rep(i, H) {
      rep(j, W) {
        if (j)
          cout << " " << A[i][j];
        else
          cout << A[i][j];
      }
      cout << endl;
    }
  } else {
    output("No");
    //        rep(i, H){
    //            voutput(A[i]);
    //        }
  }

  return 0;
}