This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define int long long
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
int N, K;
vector<pair<ii, ii>> rect;
ll res = 1e18;
ll sub(int d, int x, int y)
{
  int X = x / d;
  int Y = y / d;
  ll ret = 0;
  if(X % 2 == 0 || Y % 2 == 0)
    ret += 1ll * X * Y / 2 * d * d;
  else
    ret += (1ll * X * Y / 2 + 1) * d * d;
  if (X % 2 == Y % 2) {
		ret += (ll) (x - X * d) * (y - Y * d);
	}
	if (Y % 2 == 0) {
		ret += (ll) (X + 1) / 2 * (y - Y * d) * d;
	}
	else {
		ret += (ll) X / 2 * (y - Y * d) * d;
	}
	if (X % 2 == 0) {
		ret += (ll) (Y + 1) / 2 * (x - X * d) * d;
	}
	else {
		ret += (ll) Y / 2 * (x - X * d) * d;
	}
	return ret;
}
void solve(int d)
{
  ll sum = 0, rem = 0;
  for(auto & all : rect){
    sum += sub(d, all.se.fi, all.se.se) + sub(d, all.fi.fi - 1, all.fi.se - 1)
            - sub(d, all.fi.fi - 1, all.se.se) - sub(d, all.se.fi, all.fi.se - 1);
    rem += 1ll * (all.se.se - all.fi.se + 1) * (all.se.fi - all.fi.fi + 1);
  }
  ll black = sub(d, N, N);
  ll white = N * N - black;
  res = min(res, black + rem - 2 * sum);
  res = min(res, white - rem + 2 * sum);
}
signed main(void)
{
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  #ifdef LOCAL
    freopen("A.INP", "r", stdin);
    freopen("A.OUT", "w", stdout);
  #endif // LOCAL
  cin >> N >> K;
  rect.resize(K);
  for(auto & x : rect) cin >> x.fi.fi >> x.fi.se >> x.se.fi >> x.se.se;
  for(int i = 1; i < N; ++i){
    if(N % i == 0) solve(i);
  }
  cout << res;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |