#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> x1(m), y1(m), x2(m), y2(m);
for (int i = 0; i < m; i++) {
cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];
--x1[i]; --y1[i]; --x2[i]; --y2[i];
}
long long ans = 1e18;
for (int foo = 0; foo < 2; foo++) {
for (int k = 1; k < n; k++) {
if (n % k != 0) {
continue;
}
long long res = 0, area = 0;
function<long long(int, int, int, int)> Solve = [&](int x1, int y1, int x2, int y2) {
if (x1 > x2 || y1 > y2) {
return 0LL;
}
if ((x1 / k) == (x2 / k) && (y1 / k) == (y2 / k)) {
if (foo == (x1 / k + y1 / k) % 2) {
return 0LL;
} else {
return (x2 - x1 + 1) * 1LL * (y2 - y1 + 1);
}
}
if ((x1 / k) == (x2 / k)) {
long long ret = 0;
if (y1 % k != 0) {
ret += Solve(x1, y1, x2, y1 + (k - y1 % k) - 1);
y1 += (k - y1 % k);
}
if (y2 % k != k - 1) {
ret += Solve(x1, y2 - y2 % k, x2, y2);
y2 -= (y2 % k) + 1;
}
long long blocks = (y2 - y1 + 1) / k;
if ((x1 / k + y1 / k) % 2 == foo) {
blocks = blocks / 2;
} else {
blocks = (blocks + 1) / 2;
}
return ret + blocks * k * (x2 - x1 + 1);
}
if ((y1 / k) == (y2 / k)) {
long long ret = 0;
if (x1 % k != 0) {
ret += Solve(x1, y1, x1 + (k - x1 % k) - 1, y2);
x1 += (k - x1 % k);
}
if (x2 % k != k - 1) {
ret += Solve(x2 - x2 % k, y1, x2, y2);
x2 -= (x2 % k) + 1;
}
long long blocks = (x2 - x1 + 1) / k;
if ((x1 / k + y1 / k) % 2 == foo) {
blocks = blocks / 2;
} else {
blocks = (blocks + 1) / 2;
}
return ret + blocks * k * (y2 - y1 + 1);
}
long long ret = 0;
if (x1 % k != 0) {
ret += Solve(x1, y1, x1 + (k - x1 % k) - 1, y2);
x1 += (k - x1 % k);
}
if (x2 % k != k - 1) {
ret += Solve(x2 - x2 % k, y1, x2, y2);
x2 -= (x2 % k) + 1;
}
if (y1 % k != 0) {
ret += Solve(x1, y1, x2, y1 + (k - y1 % k) - 1);
y1 += (k - y1 % k);
}
if (y2 % k != k - 1) {
ret += Solve(x1, y2 - (y2 % k), x2, y2);
y2 -= (y2 % k) + 1;
}
long long blocks_x = (x2 - x1 + 1) / k;
long long blocks_y = (y2 - y1 + 1) / k;
if ((x1 / k + y1 / k) % 2 == foo) {
return ret + ((blocks_y + 1) / 2) * (blocks_x / 2) * k * k + (blocks_y / 2) * ((blocks_x + 1) / 2) * k * k;
} else {
return ret + ((blocks_y + 1) / 2) * ((blocks_x + 1) / 2) * k * k + (blocks_y / 2) * (blocks_x / 2) * k * k;
}
};
for (int i = 0; i < m; i++) {
res += Solve(x1[i], y1[i], x2[i], y2[i]);
area += (x2[i] - x1[i] + 1) * 1LL * (y2[i] - y1[i] + 1);
}
long long black = 0;
long long v = n / k;
if (foo == 0) {
black = ((v + 1) / 2) * ((v + 1) / 2) * k * k + (v / 2) * (v / 2) * k * k;
} else {
black = ((v + 1) / 2) * (v / 2) * k * k + (v / 2) * ((v + 1) / 2) * k * k;
}
ans = min(ans, res + black - (area - res));
}
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
1372 KB |
Output is correct |
2 |
Correct |
5 ms |
604 KB |
Output is correct |
3 |
Correct |
13 ms |
1092 KB |
Output is correct |
4 |
Correct |
13 ms |
1112 KB |
Output is correct |
5 |
Correct |
17 ms |
1328 KB |
Output is correct |
6 |
Correct |
11 ms |
860 KB |
Output is correct |
7 |
Correct |
3 ms |
348 KB |
Output is correct |
8 |
Correct |
11 ms |
860 KB |
Output is correct |
9 |
Correct |
27 ms |
1888 KB |
Output is correct |
10 |
Correct |
15 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
504 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
504 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
10 ms |
856 KB |
Output is correct |
17 |
Correct |
21 ms |
1628 KB |
Output is correct |
18 |
Correct |
34 ms |
2036 KB |
Output is correct |
19 |
Correct |
140 ms |
1840 KB |
Output is correct |
20 |
Correct |
154 ms |
2000 KB |
Output is correct |
21 |
Correct |
20 ms |
1628 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
24 ms |
1140 KB |
Output is correct |
24 |
Correct |
40 ms |
1852 KB |
Output is correct |
25 |
Correct |
5 ms |
348 KB |
Output is correct |
26 |
Correct |
20 ms |
1116 KB |
Output is correct |
27 |
Correct |
31 ms |
1372 KB |
Output is correct |
28 |
Correct |
33 ms |
1884 KB |
Output is correct |
29 |
Correct |
9 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
1372 KB |
Output is correct |
2 |
Correct |
5 ms |
604 KB |
Output is correct |
3 |
Correct |
13 ms |
1092 KB |
Output is correct |
4 |
Correct |
13 ms |
1112 KB |
Output is correct |
5 |
Correct |
17 ms |
1328 KB |
Output is correct |
6 |
Correct |
11 ms |
860 KB |
Output is correct |
7 |
Correct |
3 ms |
348 KB |
Output is correct |
8 |
Correct |
11 ms |
860 KB |
Output is correct |
9 |
Correct |
27 ms |
1888 KB |
Output is correct |
10 |
Correct |
15 ms |
1116 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
504 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
10 ms |
856 KB |
Output is correct |
27 |
Correct |
21 ms |
1628 KB |
Output is correct |
28 |
Correct |
34 ms |
2036 KB |
Output is correct |
29 |
Correct |
140 ms |
1840 KB |
Output is correct |
30 |
Correct |
154 ms |
2000 KB |
Output is correct |
31 |
Correct |
20 ms |
1628 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
24 ms |
1140 KB |
Output is correct |
34 |
Correct |
40 ms |
1852 KB |
Output is correct |
35 |
Correct |
5 ms |
348 KB |
Output is correct |
36 |
Correct |
20 ms |
1116 KB |
Output is correct |
37 |
Correct |
31 ms |
1372 KB |
Output is correct |
38 |
Correct |
33 ms |
1884 KB |
Output is correct |
39 |
Correct |
9 ms |
860 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
125 ms |
1628 KB |
Output is correct |
42 |
Correct |
38 ms |
1880 KB |
Output is correct |
43 |
Correct |
70 ms |
1628 KB |
Output is correct |
44 |
Correct |
37 ms |
1884 KB |
Output is correct |
45 |
Correct |
29 ms |
1880 KB |
Output is correct |
46 |
Correct |
139 ms |
1884 KB |
Output is correct |
47 |
Correct |
26 ms |
1628 KB |
Output is correct |
48 |
Correct |
55 ms |
1628 KB |
Output is correct |
49 |
Correct |
34 ms |
1624 KB |
Output is correct |
50 |
Correct |
636 ms |
2136 KB |
Output is correct |
51 |
Correct |
684 ms |
1964 KB |
Output is correct |
52 |
Correct |
643 ms |
1852 KB |
Output is correct |
53 |
Correct |
675 ms |
1956 KB |
Output is correct |
54 |
Correct |
625 ms |
1840 KB |
Output is correct |
55 |
Correct |
702 ms |
1884 KB |
Output is correct |
56 |
Correct |
608 ms |
1812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
20 ms |
1372 KB |
Output is correct |
10 |
Correct |
5 ms |
604 KB |
Output is correct |
11 |
Correct |
13 ms |
1092 KB |
Output is correct |
12 |
Correct |
13 ms |
1112 KB |
Output is correct |
13 |
Correct |
17 ms |
1328 KB |
Output is correct |
14 |
Correct |
11 ms |
860 KB |
Output is correct |
15 |
Correct |
3 ms |
348 KB |
Output is correct |
16 |
Correct |
11 ms |
860 KB |
Output is correct |
17 |
Correct |
27 ms |
1888 KB |
Output is correct |
18 |
Correct |
15 ms |
1116 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
504 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
10 ms |
856 KB |
Output is correct |
35 |
Correct |
21 ms |
1628 KB |
Output is correct |
36 |
Correct |
34 ms |
2036 KB |
Output is correct |
37 |
Correct |
140 ms |
1840 KB |
Output is correct |
38 |
Correct |
154 ms |
2000 KB |
Output is correct |
39 |
Correct |
20 ms |
1628 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
24 ms |
1140 KB |
Output is correct |
42 |
Correct |
40 ms |
1852 KB |
Output is correct |
43 |
Correct |
5 ms |
348 KB |
Output is correct |
44 |
Correct |
20 ms |
1116 KB |
Output is correct |
45 |
Correct |
31 ms |
1372 KB |
Output is correct |
46 |
Correct |
33 ms |
1884 KB |
Output is correct |
47 |
Correct |
9 ms |
860 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
125 ms |
1628 KB |
Output is correct |
50 |
Correct |
38 ms |
1880 KB |
Output is correct |
51 |
Correct |
70 ms |
1628 KB |
Output is correct |
52 |
Correct |
37 ms |
1884 KB |
Output is correct |
53 |
Correct |
29 ms |
1880 KB |
Output is correct |
54 |
Correct |
139 ms |
1884 KB |
Output is correct |
55 |
Correct |
26 ms |
1628 KB |
Output is correct |
56 |
Correct |
55 ms |
1628 KB |
Output is correct |
57 |
Correct |
34 ms |
1624 KB |
Output is correct |
58 |
Correct |
636 ms |
2136 KB |
Output is correct |
59 |
Correct |
684 ms |
1964 KB |
Output is correct |
60 |
Correct |
643 ms |
1852 KB |
Output is correct |
61 |
Correct |
675 ms |
1956 KB |
Output is correct |
62 |
Correct |
625 ms |
1840 KB |
Output is correct |
63 |
Correct |
702 ms |
1884 KB |
Output is correct |
64 |
Correct |
608 ms |
1812 KB |
Output is correct |
65 |
Correct |
1 ms |
600 KB |
Output is correct |
66 |
Correct |
0 ms |
344 KB |
Output is correct |
67 |
Correct |
1202 ms |
2132 KB |
Output is correct |
68 |
Correct |
1201 ms |
1896 KB |
Output is correct |
69 |
Correct |
1012 ms |
1708 KB |
Output is correct |
70 |
Correct |
1000 ms |
1880 KB |
Output is correct |
71 |
Correct |
1079 ms |
4016 KB |
Output is correct |
72 |
Correct |
1047 ms |
3912 KB |
Output is correct |
73 |
Correct |
1112 ms |
3804 KB |
Output is correct |
74 |
Correct |
1189 ms |
4088 KB |
Output is correct |
75 |
Correct |
1102 ms |
3952 KB |
Output is correct |
76 |
Correct |
1200 ms |
4180 KB |
Output is correct |
77 |
Correct |
187 ms |
4360 KB |
Output is correct |
78 |
Correct |
50 ms |
3940 KB |
Output is correct |
79 |
Correct |
126 ms |
3788 KB |
Output is correct |
80 |
Correct |
131 ms |
3920 KB |
Output is correct |
81 |
Correct |
135 ms |
3668 KB |
Output is correct |
82 |
Correct |
97 ms |
3932 KB |
Output is correct |
83 |
Correct |
85 ms |
3960 KB |
Output is correct |
84 |
Correct |
719 ms |
4272 KB |
Output is correct |
85 |
Correct |
1232 ms |
4432 KB |
Output is correct |
86 |
Correct |
4 ms |
348 KB |
Output is correct |
87 |
Correct |
1 ms |
348 KB |
Output is correct |
88 |
Correct |
1238 ms |
4412 KB |
Output is correct |
89 |
Correct |
280 ms |
1116 KB |
Output is correct |
90 |
Correct |
1 ms |
344 KB |
Output is correct |