#include <bits/stdc++.h>
using namespace std;
#define FOR(i, x, y) for (int i = x; i < y; i++)
#define ll long long
struct rect{ int x1, y1, x2, y2; };
ll n, k, ans = 1e18; vector<rect> A;
void solve(ll sz, int T){
ll bl = (n / sz) * (n / sz) / 2;
if ((n / sz) % 2) bl += T;
ll res = sz * sz * bl;
auto eval = [&](int x1, int y1, int x2, int y2, ll area){
int dX = x2 - x1 + 1;
int dY = y2 - y1 + 1;
if (!dX or !dY) return 0LL;
ll L = (dY - 1) / sz + 1; //ceil div to take care of smoll square
ll H = (dX - 1) / sz + 1;
ll black = L * H / 2;
//odd x odd dimension will have one extra of white/black
int a = (x1 / sz) % 2, b = (y1 / sz) % 2;
if (L % 2 and H % 2) black += (a ^ b ^ T); //T = 1 means start is black
ll white = L * H - black;
return black * area - white * area; //color operations saved
};
for (auto [x1, y1, x2, y2] : A){
ll VU = sz - (x1 % sz);
ll VD = (x2 % sz) + 1;
ll HL = sz - (y1 % sz);
ll HR = (y2 % sz) + 1;
//small dimension cases
if (VU + VD > x2 - x1 + 1) VU = x2 - x1 + 1, VD = 0;
if (HL + HR > y2 - y1 + 1) HL = y2 - y1 + 1, HR = 0;
//inner rect
res -= eval(x1 + VU, y1 + HL, x2 - VD, y2 - HR, (ll)sz * sz);
//corner rec
res -= eval(x1, y1, x1 + VU - 1, y1 + HL - 1, HL * VU); //top left
res -= eval(x2 - VD + 1, y1, x2, y1 + HL - 1, VD * HL); //bottom left
res -= eval(x1, y2 - HR + 1, x1 + VU - 1, y2, HR * VU); //top right
res -= eval(x2 - VD + 1, y2 - HR + 1, x2, y2, VD * HR); //bottom right
//side rec
res -= eval(x1 + VU, y1, x2 - VD, y1 + HL - 1, sz * HL); //left
res -= eval(x1, y1 + HL, x1 + VU - 1, y2 - HR, sz * VU); //up
res -= eval(x1 + VU, y2 - HR + 1, x2 - VD, y2, sz * HR); //right
res -= eval(x2 - VD + 1, y1 + HL, x2, y2 - HR, sz * VD); //down
}
ans = min(ans, res);
}
int main(){
cin >> n >> k; A.resize(k);
for (auto &i : A){
cin >> i.x1 >> i.y1 >> i.x2 >> i.y2;
i.x1--; i.y1--; i.x2--; i.y2--; //0 indexed makes mod ops easier
}
FOR(i, 2, n + 1) if (!(n % i)) solve(n / i, 0), solve(n / i, 1);
cout<<ans<<endl;
}
Compilation message
chessboard.cpp: In function 'void solve(long long int, int)':
chessboard.cpp:34:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
34 | for (auto [x1, y1, x2, y2] : A){
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
288 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
2100 KB |
Output is correct |
2 |
Correct |
20 ms |
916 KB |
Output is correct |
3 |
Correct |
44 ms |
1768 KB |
Output is correct |
4 |
Correct |
44 ms |
1760 KB |
Output is correct |
5 |
Correct |
72 ms |
2036 KB |
Output is correct |
6 |
Correct |
39 ms |
1588 KB |
Output is correct |
7 |
Correct |
9 ms |
460 KB |
Output is correct |
8 |
Correct |
43 ms |
1596 KB |
Output is correct |
9 |
Correct |
96 ms |
2552 KB |
Output is correct |
10 |
Correct |
54 ms |
1852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
204 KB |
Output is correct |
5 |
Correct |
2 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
2 ms |
300 KB |
Output is correct |
8 |
Correct |
1 ms |
256 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 |
308 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
2 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
204 KB |
Output is correct |
5 |
Correct |
2 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
2 ms |
300 KB |
Output is correct |
8 |
Correct |
1 ms |
256 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 |
308 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
2 ms |
204 KB |
Output is correct |
16 |
Correct |
27 ms |
1100 KB |
Output is correct |
17 |
Correct |
71 ms |
2444 KB |
Output is correct |
18 |
Correct |
97 ms |
2672 KB |
Output is correct |
19 |
Correct |
216 ms |
2560 KB |
Output is correct |
20 |
Correct |
236 ms |
2628 KB |
Output is correct |
21 |
Correct |
70 ms |
2444 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
54 ms |
1628 KB |
Output is correct |
24 |
Correct |
94 ms |
2660 KB |
Output is correct |
25 |
Correct |
10 ms |
576 KB |
Output is correct |
26 |
Correct |
57 ms |
2048 KB |
Output is correct |
27 |
Correct |
75 ms |
2236 KB |
Output is correct |
28 |
Correct |
93 ms |
2676 KB |
Output is correct |
29 |
Correct |
28 ms |
1228 KB |
Output is correct |
30 |
Correct |
3 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
2100 KB |
Output is correct |
2 |
Correct |
20 ms |
916 KB |
Output is correct |
3 |
Correct |
44 ms |
1768 KB |
Output is correct |
4 |
Correct |
44 ms |
1760 KB |
Output is correct |
5 |
Correct |
72 ms |
2036 KB |
Output is correct |
6 |
Correct |
39 ms |
1588 KB |
Output is correct |
7 |
Correct |
9 ms |
460 KB |
Output is correct |
8 |
Correct |
43 ms |
1596 KB |
Output is correct |
9 |
Correct |
96 ms |
2552 KB |
Output is correct |
10 |
Correct |
54 ms |
1852 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
2 ms |
204 KB |
Output is correct |
15 |
Correct |
2 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
2 ms |
300 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
2 ms |
308 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
2 ms |
204 KB |
Output is correct |
26 |
Correct |
27 ms |
1100 KB |
Output is correct |
27 |
Correct |
71 ms |
2444 KB |
Output is correct |
28 |
Correct |
97 ms |
2672 KB |
Output is correct |
29 |
Correct |
216 ms |
2560 KB |
Output is correct |
30 |
Correct |
236 ms |
2628 KB |
Output is correct |
31 |
Correct |
70 ms |
2444 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
54 ms |
1628 KB |
Output is correct |
34 |
Correct |
94 ms |
2660 KB |
Output is correct |
35 |
Correct |
10 ms |
576 KB |
Output is correct |
36 |
Correct |
57 ms |
2048 KB |
Output is correct |
37 |
Correct |
75 ms |
2236 KB |
Output is correct |
38 |
Correct |
93 ms |
2676 KB |
Output is correct |
39 |
Correct |
28 ms |
1228 KB |
Output is correct |
40 |
Correct |
3 ms |
332 KB |
Output is correct |
41 |
Correct |
209 ms |
2436 KB |
Output is correct |
42 |
Correct |
114 ms |
2644 KB |
Output is correct |
43 |
Correct |
142 ms |
2368 KB |
Output is correct |
44 |
Correct |
110 ms |
2544 KB |
Output is correct |
45 |
Correct |
110 ms |
2676 KB |
Output is correct |
46 |
Correct |
232 ms |
2624 KB |
Output is correct |
47 |
Correct |
92 ms |
2476 KB |
Output is correct |
48 |
Correct |
131 ms |
2536 KB |
Output is correct |
49 |
Correct |
106 ms |
2400 KB |
Output is correct |
50 |
Correct |
814 ms |
2672 KB |
Output is correct |
51 |
Correct |
890 ms |
2692 KB |
Output is correct |
52 |
Correct |
818 ms |
2608 KB |
Output is correct |
53 |
Correct |
897 ms |
2724 KB |
Output is correct |
54 |
Correct |
811 ms |
2660 KB |
Output is correct |
55 |
Correct |
899 ms |
2696 KB |
Output is correct |
56 |
Correct |
769 ms |
2588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
288 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
69 ms |
2100 KB |
Output is correct |
10 |
Correct |
20 ms |
916 KB |
Output is correct |
11 |
Correct |
44 ms |
1768 KB |
Output is correct |
12 |
Correct |
44 ms |
1760 KB |
Output is correct |
13 |
Correct |
72 ms |
2036 KB |
Output is correct |
14 |
Correct |
39 ms |
1588 KB |
Output is correct |
15 |
Correct |
9 ms |
460 KB |
Output is correct |
16 |
Correct |
43 ms |
1596 KB |
Output is correct |
17 |
Correct |
96 ms |
2552 KB |
Output is correct |
18 |
Correct |
54 ms |
1852 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
2 ms |
204 KB |
Output is correct |
23 |
Correct |
2 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
2 ms |
300 KB |
Output is correct |
26 |
Correct |
1 ms |
256 KB |
Output is correct |
27 |
Correct |
1 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
2 ms |
308 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
2 ms |
204 KB |
Output is correct |
34 |
Correct |
27 ms |
1100 KB |
Output is correct |
35 |
Correct |
71 ms |
2444 KB |
Output is correct |
36 |
Correct |
97 ms |
2672 KB |
Output is correct |
37 |
Correct |
216 ms |
2560 KB |
Output is correct |
38 |
Correct |
236 ms |
2628 KB |
Output is correct |
39 |
Correct |
70 ms |
2444 KB |
Output is correct |
40 |
Correct |
1 ms |
204 KB |
Output is correct |
41 |
Correct |
54 ms |
1628 KB |
Output is correct |
42 |
Correct |
94 ms |
2660 KB |
Output is correct |
43 |
Correct |
10 ms |
576 KB |
Output is correct |
44 |
Correct |
57 ms |
2048 KB |
Output is correct |
45 |
Correct |
75 ms |
2236 KB |
Output is correct |
46 |
Correct |
93 ms |
2676 KB |
Output is correct |
47 |
Correct |
28 ms |
1228 KB |
Output is correct |
48 |
Correct |
3 ms |
332 KB |
Output is correct |
49 |
Correct |
209 ms |
2436 KB |
Output is correct |
50 |
Correct |
114 ms |
2644 KB |
Output is correct |
51 |
Correct |
142 ms |
2368 KB |
Output is correct |
52 |
Correct |
110 ms |
2544 KB |
Output is correct |
53 |
Correct |
110 ms |
2676 KB |
Output is correct |
54 |
Correct |
232 ms |
2624 KB |
Output is correct |
55 |
Correct |
92 ms |
2476 KB |
Output is correct |
56 |
Correct |
131 ms |
2536 KB |
Output is correct |
57 |
Correct |
106 ms |
2400 KB |
Output is correct |
58 |
Correct |
814 ms |
2672 KB |
Output is correct |
59 |
Correct |
890 ms |
2692 KB |
Output is correct |
60 |
Correct |
818 ms |
2608 KB |
Output is correct |
61 |
Correct |
897 ms |
2724 KB |
Output is correct |
62 |
Correct |
811 ms |
2660 KB |
Output is correct |
63 |
Correct |
899 ms |
2696 KB |
Output is correct |
64 |
Correct |
769 ms |
2588 KB |
Output is correct |
65 |
Correct |
0 ms |
204 KB |
Output is correct |
66 |
Correct |
0 ms |
204 KB |
Output is correct |
67 |
Correct |
1060 ms |
2640 KB |
Output is correct |
68 |
Correct |
1048 ms |
2636 KB |
Output is correct |
69 |
Correct |
915 ms |
2500 KB |
Output is correct |
70 |
Correct |
966 ms |
2652 KB |
Output is correct |
71 |
Correct |
971 ms |
2564 KB |
Output is correct |
72 |
Correct |
951 ms |
2584 KB |
Output is correct |
73 |
Correct |
969 ms |
2608 KB |
Output is correct |
74 |
Correct |
1032 ms |
2620 KB |
Output is correct |
75 |
Correct |
979 ms |
2664 KB |
Output is correct |
76 |
Correct |
1050 ms |
2692 KB |
Output is correct |
77 |
Correct |
240 ms |
2628 KB |
Output is correct |
78 |
Correct |
119 ms |
2584 KB |
Output is correct |
79 |
Correct |
177 ms |
2468 KB |
Output is correct |
80 |
Correct |
193 ms |
2492 KB |
Output is correct |
81 |
Correct |
176 ms |
2392 KB |
Output is correct |
82 |
Correct |
160 ms |
2660 KB |
Output is correct |
83 |
Correct |
145 ms |
2500 KB |
Output is correct |
84 |
Correct |
669 ms |
2692 KB |
Output is correct |
85 |
Correct |
1110 ms |
2800 KB |
Output is correct |
86 |
Correct |
2 ms |
204 KB |
Output is correct |
87 |
Correct |
1 ms |
204 KB |
Output is correct |
88 |
Correct |
1130 ms |
2736 KB |
Output is correct |
89 |
Correct |
228 ms |
1024 KB |
Output is correct |
90 |
Correct |
1 ms |
288 KB |
Output is correct |