#include <bits/stdc++.h>
#define MN 107171
using namespace std;
using ll = long long;
int 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(int l) {
ll arie = ((n / l) * (n / l) + 1) / 2 * l * l, selec, hash;
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";
}
Compilation message
chessboard.cpp: In function 'll sol(int)':
chessboard.cpp:26:59: warning: unused variable 'hash' [-Wunused-variable]
26 | ll arie = ((n / l) * (n / l) + 1) / 2 * l * l, selec, hash;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
66 ms |
1336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
216 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
2 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
2 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
216 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
2 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
2 ms |
204 KB |
Output is correct |
16 |
Correct |
32 ms |
840 KB |
Output is correct |
17 |
Correct |
70 ms |
2344 KB |
Output is correct |
18 |
Correct |
114 ms |
2360 KB |
Output is correct |
19 |
Correct |
435 ms |
2544 KB |
Output is correct |
20 |
Correct |
483 ms |
2456 KB |
Output is correct |
21 |
Correct |
70 ms |
2360 KB |
Output is correct |
22 |
Correct |
2 ms |
204 KB |
Output is correct |
23 |
Correct |
74 ms |
1440 KB |
Output is correct |
24 |
Correct |
104 ms |
2396 KB |
Output is correct |
25 |
Correct |
17 ms |
640 KB |
Output is correct |
26 |
Correct |
68 ms |
1424 KB |
Output is correct |
27 |
Correct |
104 ms |
2496 KB |
Output is correct |
28 |
Correct |
105 ms |
2388 KB |
Output is correct |
29 |
Correct |
28 ms |
1340 KB |
Output is correct |
30 |
Correct |
4 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
66 ms |
1336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Incorrect |
66 ms |
1336 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |