Problem p03689 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <forward_list>
#include <initializer_list>
#include <tuple>

#define cauto const auto &
#else

#endif

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;

#define VV(T) vector<vector<T>>

template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}

template <class F, class T> void convert(const F &f, T &t) {
  stringstream ss;
  ss << f;
  ss >> t;
}

#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _rep(i, n) _rep2((i), 0, (n))
#define _rep2(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(...) GET_MACRO(__VA_ARGS__, _rep2, _rep)(__VA_ARGS__)
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define fi first
#define se second
#define mkp make_pair
#define DEBUG
#ifdef DEBUG
#define dump(x) cout << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cout << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;
#else
#define dump(x)
#define debug(x)
#endif

#define MOD 1000000007LL
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define maxs(x, y) x = max(x, y)
#define mins(x, y) x = min(x, y)

int H, W, h, w;

void func(int hh, int ww, int val1, int val2) {
  vvint vv;
  initvv(vv, H, W, val2);
  int num = H * W * val2;
  rep(i, H) {
    rep(j, W) {
      int h1 = hh - 1 + i * (hh);
      int w2 = ww - 1 + j * (ww);
      if (h1 >= H || w2 >= W)
        break;
      vv[h1][w2] = val1;
      num += -val2 + val1;
    }
  }
  assert(num > 0);
  int cnt = 0;
  rep(i, h) { rep(j, w) cnt += vv[i][j]; }
  // cout<<val<<endl;
  // cout<<cnt<<endl;
  assert(cnt < 0);
  cout << "Yes" << endl;
  rep(i, H) {
    rep(j, W) {
      if (j)
        cout << " ";
      cout << vv[i][j];
      // printf("%3d", vv[i][j]);
    }
    cout << endl;
  }
}

void mainmain() {
  cin >> H >> W >> h >> w;
  // vvint vv;
  // initvv(vv, H, W, 1);
  // H = rand()%200+100;
  // W = rand()%200+100;
  // h = H - rand()%30;
  // w = W - rand()%30;
  // cout<<H<<" "<<W<<" "<<h<<" "<<w<<endl;
  int num = -h * w;
  int cnt = H * W;
  rep(cost, 1, 600) rep(hh, 1, h + 1) {
    rep(ww, 1, w + 1) {
      if (hh * ww == 1)
        continue;
      int t = (h / hh) * (w / ww);
      int val = cost * (h * w - t) / t + 1;
      val *= -1;
      int tt = (H / hh) * (W / ww);
      int cnt = tt * val + (H * W - tt) * cost;
      if (cnt > 0) {
        // cout<<hh<<" "<<ww<<" "<<t<<" "<<val<<" "<<cnt<<endl;
        // cout<<tt<<endl;
        func(hh, ww, val, cost);
        return;
      }
    }
  }
  cout << "No" << endl;
  return;
  // rep(i,H){
  // 	rep(j,W){
  // 		int hh = h-1 + i*(h);
  // 		int ww = w-1 + j*(w);
  // 		if(hh>=H||ww>=W) break;
  // 		// if(hh>=H){
  // 		// 	if(hh-h+1<H) hh=H-1;
  // 		// 	else break;
  // 		// }
  // 		// if(ww>=W){
  // 		// 	if(ww-w+1<W) ww=W-1;
  // 		// 	else break;
  // 		// }
  // 		vv[hh][ww] = num;
  // 		// cout<<hh<<" "<<ww<<endl;
  // 		cnt += -1+num;
  // 	}
  // }
  // if(cnt>0){
  // 	cout<<"Yes"<<endl;
  // 	rep(i,H){
  // 		rep(j,W){
  // 			if(j) cout<<" ";
  // 			cout<<vv[i][j];
  // 		}
  // 		cout<<endl;
  // 	}
  // }
  // else{
  // 	cout<<"No"<<endl;
  // }
}

signed main() {
  // ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);
  mainmain();
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <forward_list>
#include <initializer_list>
#include <tuple>

#define cauto const auto &
#else

#endif

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;

#define VV(T) vector<vector<T>>

template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}

template <class F, class T> void convert(const F &f, T &t) {
  stringstream ss;
  ss << f;
  ss >> t;
}

#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _rep(i, n) _rep2((i), 0, (n))
#define _rep2(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(...) GET_MACRO(__VA_ARGS__, _rep2, _rep)(__VA_ARGS__)
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define fi first
#define se second
#define mkp make_pair
#define DEBUG
#ifdef DEBUG
#define dump(x) cout << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cout << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;
#else
#define dump(x)
#define debug(x)
#endif

#define MOD 1000000007LL
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define maxs(x, y) x = max(x, y)
#define mins(x, y) x = min(x, y)

int H, W, h, w;

void func(int hh, int ww, int val1, int val2) {
  vvint vv;
  initvv(vv, H, W, val2);
  int num = H * W * val2;
  rep(i, H) {
    rep(j, W) {
      int h1 = hh - 1 + i * (hh);
      int w2 = ww - 1 + j * (ww);
      if (h1 >= H || w2 >= W)
        break;
      vv[h1][w2] = val1;
      num += -val2 + val1;
    }
  }
  assert(num > 0);
  int cnt = 0;
  rep(i, h) { rep(j, w) cnt += vv[i][j]; }
  // cout<<val<<endl;
  // cout<<cnt<<endl;
  assert(cnt < 0);
  cout << "Yes" << endl;
  rep(i, H) {
    rep(j, W) {
      if (j)
        cout << " ";
      cout << vv[i][j];
      // printf("%3d", vv[i][j]);
    }
    cout << endl;
  }
}

void mainmain() {
  cin >> H >> W >> h >> w;
  // vvint vv;
  // initvv(vv, H, W, 1);
  // H = rand()%200+100;
  // W = rand()%200+100;
  // h = H - rand()%30;
  // w = W - rand()%30;
  // cout<<H<<" "<<W<<" "<<h<<" "<<w<<endl;
  int num = -h * w;
  int cnt = H * W;
  rep(cost, 550, 600) rep(hh, 1, h + 1) {
    rep(ww, 1, w + 1) {
      if (hh * ww == 1)
        continue;
      int t = (h / hh) * (w / ww);
      int val = cost * (h * w - t) / t + 1;
      val *= -1;
      int tt = (H / hh) * (W / ww);
      int cnt = tt * val + (H * W - tt) * cost;
      if (cnt > 0) {
        // cout<<hh<<" "<<ww<<" "<<t<<" "<<val<<" "<<cnt<<endl;
        // cout<<tt<<endl;
        func(hh, ww, val, cost);
        return;
      }
    }
  }
  cout << "No" << endl;
  return;
  // rep(i,H){
  // 	rep(j,W){
  // 		int hh = h-1 + i*(h);
  // 		int ww = w-1 + j*(w);
  // 		if(hh>=H||ww>=W) break;
  // 		// if(hh>=H){
  // 		// 	if(hh-h+1<H) hh=H-1;
  // 		// 	else break;
  // 		// }
  // 		// if(ww>=W){
  // 		// 	if(ww-w+1<W) ww=W-1;
  // 		// 	else break;
  // 		// }
  // 		vv[hh][ww] = num;
  // 		// cout<<hh<<" "<<ww<<endl;
  // 		cnt += -1+num;
  // 	}
  // }
  // if(cnt>0){
  // 	cout<<"Yes"<<endl;
  // 	rep(i,H){
  // 		rep(j,W){
  // 			if(j) cout<<" ";
  // 			cout<<vv[i][j];
  // 		}
  // 		cout<<endl;
  // 	}
  // }
  // else{
  // 	cout<<"No"<<endl;
  // }
}

signed main() {
  // ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);
  mainmain();
}