#include <bits/stdc++.h>
#define FR(i, N) for (int i = 0; i < int(N); i++)
#define all(x) begin(x), end(x)
#define f first
#define s second
#define int ll
using namespace std;
using ll = long long;
ll calc(pair<pair<int, int>, pair<int, int>> rec, int d, int par) {
auto p = rec;
assert (p.f.f >= 0 && p.f.s >= 0 && p.s.f >= 0 && p.s.s >= 0);
if (p.f.f > p.s.f || p.f.s > p.s.s) {
return 0;
}
int x1 = p.f.f/d;
int x2 = p.s.f/d;
int y1 = p.f.s/d;
int y2 = p.s.s/d;
if (p.f.f%d == 0 && p.f.s%d == 0 && p.s.f%d == d-1 && p.s.s%d == d-1) {
int tot = (x2 - x1 + 1) * (y2 - y1 + 1);
if ((x1 + y1)%2 == par) {
//top left here is white
return (((x2 - x1 + 1)*(y2 - y1 + 1) + 1)/2)*d*d;
}
else {
return (tot - ((x2 - x1 + 1)*(y2 - y1 + 1) + 1)/2)*d*d;
}
}
else if (x1 == x2 && y1 == y2) {
if ((x1 + y1)%2 == par) {
return (p.s.f - p.f.f + 1) * (p.s.s - p.f.s + 1);
}
else {
return 0;
}
}
else if (p.f.f%d == 0 && p.s.f%d == d-1 && y1 == y2) {
int tot = (x2 - x1 + 1);
if ((x1 + y1)%2 == par) {
return (tot + 1)/2 * (p.s.s - p.f.s + 1) * d;
}
else {
return (tot - (tot + 1)/2) * (p.s.s - p.f.s + 1) * d;
}
}
else if (p.f.s%d == 0 && p.s.s%d == d-1 && x1 == x2) {
int tot = (y2 - y1 + 1);
if ((x1 + y1)%2 == par) {
return (tot + 1)/2 * (p.s.f - p.f.f + 1)*d;
}
else {
return (tot - (tot + 1)/2) * (p.s.f - p.f.f + 1)*d;
}
}
else if (x1 == x2) {
auto left = rec;
left.s.s = y1*d+d-1;
auto right = rec;
right.f.s = y2*d;
rec.f.s = left.s.s+1;
rec.s.s = right.f.s-1;
return calc(left, d, par) + calc(right, d, par) + calc(rec, d, par);
}
else if (y1 == y2) {
auto top = rec;
top.s.f = x1*d + d - 1;
auto bot = rec;
bot.f.f = x2*d;
rec.f.f = top.s.f+1;
rec.s.f = bot.f.f-1;
return calc(top, d, par) + calc(bot, d, par) + calc(rec, d, par);
}
else {
auto tleft = rec;
auto bleft = rec;
auto tright = rec;
auto bright = rec;
tleft.s.s = y1*d + d-1;
tleft.s.f = x1*d + d-1;
bleft.f.f = x2*d;
bleft.s.s = y1*d + d-1;
tright.f.s = y2*d;
tright.s.f = x1*d + d-1;
bright.f.f = x2*d;
bright.f.s = y2*d;
auto left = rec;
auto right = rec;
auto bot = rec;
auto top = rec;
left.f.f = tleft.s.f+1;
left.s.f = bleft.f.f-1;
left.s.s= y1*d + d-1;
right.f.f = tright.s.f+1;
right.s.f = bright.f.f-1;
right.f.s=y2*d;
bot.f.s = bleft.s.s+1;
bot.s.s=bright.f.s-1;
bot.f.f=x2*d;
top.f.s=tleft.s.s+1;
top.s.s=tright.f.s-1;
top.s.f=x1*d+d-1;
rec.f.f = d*(x1 + 1);
rec.f.s = d*(y1 + 1);
rec.s.f = d*x2-1;
rec.s.s = d*y2-1;
return calc(tleft, d, par) + calc(bleft, d, par) + calc(tright, d, par) + calc(bright, d, par) + calc(left, d, par) + calc(right, d, par) + calc(bot, d, par) + calc(top, d, par) + calc(rec, d, par);
}
}
signed main() {
cin.tie(0);
cin.sync_with_stdio(0);
// cerr << calc({{1, 5}, {8, 7}}, 3, 0) << endl;
int N, K;
cin >> N >> K;
vector<pair<pair<int, int>, pair<int, int>>> vec(K);
for (int i = 0; i < K; i++) {
cin >> vec[i].f.f >> vec[i].f.s >> vec[i].s.f >> vec[i].s.s;
vec[i].f.f--;
vec[i].f.s--;
vec[i].s.f--;
vec[i].s.s--;
}
int ans = N*N;
for (int d = 1; d < N; d++) {
if (N%d == 0) {
int cnt = N/d;
// ll white;
// ll black;
int black = d*d*(((cnt * cnt)+1)/2);
int white = d*d*(cnt*cnt - ((cnt*cnt)+1)/2);
for (auto& p : vec) {
int tot = (p.s.f - p.f.f + 1) * (p.s.s - p.f.s + 1);
int res1 = calc(p, d, 0);
int res2 = calc(p, d, 1);
black += res2;
black -= tot-res2;
white += res1;
white -= tot-res1;
}
ans = min({ans, white, black});
//top left is black
}
}
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
2284 KB |
Output is correct |
2 |
Correct |
8 ms |
876 KB |
Output is correct |
3 |
Correct |
18 ms |
1516 KB |
Output is correct |
4 |
Correct |
19 ms |
1772 KB |
Output is correct |
5 |
Correct |
25 ms |
2028 KB |
Output is correct |
6 |
Correct |
16 ms |
1388 KB |
Output is correct |
7 |
Correct |
4 ms |
620 KB |
Output is correct |
8 |
Correct |
16 ms |
1388 KB |
Output is correct |
9 |
Correct |
40 ms |
3180 KB |
Output is correct |
10 |
Correct |
23 ms |
1900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
15 ms |
1132 KB |
Output is correct |
17 |
Correct |
32 ms |
2924 KB |
Output is correct |
18 |
Correct |
48 ms |
3436 KB |
Output is correct |
19 |
Correct |
154 ms |
3052 KB |
Output is correct |
20 |
Correct |
174 ms |
3436 KB |
Output is correct |
21 |
Correct |
31 ms |
2924 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
30 ms |
1644 KB |
Output is correct |
24 |
Correct |
44 ms |
3180 KB |
Output is correct |
25 |
Correct |
7 ms |
620 KB |
Output is correct |
26 |
Correct |
29 ms |
2176 KB |
Output is correct |
27 |
Correct |
40 ms |
2412 KB |
Output is correct |
28 |
Correct |
47 ms |
3328 KB |
Output is correct |
29 |
Correct |
13 ms |
1388 KB |
Output is correct |
30 |
Correct |
2 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
2284 KB |
Output is correct |
2 |
Correct |
8 ms |
876 KB |
Output is correct |
3 |
Correct |
18 ms |
1516 KB |
Output is correct |
4 |
Correct |
19 ms |
1772 KB |
Output is correct |
5 |
Correct |
25 ms |
2028 KB |
Output is correct |
6 |
Correct |
16 ms |
1388 KB |
Output is correct |
7 |
Correct |
4 ms |
620 KB |
Output is correct |
8 |
Correct |
16 ms |
1388 KB |
Output is correct |
9 |
Correct |
40 ms |
3180 KB |
Output is correct |
10 |
Correct |
23 ms |
1900 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
15 ms |
1132 KB |
Output is correct |
27 |
Correct |
32 ms |
2924 KB |
Output is correct |
28 |
Correct |
48 ms |
3436 KB |
Output is correct |
29 |
Correct |
154 ms |
3052 KB |
Output is correct |
30 |
Correct |
174 ms |
3436 KB |
Output is correct |
31 |
Correct |
31 ms |
2924 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
30 ms |
1644 KB |
Output is correct |
34 |
Correct |
44 ms |
3180 KB |
Output is correct |
35 |
Correct |
7 ms |
620 KB |
Output is correct |
36 |
Correct |
29 ms |
2176 KB |
Output is correct |
37 |
Correct |
40 ms |
2412 KB |
Output is correct |
38 |
Correct |
47 ms |
3328 KB |
Output is correct |
39 |
Correct |
13 ms |
1388 KB |
Output is correct |
40 |
Correct |
2 ms |
364 KB |
Output is correct |
41 |
Correct |
142 ms |
2924 KB |
Output is correct |
42 |
Correct |
53 ms |
3308 KB |
Output is correct |
43 |
Correct |
85 ms |
2924 KB |
Output is correct |
44 |
Correct |
51 ms |
3180 KB |
Output is correct |
45 |
Correct |
43 ms |
3436 KB |
Output is correct |
46 |
Correct |
159 ms |
3180 KB |
Output is correct |
47 |
Correct |
38 ms |
3052 KB |
Output is correct |
48 |
Correct |
68 ms |
3052 KB |
Output is correct |
49 |
Correct |
47 ms |
2924 KB |
Output is correct |
50 |
Correct |
695 ms |
3180 KB |
Output is correct |
51 |
Correct |
749 ms |
3308 KB |
Output is correct |
52 |
Correct |
689 ms |
3180 KB |
Output is correct |
53 |
Correct |
736 ms |
3308 KB |
Output is correct |
54 |
Correct |
686 ms |
3052 KB |
Output is correct |
55 |
Correct |
766 ms |
3436 KB |
Output is correct |
56 |
Correct |
660 ms |
3052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
29 ms |
2284 KB |
Output is correct |
10 |
Correct |
8 ms |
876 KB |
Output is correct |
11 |
Correct |
18 ms |
1516 KB |
Output is correct |
12 |
Correct |
19 ms |
1772 KB |
Output is correct |
13 |
Correct |
25 ms |
2028 KB |
Output is correct |
14 |
Correct |
16 ms |
1388 KB |
Output is correct |
15 |
Correct |
4 ms |
620 KB |
Output is correct |
16 |
Correct |
16 ms |
1388 KB |
Output is correct |
17 |
Correct |
40 ms |
3180 KB |
Output is correct |
18 |
Correct |
23 ms |
1900 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
15 ms |
1132 KB |
Output is correct |
35 |
Correct |
32 ms |
2924 KB |
Output is correct |
36 |
Correct |
48 ms |
3436 KB |
Output is correct |
37 |
Correct |
154 ms |
3052 KB |
Output is correct |
38 |
Correct |
174 ms |
3436 KB |
Output is correct |
39 |
Correct |
31 ms |
2924 KB |
Output is correct |
40 |
Correct |
1 ms |
364 KB |
Output is correct |
41 |
Correct |
30 ms |
1644 KB |
Output is correct |
42 |
Correct |
44 ms |
3180 KB |
Output is correct |
43 |
Correct |
7 ms |
620 KB |
Output is correct |
44 |
Correct |
29 ms |
2176 KB |
Output is correct |
45 |
Correct |
40 ms |
2412 KB |
Output is correct |
46 |
Correct |
47 ms |
3328 KB |
Output is correct |
47 |
Correct |
13 ms |
1388 KB |
Output is correct |
48 |
Correct |
2 ms |
364 KB |
Output is correct |
49 |
Correct |
142 ms |
2924 KB |
Output is correct |
50 |
Correct |
53 ms |
3308 KB |
Output is correct |
51 |
Correct |
85 ms |
2924 KB |
Output is correct |
52 |
Correct |
51 ms |
3180 KB |
Output is correct |
53 |
Correct |
43 ms |
3436 KB |
Output is correct |
54 |
Correct |
159 ms |
3180 KB |
Output is correct |
55 |
Correct |
38 ms |
3052 KB |
Output is correct |
56 |
Correct |
68 ms |
3052 KB |
Output is correct |
57 |
Correct |
47 ms |
2924 KB |
Output is correct |
58 |
Correct |
695 ms |
3180 KB |
Output is correct |
59 |
Correct |
749 ms |
3308 KB |
Output is correct |
60 |
Correct |
689 ms |
3180 KB |
Output is correct |
61 |
Correct |
736 ms |
3308 KB |
Output is correct |
62 |
Correct |
686 ms |
3052 KB |
Output is correct |
63 |
Correct |
766 ms |
3436 KB |
Output is correct |
64 |
Correct |
660 ms |
3052 KB |
Output is correct |
65 |
Correct |
0 ms |
364 KB |
Output is correct |
66 |
Correct |
0 ms |
364 KB |
Output is correct |
67 |
Correct |
1234 ms |
3308 KB |
Output is correct |
68 |
Correct |
1222 ms |
3308 KB |
Output is correct |
69 |
Correct |
1035 ms |
2924 KB |
Output is correct |
70 |
Correct |
1057 ms |
5356 KB |
Output is correct |
71 |
Correct |
1114 ms |
5228 KB |
Output is correct |
72 |
Correct |
1085 ms |
5100 KB |
Output is correct |
73 |
Correct |
1134 ms |
4972 KB |
Output is correct |
74 |
Correct |
1213 ms |
5484 KB |
Output is correct |
75 |
Correct |
1150 ms |
5228 KB |
Output is correct |
76 |
Correct |
1220 ms |
5484 KB |
Output is correct |
77 |
Correct |
201 ms |
5772 KB |
Output is correct |
78 |
Correct |
58 ms |
5228 KB |
Output is correct |
79 |
Correct |
143 ms |
4972 KB |
Output is correct |
80 |
Correct |
148 ms |
5228 KB |
Output is correct |
81 |
Correct |
146 ms |
4972 KB |
Output is correct |
82 |
Correct |
109 ms |
5484 KB |
Output is correct |
83 |
Correct |
100 ms |
5100 KB |
Output is correct |
84 |
Correct |
764 ms |
5712 KB |
Output is correct |
85 |
Correct |
1266 ms |
5740 KB |
Output is correct |
86 |
Correct |
4 ms |
364 KB |
Output is correct |
87 |
Correct |
1 ms |
364 KB |
Output is correct |
88 |
Correct |
1279 ms |
5808 KB |
Output is correct |
89 |
Correct |
285 ms |
1388 KB |
Output is correct |
90 |
Correct |
1 ms |
364 KB |
Output is correct |