#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tpl;
#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())
const int mn = 5e5 + 5;
ll xL[mn], yL[mn], xR[mn], yR[mn], cnt[2][2];
ll squared (ll a) { return a * a; }
void compute (vector<ll> &a, int L, int R, int u) {
int dL = L / u, dR = R / u, mid = dR - dL - 1;
if (dL == dR)
return a[dL & 1] += R % u - L % u + 1, void();
a[dL & 1] += u - L % u, a[dR & 1] += 1 + R % u;
if (mid & 1) {
a[dL & 1 ^ 1] += ((mid >> 1) + (mid & 1)) * u;
a[dL & 1] += (mid >> 1) * u;
}
else a[0] += (mid >> 1) * u, a[1] += (mid >> 1) * u;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, k; cin >> n >> k;
vector<int> wish;
for (int i = 1; i * i <= n; i++) {
if (n % i) continue;
wish.push_back(i);
if (i != n / i && n / i < n) wish.push_back(n / i);
}
for (int i = 0; i < k; i++) {
cin >> xL[i] >> yL[i] >> xR[i] >> yR[i];
xL[i]--, yL[i]--, xR[i]--, yR[i]--;
}
ll ans = LLONG_MAX;
for (int u : wish) {
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++) cnt[i][j] = 0;
ll sq = n / u;
cnt[0][0] = (squared((sq >> 1) + (sq & 1)) + squared(sq >> 1)) * u * u;
cnt[0][1] = (2LL * (sq >> 1) * ((sq >> 1) + (sq & 1))) * u * u;
for (int i = 0; i < k; i++) {
vector<ll> row(2), col(2);
compute(row, xL[i], xR[i], u), compute(col, yL[i], yR[i], u);
// if (u == 1)
// cout << row[0] << " " << row[1] << " "<< col[0] << " " << col[1] << "\n";
ll type_0 = row[0] * col[0] + row[1] * col[1];
ll type_1 = row[0] * col[1] + row[1] * col[0];
cnt[0][0] -= type_0, cnt[1][0] += type_0;
cnt[0][1] -= type_1, cnt[1][1] += type_1;
}
// cout << "size-square " << u << " " << cnt[0][0] << " " << cnt[0][1] << " " << cnt[1][0] << " " << cnt[1][1] << "\n";
ans = min(ans, cnt[0][0] + cnt[1][1]);
ans = min(ans, cnt[1][0] + cnt[0][1]);
}
cout << ans << "\n";
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... |