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;
const int N = 1e5 + 5;
const long long INF = 1e18 + 5;
const int MAXN = 1e6;
const int base = 317;
typedef long long ll;
const ll bs = 1e5 + 1;
const ll mod = 998244353;
#define int ll
ll ans = INF, n, k, xx[N], yy[N], xx1[N], yy1[N];
int fnd(int cor, int x) {
if (cor <= 0)
return 0;
int res = (cor / x) / 2 * x;
if ((cor / x) % 2)
res += cor % x + 1;
return res;
}
ll calc(ll x) {
ll res1 = 0, res2 = 0;
for (int i = 1; i <= k; i++) {
int b1 = fnd(xx1[i], x) - fnd(xx[i] - 1, x),
w1 = (xx1[i] - xx[i] + 1) - b1,
b2 = fnd(yy1[i], x) - fnd(yy[i] - 1, x),
w2 = (yy1[i] - yy[i] + 1) - b2;
int x1 = xx[i] / x, y1 = yy[i] / x;
if (x1 % 2) {
swap(b2, w2);
}
if (y1 % 2) {
swap(b1, w1);
}
if (x1 % 2 == y1 % 2) {
res1 += b1 * b2 + w1 * w2;
res2 += (b1 * w2 + w1 * b2);
} else {
res1 += b1 * w2 + w1 * b2;
res2 += b1 * b2 + w1 * w2;
}
}
ll c1 = (n / x) * (n / x) / 2 * x * x;
ll c2 = n * n - c1;
return min(c1 - res2 + res1, c2 - res1 + res2);
}
main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for (int i = 1; i <= k; i++) {
cin >> xx[i] >> yy[i] >> xx1[i] >> yy1[i];
xx[i]--;
yy[i]--;
xx1[i]--;
yy1[i]--;
}
for (int i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
ans = min(ans, calc(i));
if (i != 1)
ans = min(ans, calc(n / i));
}
}
cout << ans;
return 0;
}
/*
*/
Compilation message (stderr)
chessboard.cpp:55:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main() {
^
# | 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... |