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 MN 107171
using namespace std;
using ll = long long;
ll n, k;
vector<tuple<int, int, int, int>> R;
ll calc(ll a, ll b, ll l) {
if(a % l == 0 && b % l == 0)
return ((a / l) * (b / l) + 1) / 2 * l * l;
if(a % l) {
ll nrseg;
if(a / l % 2ll == 0) {
if(b % (2ll * l) <= l) nrseg = (b / (2ll * l)) * l + b % (2 * l);
else nrseg = (b / (2ll * l) + 1) * l;
} else {
if(b % (2ll * l) <= l) nrseg = (b / (2ll * l)) * l;
else nrseg = (b / (2ll * l) - 1) * l + b % (2 * l);
}
return calc(a - (a % l), b, l) + (a % l) * nrseg;
}
return calc(b, a, l);
}
ll sol(ll l) {
ll arie = ((n / l) * (n / l) + 1) / 2 * l * l, selec;
for(auto [a, b, c, d] : R) {
selec = calc(c, d, l) + calc(a-1, b-1, l) - calc(c, b-1, l) - calc(a-1, d, l);
arie += (c - a + 1) * (d - b + 1) - 2 * selec;
}
return min(arie, 1ll * n * n - arie);
}
int main() {
cin >> n >> k;
for(int i = 1, a, b, c, d; i <= k; ++i)
cin >> a >> b >> c >> d, R.emplace_back(a, b, c, d);
ll re = LLONG_MAX;
for(int i = 1; i < n; ++i)
if(n % i == 0) re = min(re, sol(i));
cout << re << "\n";
}
# | 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... |