Problem p03689 (C++)

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

#ifndef LOCAL_
#define fprintf                                                                \
  if (false)                                                                   \
  fprintf
#endif // LOCAL_
#define dump() fprintf(stderr, "#%s.%d\n", __func__, __LINE__);
#define dumpl(x1)                                                              \
  fprintf(stderr, "#%s.%d (%s) = (%ld)\n", __func__, __LINE__, #x1, x1);
#define dumpll(x1, x2)                                                         \
  fprintf(stderr, "#%s.%d (%s, %s) = (%ld, %ld)\n", __func__, __LINE__, #x1,   \
          #x2, x1, x2);
#define dumplll(x1, x2, x3)                                                    \
  fprintf(stderr, "#%s.%d (%s, %s, %s) = (%ld, %ld, %ld)\n", __func__,         \
          __LINE__, #x1, #x2, #x3, x1, x2, x3);
#define dumpd(x1)                                                              \
  fprintf(stderr, "#%s.%d (%s) = (%lf)\n", __func__, __LINE__, #x1, x1);
#define dumpdd(x1, x2)                                                         \
  fprintf(stderr, "#%s.%d (%s, %s) = (%lf, %lf)\n", __func__, __LINE__, #x1,   \
          #x2, x1, x2);
#define loop for (;;)
typedef std::vector<long> LI;
typedef std::queue<long> QI;
#define rep(i, n) for (long i = 0; i < (long)n; ++i)
const double pi = M_PI;

template <typename T> void scan1(T &x) { fprintf(stderr, "unknown type\n"); }
template <> void scan1(long &x) {
  if (scanf("%ld", &x) < 0)
    exit(0);
}
template <> void scan1(std::string &x) {
  if (not(std::cin >> x))
    exit(0);
}
void scan() {}
template <typename Head, typename... Tail> void scan(Head &x, Tail &...xs) {
  scan1(x);
  scan(xs...);
}

struct Solver {
  Solver() { fprintf(stderr, "--------Solver begin--------\n"); }
  ~Solver() { fprintf(stderr, "--------Solver end--------\n"); }
  void solve() {
    long h1, w1, h2, w2;
    scan(h1, w1, h2, w2);
    if (w2 == 1 or h2 == 1) {
      assert(0);
      bool res =
          (h1 / h2) * (w1 / w2) < h1 * w1 - (h1 / h2 * h2) * (w1 / w2 * w2);
      puts(res ? "Yes" : "No");
      if (res) {
        for (long r = 0; r < h1; ++r) {
          printf("%ld",
                 (r % h2 == h2 - 1) and (0 % w2 == w2 - 1) ? -h2 * w2 : 1);
          for (long c = 1; c < w1; ++c) {
            printf(" %ld",
                   (r % h2 == h2 - 1) and (c % w2 == w2 - 1) ? -h2 * w2 : 1);
          }
          putchar('\n');
        }
      }
    } else {
      long big = 500000000;
      bool res = h1 % h2 != 0 or w1 % w2 != 0;
      puts(res ? "Yes" : "No");
      if (res) {
        for (long r = 0; r < h1; ++r) {
          printf("%ld", (r % h2 == h2 - 1) and (0 % w2 == w2 - 1)
                            ? -h2 * w2 - big
                        : (r % h2 == 0 and 0 % w2 == 0) ? big + 1
                                                        : 1);
          for (long c = 1; c < w1; ++c) {
            printf(" %ld", (r % h2 == h2 - 1) and (c % w2 == w2 - 1)
                               ? -h2 * w2 - big
                           : (r % h2 == 0 and c % w2 == 0) ? big + 1
                                                           : 1);
          }
          putchar('\n');
        }
      }
    }
  }
};

int main() { loop std::unique_ptr<Solver>(new Solver())->solve(); }


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

#ifndef LOCAL_
#define fprintf                                                                \
  if (false)                                                                   \
  fprintf
#endif // LOCAL_
#define dump() fprintf(stderr, "#%s.%d\n", __func__, __LINE__);
#define dumpl(x1)                                                              \
  fprintf(stderr, "#%s.%d (%s) = (%ld)\n", __func__, __LINE__, #x1, x1);
#define dumpll(x1, x2)                                                         \
  fprintf(stderr, "#%s.%d (%s, %s) = (%ld, %ld)\n", __func__, __LINE__, #x1,   \
          #x2, x1, x2);
#define dumplll(x1, x2, x3)                                                    \
  fprintf(stderr, "#%s.%d (%s, %s, %s) = (%ld, %ld, %ld)\n", __func__,         \
          __LINE__, #x1, #x2, #x3, x1, x2, x3);
#define dumpd(x1)                                                              \
  fprintf(stderr, "#%s.%d (%s) = (%lf)\n", __func__, __LINE__, #x1, x1);
#define dumpdd(x1, x2)                                                         \
  fprintf(stderr, "#%s.%d (%s, %s) = (%lf, %lf)\n", __func__, __LINE__, #x1,   \
          #x2, x1, x2);
#define loop for (;;)
typedef std::vector<long> LI;
typedef std::queue<long> QI;
#define rep(i, n) for (long i = 0; i < (long)n; ++i)
const double pi = M_PI;

template <typename T> void scan1(T &x) { fprintf(stderr, "unknown type\n"); }
template <> void scan1(long &x) {
  if (scanf("%ld", &x) < 0)
    exit(0);
}
template <> void scan1(std::string &x) {
  if (not(std::cin >> x))
    exit(0);
}
void scan() {}
template <typename Head, typename... Tail> void scan(Head &x, Tail &...xs) {
  scan1(x);
  scan(xs...);
}

struct Solver {
  Solver() { fprintf(stderr, "--------Solver begin--------\n"); }
  ~Solver() { fprintf(stderr, "--------Solver end--------\n"); }
  void solve() {
    long h1, w1, h2, w2;
    scan(h1, w1, h2, w2);
    if (w2 == 1 and h2 == 1) {
      puts("No");
    }
    // else if( w2 == 1 or h2 == 1 ) {
    //    bool res = (h1 / h2) * (w1 / w2) < h1 * w1 - (h1 / h2 * h2) * (w1 / w2
    //    * w2); puts(res ? "Yes" : "No"); if( res ) {
    //       for(long r = 0; r < h1; ++r) {
    //          printf("%ld", (r % h2 == h2 - 1) and (0 % w2 == w2 - 1) ? -h2 *
    //          w2 : 1); for(long c = 1; c < w1; ++c) {
    //             printf(" %ld", (r % h2 == h2 - 1) and (c % w2 == w2 - 1) ?
    //             -h2 * w2 : 1);
    //          }
    //          putchar('\n');
    //       }
    //    }
    // }
    else {
      long big = 500000000;
      bool res = h1 % h2 != 0 or w1 % w2 != 0;
      puts(res ? "Yes" : "No");
      if (res) {
        for (long r = 0; r < h1; ++r) {
          printf("%ld", (r % h2 == h2 - 1) and (0 % w2 == w2 - 1)
                            ? -h2 * w2 - big
                        : (r % h2 == 0 and 0 % w2 == 0) ? big + 1
                                                        : 1);
          for (long c = 1; c < w1; ++c) {
            printf(" %ld", (r % h2 == h2 - 1) and (c % w2 == w2 - 1)
                               ? -h2 * w2 - big
                           : (r % h2 == 0 and c % w2 == 0) ? big + 1
                                                           : 1);
          }
          putchar('\n');
        }
      }
    }
  }
};

int main() { loop std::unique_ptr<Solver>(new Solver())->solve(); }