#include <bits/stdc++.h>
using namespace std;
#define x1 x1_
#define x2 x2_
#define y1 y1_
#define y2 y2_
const int N = 1e5 + 5;
const int BLACK_FIRST = 0;
const int WHITE_FIRST = 1;
// color of a ceil will be determined by (id row + id col) % 2 and the color of first block (BLACK_FIRST / WHITE_FIRST)
// in particular: (id row + id col) % 2 = BLACK_FIRST --> black id, otherwise ...
// id row = row div edge
int n, k;
int x[2][N], y[2][N];
void read() {
cin >> n >> k;
for (int i = 1; i <= k; i++) {
for (int j = 0; j < 2; j++) {
cin >> x[j][i] >> y[j][i];
--x[j][i];
--y[j][i];
}
}
}
int id_block (int x, int edge) {
return x / edge;
}
// (0...x1) / k % 2 == type
int num_ceil (int x, int k, int type) {
if (x < 0) {
return 0;
}
if (x == 0) {
return (x / k) % 2 == type;
}
int block_x = id_block(x, k);
int ans = 0;
if (block_x > 0) {
// [0, block_x - 1]
if (type == 0) {
ans += (block_x - block_x / 2) * k;
}
else {
ans += (block_x / 2) * k;
}
}
// just in block_x
int lo = block_x * k;
int hi = x;
if (block_x % 2 == type) {
ans += (hi - lo + 1);
}
return ans;
}
int num_ceil (int l, int r, int k, int type) {
assert(num_ceil(r, k, type) >= num_ceil(l - 1, k, type));
return num_ceil(r, k, type) - num_ceil(l - 1, k, type);
}
int64_t solve (int edge, int type) {
int64_t ans = 0;
// (r + c) % 2 == type
for (int row_type = 0; row_type < 2; row_type++) {
int col_type = (type - row_type + 2) % 2;
ans += 1ll * num_ceil(0, n - 1, edge, row_type) * num_ceil(0, n - 1, edge, col_type);
}
for (int i = 1; i <= k; i++) {
for (int row_type = 0; row_type < 2; row_type++) {
for (int col_type = 0; col_type < 2; col_type++) {
// already black
// since we colored it before --> subtract
if ((row_type + col_type) % 2 == type) {
ans -= 1ll * num_ceil(x[0][i], x[1][i], edge, row_type) * num_ceil(y[0][i], y[1][i], edge, col_type);
}
// must be white, but now it's black
else {
ans += 1ll * num_ceil(x[0][i], x[1][i], edge, row_type) * num_ceil(y[0][i], y[1][i], edge, col_type);
}
}
}
}
return ans;
}
void solve() {
int64_t best = 9e18;
for (int edge = 1; edge < n; edge++) {
if (n % edge) {
continue;
}
best = min(best, solve(edge, BLACK_FIRST));
best = min(best, solve(edge, WHITE_FIRST));
}
cout << best;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
read();
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
2780 KB |
Output is correct |
2 |
Correct |
10 ms |
884 KB |
Output is correct |
3 |
Correct |
23 ms |
1876 KB |
Output is correct |
4 |
Correct |
22 ms |
1900 KB |
Output is correct |
5 |
Correct |
28 ms |
2464 KB |
Output is correct |
6 |
Correct |
18 ms |
1684 KB |
Output is correct |
7 |
Correct |
5 ms |
596 KB |
Output is correct |
8 |
Correct |
18 ms |
1620 KB |
Output is correct |
9 |
Correct |
51 ms |
3812 KB |
Output is correct |
10 |
Correct |
27 ms |
2260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
336 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
336 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
25 ms |
1192 KB |
Output is correct |
17 |
Correct |
38 ms |
2940 KB |
Output is correct |
18 |
Correct |
79 ms |
3340 KB |
Output is correct |
19 |
Correct |
415 ms |
3124 KB |
Output is correct |
20 |
Correct |
476 ms |
3452 KB |
Output is correct |
21 |
Correct |
34 ms |
2896 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
62 ms |
1620 KB |
Output is correct |
24 |
Correct |
69 ms |
3084 KB |
Output is correct |
25 |
Correct |
12 ms |
596 KB |
Output is correct |
26 |
Correct |
44 ms |
2128 KB |
Output is correct |
27 |
Correct |
75 ms |
2476 KB |
Output is correct |
28 |
Correct |
76 ms |
3284 KB |
Output is correct |
29 |
Correct |
19 ms |
1368 KB |
Output is correct |
30 |
Correct |
3 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
2780 KB |
Output is correct |
2 |
Correct |
10 ms |
884 KB |
Output is correct |
3 |
Correct |
23 ms |
1876 KB |
Output is correct |
4 |
Correct |
22 ms |
1900 KB |
Output is correct |
5 |
Correct |
28 ms |
2464 KB |
Output is correct |
6 |
Correct |
18 ms |
1684 KB |
Output is correct |
7 |
Correct |
5 ms |
596 KB |
Output is correct |
8 |
Correct |
18 ms |
1620 KB |
Output is correct |
9 |
Correct |
51 ms |
3812 KB |
Output is correct |
10 |
Correct |
27 ms |
2260 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
336 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
25 ms |
1192 KB |
Output is correct |
27 |
Correct |
38 ms |
2940 KB |
Output is correct |
28 |
Correct |
79 ms |
3340 KB |
Output is correct |
29 |
Correct |
415 ms |
3124 KB |
Output is correct |
30 |
Correct |
476 ms |
3452 KB |
Output is correct |
31 |
Correct |
34 ms |
2896 KB |
Output is correct |
32 |
Correct |
2 ms |
340 KB |
Output is correct |
33 |
Correct |
62 ms |
1620 KB |
Output is correct |
34 |
Correct |
69 ms |
3084 KB |
Output is correct |
35 |
Correct |
12 ms |
596 KB |
Output is correct |
36 |
Correct |
44 ms |
2128 KB |
Output is correct |
37 |
Correct |
75 ms |
2476 KB |
Output is correct |
38 |
Correct |
76 ms |
3284 KB |
Output is correct |
39 |
Correct |
19 ms |
1368 KB |
Output is correct |
40 |
Correct |
3 ms |
468 KB |
Output is correct |
41 |
Correct |
384 ms |
3604 KB |
Output is correct |
42 |
Correct |
89 ms |
4048 KB |
Output is correct |
43 |
Correct |
200 ms |
3660 KB |
Output is correct |
44 |
Correct |
79 ms |
3916 KB |
Output is correct |
45 |
Correct |
50 ms |
4200 KB |
Output is correct |
46 |
Correct |
435 ms |
4044 KB |
Output is correct |
47 |
Correct |
44 ms |
3820 KB |
Output is correct |
48 |
Correct |
138 ms |
3668 KB |
Output is correct |
49 |
Correct |
73 ms |
3656 KB |
Output is correct |
50 |
Execution timed out |
2056 ms |
3788 KB |
Time limit exceeded |
51 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
36 ms |
2780 KB |
Output is correct |
10 |
Correct |
10 ms |
884 KB |
Output is correct |
11 |
Correct |
23 ms |
1876 KB |
Output is correct |
12 |
Correct |
22 ms |
1900 KB |
Output is correct |
13 |
Correct |
28 ms |
2464 KB |
Output is correct |
14 |
Correct |
18 ms |
1684 KB |
Output is correct |
15 |
Correct |
5 ms |
596 KB |
Output is correct |
16 |
Correct |
18 ms |
1620 KB |
Output is correct |
17 |
Correct |
51 ms |
3812 KB |
Output is correct |
18 |
Correct |
27 ms |
2260 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
340 KB |
Output is correct |
32 |
Correct |
2 ms |
336 KB |
Output is correct |
33 |
Correct |
2 ms |
340 KB |
Output is correct |
34 |
Correct |
25 ms |
1192 KB |
Output is correct |
35 |
Correct |
38 ms |
2940 KB |
Output is correct |
36 |
Correct |
79 ms |
3340 KB |
Output is correct |
37 |
Correct |
415 ms |
3124 KB |
Output is correct |
38 |
Correct |
476 ms |
3452 KB |
Output is correct |
39 |
Correct |
34 ms |
2896 KB |
Output is correct |
40 |
Correct |
2 ms |
340 KB |
Output is correct |
41 |
Correct |
62 ms |
1620 KB |
Output is correct |
42 |
Correct |
69 ms |
3084 KB |
Output is correct |
43 |
Correct |
12 ms |
596 KB |
Output is correct |
44 |
Correct |
44 ms |
2128 KB |
Output is correct |
45 |
Correct |
75 ms |
2476 KB |
Output is correct |
46 |
Correct |
76 ms |
3284 KB |
Output is correct |
47 |
Correct |
19 ms |
1368 KB |
Output is correct |
48 |
Correct |
3 ms |
468 KB |
Output is correct |
49 |
Correct |
384 ms |
3604 KB |
Output is correct |
50 |
Correct |
89 ms |
4048 KB |
Output is correct |
51 |
Correct |
200 ms |
3660 KB |
Output is correct |
52 |
Correct |
79 ms |
3916 KB |
Output is correct |
53 |
Correct |
50 ms |
4200 KB |
Output is correct |
54 |
Correct |
435 ms |
4044 KB |
Output is correct |
55 |
Correct |
44 ms |
3820 KB |
Output is correct |
56 |
Correct |
138 ms |
3668 KB |
Output is correct |
57 |
Correct |
73 ms |
3656 KB |
Output is correct |
58 |
Execution timed out |
2056 ms |
3788 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |