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>
using namespace std;
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
using ll = long long;
using bk = tuple<ll, ll, ll, ll, ll>;
ll INF = 1LL << 60;
ll find(ll p, ll k) {
return p / (2 * k) * k + min(k, p % (2 * k));
}
ll count1d(ll x, ll w, ll k) {
return find(x + w, k) - find(x, k);
}
ll count2d(ll x, ll y, ll w, ll h, ll k) {
ll r = count1d(x, w, k);
ll c = count1d(y, h, k);
return r * c + (w-r) * (h-c);
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
ll N, K, A;
cin >> N >> K;
A = N*N;
vector<bk> blocks (K);
F0R(i, K) {
ll x, y, w, h;
cin >> x >> y >> w >> h;
w-=x; h-=y;
--x; --y; ++w; ++h;
blocks[i] = make_tuple(w*h, y, x, h, w);
}
ll best = INF;
ROF(i, 1, N) {
if (N % i != 0) continue;
ll c = count2d(0, 0, N, N, i);
for (bk block : blocks) {
ll x, y, w, h, a;
tie(a, x, y, w, h) = block;
ll overlap = count2d(x, y, w, h, i);
c -= overlap;
c += a - overlap;
}
best = min(best, c);
best = min(best, A-c);
}
cout << best << endl;
}
# | 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... |