// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
#define pb push_back
template<class T> using V = vector<T>;
template<class T, size_t SZ> using AR = array<T, SZ>;
using ll = long long;
const ll INFL = ll(1e18) + 10;
int main() {
cin.tie(0)->sync_with_stdio(0);
// ONLY SQRT(N) WIDTHS
int N, K; cin >> N >> K;
V<AR<int, 4>> A(K);
for(auto& x : A) cin >> x[0] >> x[1] >> x[2] >> x[3];
V<int> div;
for(int i = 1; i*i <= N; i++) {
if (N % i == 0) {
div.pb(i);
if (i*i != N) div.pb(N / i);
}
}
sort(begin(div), end(div)); div.pop_back();
ll ans = INFL;
for(auto& w : div) {
int n = N / w;
AR<ll, 2> res = {((n * 1LL * n) / 2) * w * 1LL * w, ((n * 1LL * n + 1) / 2) * w * 1LL * w};
// res[0] -> top-left is white, res[1] -> top-left is black
for(auto x : A) {
auto [r, c, R, C] = x; --r, --c, --R, --C;
// BLACK AND WHITE PER ROW (FOR FIRST ROW)
int BPR = 0, WPR = 0;
// CHECK (r, c) and (r, C)
int wr = r / w, wc = c / w, wC = C / w, wR = R / w;
int Crc = (wr + wc) % 2, CrC = (wr + wC) % 2;
if (wc == wC) (Crc == 0 ? WPR += C - c + 1 : BPR += C - c + 1);
else {
int LB = (wc + 1) * w - 1;
(Crc == 0 ? WPR += LB - c + 1 : BPR += LB - c + 1);
int RB = wC * w;
(CrC == 0 ? WPR += C - RB + 1 : BPR += C - RB + 1);
int LEN = (RB - 1) - (LB + 1) + 1;
assert(LEN % w == 0); int BLK = LEN / w;
if (Crc == 0) BPR += ((BLK + 1) / 2) * w, WPR += (BLK / 2) * w;
else WPR += ((BLK + 1) / 2) * w, BPR += (BLK / 2) * w;
}
// OBVERSED AND INVERSED ROWS
int OPR = 0, IPR = 0;
if (wr == wR) OPR += R - r + 1;
else {
int LB = (wr + 1) * w - 1;
OPR += LB - r + 1;
int LEN = R - (LB + 1) + 1;
int BLK = LEN / w; int LEFT = LEN % w;
IPR += ((BLK + 1) / 2) * w; OPR += (BLK / 2) * w;
if (BLK % 2) OPR += LEFT;
else IPR += LEFT;
}
ll BLACKS = BPR * 1LL * OPR + WPR * 1LL * IPR;
ll WHITES = WPR * 1LL * OPR + BPR * 1LL * IPR;
// cout << BLACKS << " - " << WHITES << nl;
res[0] += WHITES - BLACKS; res[1] += BLACKS - WHITES;
}
// cout << res[0] << " " << res[1] << nl;
ans = min(ans, min(res[0], res[1]));
}
cout << ans << nl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
2760 KB |
Output is correct |
2 |
Correct |
5 ms |
852 KB |
Output is correct |
3 |
Correct |
11 ms |
1832 KB |
Output is correct |
4 |
Correct |
12 ms |
1804 KB |
Output is correct |
5 |
Correct |
15 ms |
2380 KB |
Output is correct |
6 |
Correct |
10 ms |
1620 KB |
Output is correct |
7 |
Correct |
3 ms |
596 KB |
Output is correct |
8 |
Correct |
11 ms |
1612 KB |
Output is correct |
9 |
Correct |
25 ms |
3772 KB |
Output is correct |
10 |
Correct |
17 ms |
2260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
328 KB |
Output is correct |
16 |
Correct |
7 ms |
1100 KB |
Output is correct |
17 |
Correct |
19 ms |
2892 KB |
Output is correct |
18 |
Correct |
26 ms |
3412 KB |
Output is correct |
19 |
Correct |
64 ms |
3028 KB |
Output is correct |
20 |
Correct |
71 ms |
3408 KB |
Output is correct |
21 |
Correct |
18 ms |
2900 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
17 ms |
1620 KB |
Output is correct |
24 |
Correct |
23 ms |
3156 KB |
Output is correct |
25 |
Correct |
4 ms |
596 KB |
Output is correct |
26 |
Correct |
18 ms |
2100 KB |
Output is correct |
27 |
Correct |
20 ms |
2388 KB |
Output is correct |
28 |
Correct |
25 ms |
3276 KB |
Output is correct |
29 |
Correct |
8 ms |
1344 KB |
Output is correct |
30 |
Correct |
2 ms |
400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
2760 KB |
Output is correct |
2 |
Correct |
5 ms |
852 KB |
Output is correct |
3 |
Correct |
11 ms |
1832 KB |
Output is correct |
4 |
Correct |
12 ms |
1804 KB |
Output is correct |
5 |
Correct |
15 ms |
2380 KB |
Output is correct |
6 |
Correct |
10 ms |
1620 KB |
Output is correct |
7 |
Correct |
3 ms |
596 KB |
Output is correct |
8 |
Correct |
11 ms |
1612 KB |
Output is correct |
9 |
Correct |
25 ms |
3772 KB |
Output is correct |
10 |
Correct |
17 ms |
2260 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
328 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
320 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
328 KB |
Output is correct |
26 |
Correct |
7 ms |
1100 KB |
Output is correct |
27 |
Correct |
19 ms |
2892 KB |
Output is correct |
28 |
Correct |
26 ms |
3412 KB |
Output is correct |
29 |
Correct |
64 ms |
3028 KB |
Output is correct |
30 |
Correct |
71 ms |
3408 KB |
Output is correct |
31 |
Correct |
18 ms |
2900 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
17 ms |
1620 KB |
Output is correct |
34 |
Correct |
23 ms |
3156 KB |
Output is correct |
35 |
Correct |
4 ms |
596 KB |
Output is correct |
36 |
Correct |
18 ms |
2100 KB |
Output is correct |
37 |
Correct |
20 ms |
2388 KB |
Output is correct |
38 |
Correct |
25 ms |
3276 KB |
Output is correct |
39 |
Correct |
8 ms |
1344 KB |
Output is correct |
40 |
Correct |
2 ms |
400 KB |
Output is correct |
41 |
Correct |
63 ms |
3536 KB |
Output is correct |
42 |
Correct |
30 ms |
4044 KB |
Output is correct |
43 |
Correct |
40 ms |
3528 KB |
Output is correct |
44 |
Correct |
30 ms |
3912 KB |
Output is correct |
45 |
Correct |
27 ms |
4156 KB |
Output is correct |
46 |
Correct |
68 ms |
3932 KB |
Output is correct |
47 |
Correct |
23 ms |
3692 KB |
Output is correct |
48 |
Correct |
35 ms |
3708 KB |
Output is correct |
49 |
Correct |
26 ms |
3552 KB |
Output is correct |
50 |
Correct |
269 ms |
3788 KB |
Output is correct |
51 |
Correct |
286 ms |
4140 KB |
Output is correct |
52 |
Correct |
266 ms |
3788 KB |
Output is correct |
53 |
Correct |
280 ms |
4136 KB |
Output is correct |
54 |
Correct |
258 ms |
3752 KB |
Output is correct |
55 |
Correct |
293 ms |
4180 KB |
Output is correct |
56 |
Correct |
250 ms |
3712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
20 ms |
2760 KB |
Output is correct |
10 |
Correct |
5 ms |
852 KB |
Output is correct |
11 |
Correct |
11 ms |
1832 KB |
Output is correct |
12 |
Correct |
12 ms |
1804 KB |
Output is correct |
13 |
Correct |
15 ms |
2380 KB |
Output is correct |
14 |
Correct |
10 ms |
1620 KB |
Output is correct |
15 |
Correct |
3 ms |
596 KB |
Output is correct |
16 |
Correct |
11 ms |
1612 KB |
Output is correct |
17 |
Correct |
25 ms |
3772 KB |
Output is correct |
18 |
Correct |
17 ms |
2260 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
320 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
328 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
320 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
328 KB |
Output is correct |
34 |
Correct |
7 ms |
1100 KB |
Output is correct |
35 |
Correct |
19 ms |
2892 KB |
Output is correct |
36 |
Correct |
26 ms |
3412 KB |
Output is correct |
37 |
Correct |
64 ms |
3028 KB |
Output is correct |
38 |
Correct |
71 ms |
3408 KB |
Output is correct |
39 |
Correct |
18 ms |
2900 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
17 ms |
1620 KB |
Output is correct |
42 |
Correct |
23 ms |
3156 KB |
Output is correct |
43 |
Correct |
4 ms |
596 KB |
Output is correct |
44 |
Correct |
18 ms |
2100 KB |
Output is correct |
45 |
Correct |
20 ms |
2388 KB |
Output is correct |
46 |
Correct |
25 ms |
3276 KB |
Output is correct |
47 |
Correct |
8 ms |
1344 KB |
Output is correct |
48 |
Correct |
2 ms |
400 KB |
Output is correct |
49 |
Correct |
63 ms |
3536 KB |
Output is correct |
50 |
Correct |
30 ms |
4044 KB |
Output is correct |
51 |
Correct |
40 ms |
3528 KB |
Output is correct |
52 |
Correct |
30 ms |
3912 KB |
Output is correct |
53 |
Correct |
27 ms |
4156 KB |
Output is correct |
54 |
Correct |
68 ms |
3932 KB |
Output is correct |
55 |
Correct |
23 ms |
3692 KB |
Output is correct |
56 |
Correct |
35 ms |
3708 KB |
Output is correct |
57 |
Correct |
26 ms |
3552 KB |
Output is correct |
58 |
Correct |
269 ms |
3788 KB |
Output is correct |
59 |
Correct |
286 ms |
4140 KB |
Output is correct |
60 |
Correct |
266 ms |
3788 KB |
Output is correct |
61 |
Correct |
280 ms |
4136 KB |
Output is correct |
62 |
Correct |
258 ms |
3752 KB |
Output is correct |
63 |
Correct |
293 ms |
4180 KB |
Output is correct |
64 |
Correct |
250 ms |
3712 KB |
Output is correct |
65 |
Correct |
1 ms |
212 KB |
Output is correct |
66 |
Correct |
1 ms |
212 KB |
Output is correct |
67 |
Correct |
305 ms |
3908 KB |
Output is correct |
68 |
Correct |
303 ms |
3944 KB |
Output is correct |
69 |
Correct |
264 ms |
3528 KB |
Output is correct |
70 |
Correct |
294 ms |
3788 KB |
Output is correct |
71 |
Correct |
286 ms |
3744 KB |
Output is correct |
72 |
Correct |
285 ms |
3796 KB |
Output is correct |
73 |
Correct |
288 ms |
3552 KB |
Output is correct |
74 |
Correct |
305 ms |
3940 KB |
Output is correct |
75 |
Correct |
295 ms |
3716 KB |
Output is correct |
76 |
Correct |
313 ms |
4044 KB |
Output is correct |
77 |
Correct |
64 ms |
4156 KB |
Output is correct |
78 |
Correct |
30 ms |
3796 KB |
Output is correct |
79 |
Correct |
45 ms |
3564 KB |
Output is correct |
80 |
Correct |
48 ms |
3788 KB |
Output is correct |
81 |
Correct |
45 ms |
3512 KB |
Output is correct |
82 |
Correct |
46 ms |
3968 KB |
Output is correct |
83 |
Correct |
33 ms |
3704 KB |
Output is correct |
84 |
Correct |
189 ms |
4120 KB |
Output is correct |
85 |
Correct |
319 ms |
4184 KB |
Output is correct |
86 |
Correct |
1 ms |
212 KB |
Output is correct |
87 |
Correct |
1 ms |
212 KB |
Output is correct |
88 |
Correct |
325 ms |
4168 KB |
Output is correct |
89 |
Correct |
63 ms |
980 KB |
Output is correct |
90 |
Correct |
0 ms |
212 KB |
Output is correct |