#include <bits/stdc++.h>
using namespace std;
const int N = 4107, L = 26, K = 66, MOD = 1000000007;
int main() {
// freopen("input.txt", "r", stdin);
// ios::sync_with_stdio(false); cin.tie(0);
int n, k;
scanf("%d%d", &n, &k);
vector < pair <int, int> > l(k), r(k);
long long sq = 0;
for (int i = 0; i < k; ++i) {
scanf("%d%d%d%d", &l[i].first, &l[i].second, &r[i].first, &r[i].second);
sq += 1LL * (r[i].first - l[i].first + 1) * (r[i].second - l[i].second + 1);
}
auto ways = [&](int l, int r, int d, int s) {
//return ((r - 1) / (2 * d) + max((r - 1) % (2 * d) - d, 0)) - (l > 1 ? (l - 2) / (2 * d) + max((l - 2) % (2 * d) - d, 0));
int cnt = 0;
for (int i = l; i <= r; ++i) {
cnt += (i - 1) / d % 2 == s;
}
return cnt;
};
auto calc = [&](int d, int s) {
long long ret = 0;
for (int i = 0; i < k; ++i) {
//lx[i] <= x <= rx[i] and ly[i] <= y <= ry[i] and (x-1)/d % 2 == 0 and (y-1)/d % 2 == 0
ret += 1LL * ways(l[i].first, r[i].first, d, s) * ways(l[i].second, r[i].second, d, s);
}
return (sq - ret) + 1LL * (1LL * (n / d) * (n / d) + 1 - s) / 2 * (1LL * d * d) - ret;
// min + 1LL * n * n - 1LL * (1LL * (n / d) * (n / d) + 1 - s) / 2 - (sq - ret) + ret;
};
long long ans = LLONG_MAX / 2;
for (int d = 1; d * d <= n; ++d) {
if (n % d == 0) {
ans = min(ans, calc(d, 0));
ans = min(ans, calc(d, 1));
if (d * d != n && d != 1) ans = min(ans, calc(n / d, 0));
if (d * d != n && d != 1) ans = min(ans, calc(n / d, 1));
}
}
cout << ans << '\n';
return 0;
}
Compilation message
chessboard.cpp: In function 'int main()':
chessboard.cpp:11:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
11 | scanf("%d%d", &n, &k);
| ~~~~~^~~~~~~~~~~~~~~~
chessboard.cpp:15:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | scanf("%d%d%d%d", &l[i].first, &l[i].second, &r[i].first, &r[i].second);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
500 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
1 ms |
432 KB |
Output is correct |
4 |
Correct |
0 ms |
600 KB |
Output is correct |
5 |
Correct |
1 ms |
500 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
19 ms |
2920 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
352 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
352 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
19 ms |
2920 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
500 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
1 ms |
432 KB |
Output is correct |
4 |
Correct |
0 ms |
600 KB |
Output is correct |
5 |
Correct |
1 ms |
500 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Incorrect |
19 ms |
2920 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |