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 <iostream>
#include <algorithm>
#include <vector>
using namespace std;
using ll = long long;
struct el {
ll a, b, c, d;
};
vector<ll> dv;
vector<el> points;
ll ans;
ll prefans(ll x, ll y, ll k, int f)
{
ll ans;
ll ab = y / k, a;
a = (ab + 1) / 2;
a *= k;
if (ab % 2 == 0)
a += y % k;
if (x <= k)
ans = x * a;
else
{
ll aa = y - a;
ll q = x / k;
ans = aa * (q / 2) * k;
ans += a * ((q + 1) / 2) * k;
if (q % 2)
ans += aa * (x % k);
else ans += a * (x % k);
}
if (f == 1)
return ans;
else return x * y - ans;
}
ll Paint(ll x, ll y, ll d, int f) {
return prefans(x, y, d, f) - (x * y - prefans(x, y, d, f));
}
int main()
{
ll n, k;
cin >> n >> k;
ans = n * n;
for (int i = 1; i < n; i++)
if (n % i == 0)dv.push_back(i);
for (int i = 0; i < k; i++)
{
ll a, b, c, d;
cin >> a >> b >> c >> d;
points.push_back({ a,b,c,d });
}/*
for (auto p : points)
cerr << p.first << ' ' << p.second << '\n';*/
for (ll d : dv)
{
ll blackans;
ll q = n / d;
if (q % 2)
blackans = (n * n - d * d) / 2 + d * d;
else blackans = n * n / 2;
ll whiteans = n * n - blackans;
for (auto p : points)
{
blackans += Paint(p.c, p.d, d, 0) - Paint(p.a - 1, p.d, d, 0) - Paint(p.c, p.b - 1, d, 0) + Paint(p.a - 1, p.b - 1, d, 0);
whiteans += Paint(p.c, p.d, d, 1) - Paint(p.a - 1, p.d, d, 1) - Paint(p.c, p.b - 1, d, 1) + Paint(p.a - 1, p.b - 1, d, 1);
}
ans = min(ans, min(blackans, whiteans));
}
cout << ans;
return 0;
}
# | 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... |