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 K = 1e5 + 10, SQ = 700;
int n, k;
int x1[K], Y1[K], x2[K], y2[K];
vector<int> divs;
long long sum[SQ], P;
long long get(int x, int y, int k) {
if(x == 0 || y == 0) {
return 0;
}
int X = (x / k) * k, Y = (y / k) * k;
long long ans = ((X / k) * 1LL * (Y / k) + 1) / 2 * k * k;
// cout << k << "\n";
// cout << x << " " << y << "\n";
// cout << X << " " << Y << "\n";
// cout << ans << "\n";
ans += (x - X) * 1LL * k * (((Y / k) + ((X / k) % 2 == 0)) / 2);
// cout << ans << "\n";
ans += (y - Y) * 1LL * k * (((X / k) + ((Y / k) % 2 == 0)) / 2);
// cout << ans << "\n";
if((X / k + Y / k) % 2 == 0) {
ans += (x - X) * 1LL * (y - Y);
}
return ans;
}
long long get(int x1, int Y1, int x2, int y2, int k) {
return get(x2, y2, k) - get(x2, Y1 - 1, k) - get(x1 - 1, y2, k) + get(x1 - 1, Y1 - 1, k);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for(int i = 0; i < k; i++) {
cin >> x1[i] >> Y1[i] >> x2[i] >> y2[i];
P += (x2[i] - x1[i] + 1) * 1LL * (y2[i] - Y1[i] + 1);
}
divs.push_back(1);
for(int i = 2; i * i < n; i++) {
if(n % i == 0) {
divs.push_back(i);
divs.push_back(n / i);
}
}
if(n % (int)sqrt(n) == 0) {
divs.push_back(sqrt(n));
}
sort(divs.begin(), divs.end());
for(int i = 0; i < k; i++) {
for(int d = 0; d < divs.size(); d++) {
sum[d] += get(x1[i], Y1[i], x2[i], y2[i], divs[d]);
// cout << x1[i] << " " << Y1[i] << " " << x2[i] << " " << y2[i] << " " << divs[d] << " " << get(x1[i], Y1[i], x2[i], y2[i], divs[d]) << "\n";
}
}
long long ans = 1e18;
// cout << P << "\n\n";
for(int i = 0; i < divs.size(); i++) {
long long all = (((n / divs[i]) * 1LL * (n / divs[i]) + 1) / 2) * divs[i] * divs[i];
ans = min(ans, all + P - 2 * sum[i]);
// cout << i << ": " << ans << "\n";
all = ((n / divs[i]) * 1LL * (n / divs[i]) / 2) * divs[i] * divs[i];
ans = min(ans, all - P + 2 * sum[i]);
// cout << i << ": " << ans << "\n";
}
cout << ans;
return 0;
}
/*
6 8
3 3 3 3
1 2 1 2
3 4 3 4
5 5 5 5
4 3 4 3
4 4 4 4
2 1 2 1
3 6 3 6
4 1
4 1 4 4
*/
Compilation message (stderr)
chessboard.cpp: In function 'int main()':
chessboard.cpp:55:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for(int d = 0; d < divs.size(); d++) {
| ~~^~~~~~~~~~~~~
chessboard.cpp:62:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for(int i = 0; i < divs.size(); i++) {
| ~~^~~~~~~~~~~~~
# | 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... |