#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb push_back
#define ff first
#define ss second
#define pii pair<int, int>
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int n, k;
cin >> n >> k;
pii p1[k], p2[k];
for (int i = 0; i < k; i++) {
cin >> p1[i].ff >> p1[i].ss >> p2[i].ff >> p2[i].ss;
p1[i].ff--; p1[i].ss--; p2[i].ff--; p2[i].ss--;
}
ll ans = LLONG_MAX;
for (int ii = 1; ii * ii <= n; ii++) {
if (n % ii) continue;
for (auto i : {ii, n / ii}) {
if (i == n) continue;
int x = n / i;
ll cnt1 = ((ll)(x + 1 >> 1) * (x + 1 >> 1) + (ll)(x >> 1) * (x >> 1)) * i * i;
ll cnt2 = ((ll)(x + 1 >> 1) * (x >> 1) + (ll)(x >> 1) * (x + 1 >> 1)) * i * i;
for (int j = 0; j < k; j++) {
pii x = p1[j], y = p2[j];
vector<pair<pii, pii>> v;
if (x.ss / i != y.ss / i) {
if (x.ss % i) {
v.pb({x, {y.ff, x.ss + i - x.ss % i - 1}}); x.ss += i - x.ss % i;
}
if ((y.ss + 1) % i) {
v.pb({{x.ff, y.ss - y.ss % i}, y}); y.ss -= y.ss % i + 1;
}
}
v.pb({x, y});
vector<pair<pii, pii>> u1, u2;
for (auto z : v) {
pii x = z.ff, y = z.ss;
if (x.ff / i == y.ff / i) {
if (x.ss / i == y.ss / i) u1.pb({x, y});
else u2.pb({x, y});
}
else {
if (x.ff % i) {
u1.pb({x, {x.ff + i - x.ff % i - 1, y.ss}}); x.ff += i - x.ff % i;
}
if ((y.ff + 1) % i) {
u1.pb({{y.ff - y.ff % i, x.ss}, y}); y.ff -= y.ff % i + 1;
}
u2.pb({x, y});
}
}
for (auto z : u1) {
pii x = z.ff, y = z.ss;
if ((((x.ff - x.ff % i) / i) & 1) == (((x.ss - x.ss % i) / i) & 1)) {
cnt1 -= (y.ff - x.ff + 1) * (y.ss - x.ss + 1);
cnt2 += (y.ff - x.ff + 1) * (y.ss - x.ss + 1);
}
else {
cnt1 += (y.ff - x.ff + 1) * (y.ss - x.ss + 1);
cnt2 -= (y.ff - x.ff + 1) * (y.ss - x.ss + 1);
}
}
for (auto z : u2) {
pii x = z.ff, y = z.ss;
bool tr = (((x.ff / i) & 1) == ((x.ss / i) & 1));
int r = (y.ff - x.ff + 1) / i, c = (y.ss - x.ss + 1) / i;
cnt1 -= ((r & 1) && (c & 1) ? (tr ? 1 : -1) : 0) * i * i;
cnt2 -= ((r & 1) && (c & 1) ? (tr ? -1 : 1) : 0) * i * i;
}
}
ans = min({ans, cnt1, cnt2});
}
}
cout << ans;
}
Compilation message
chessboard.cpp: In function 'int main()':
chessboard.cpp:32:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
32 | ll cnt1 = ((ll)(x + 1 >> 1) * (x + 1 >> 1) + (ll)(x >> 1) * (x >> 1)) * i * i;
| ~~^~~
chessboard.cpp:32:37: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
32 | ll cnt1 = ((ll)(x + 1 >> 1) * (x + 1 >> 1) + (ll)(x >> 1) * (x >> 1)) * i * i;
| ~~^~~
chessboard.cpp:33:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
33 | ll cnt2 = ((ll)(x + 1 >> 1) * (x >> 1) + (ll)(x >> 1) * (x + 1 >> 1)) * i * i;
| ~~^~~
chessboard.cpp:33:63: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
33 | ll cnt2 = ((ll)(x + 1 >> 1) * (x >> 1) + (ll)(x >> 1) * (x + 1 >> 1)) * i * i;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
1360 KB |
Output is correct |
2 |
Correct |
6 ms |
592 KB |
Output is correct |
3 |
Correct |
12 ms |
848 KB |
Output is correct |
4 |
Correct |
12 ms |
1176 KB |
Output is correct |
5 |
Correct |
15 ms |
1312 KB |
Output is correct |
6 |
Correct |
9 ms |
848 KB |
Output is correct |
7 |
Correct |
2 ms |
572 KB |
Output is correct |
8 |
Correct |
10 ms |
848 KB |
Output is correct |
9 |
Correct |
24 ms |
1616 KB |
Output is correct |
10 |
Correct |
14 ms |
1272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
532 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
508 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
504 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
532 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
508 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
504 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
11 ms |
884 KB |
Output is correct |
17 |
Correct |
20 ms |
1616 KB |
Output is correct |
18 |
Correct |
35 ms |
1872 KB |
Output is correct |
19 |
Correct |
152 ms |
1824 KB |
Output is correct |
20 |
Correct |
170 ms |
1872 KB |
Output is correct |
21 |
Correct |
20 ms |
1724 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
25 ms |
1104 KB |
Output is correct |
24 |
Correct |
32 ms |
1616 KB |
Output is correct |
25 |
Correct |
6 ms |
336 KB |
Output is correct |
26 |
Correct |
21 ms |
1336 KB |
Output is correct |
27 |
Correct |
31 ms |
1360 KB |
Output is correct |
28 |
Correct |
36 ms |
1872 KB |
Output is correct |
29 |
Correct |
8 ms |
848 KB |
Output is correct |
30 |
Correct |
2 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
1360 KB |
Output is correct |
2 |
Correct |
6 ms |
592 KB |
Output is correct |
3 |
Correct |
12 ms |
848 KB |
Output is correct |
4 |
Correct |
12 ms |
1176 KB |
Output is correct |
5 |
Correct |
15 ms |
1312 KB |
Output is correct |
6 |
Correct |
9 ms |
848 KB |
Output is correct |
7 |
Correct |
2 ms |
572 KB |
Output is correct |
8 |
Correct |
10 ms |
848 KB |
Output is correct |
9 |
Correct |
24 ms |
1616 KB |
Output is correct |
10 |
Correct |
14 ms |
1272 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
532 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
508 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
504 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
11 ms |
884 KB |
Output is correct |
27 |
Correct |
20 ms |
1616 KB |
Output is correct |
28 |
Correct |
35 ms |
1872 KB |
Output is correct |
29 |
Correct |
152 ms |
1824 KB |
Output is correct |
30 |
Correct |
170 ms |
1872 KB |
Output is correct |
31 |
Correct |
20 ms |
1724 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
25 ms |
1104 KB |
Output is correct |
34 |
Correct |
32 ms |
1616 KB |
Output is correct |
35 |
Correct |
6 ms |
336 KB |
Output is correct |
36 |
Correct |
21 ms |
1336 KB |
Output is correct |
37 |
Correct |
31 ms |
1360 KB |
Output is correct |
38 |
Correct |
36 ms |
1872 KB |
Output is correct |
39 |
Correct |
8 ms |
848 KB |
Output is correct |
40 |
Correct |
2 ms |
336 KB |
Output is correct |
41 |
Correct |
141 ms |
1616 KB |
Output is correct |
42 |
Correct |
38 ms |
1872 KB |
Output is correct |
43 |
Correct |
71 ms |
1616 KB |
Output is correct |
44 |
Correct |
37 ms |
1872 KB |
Output is correct |
45 |
Correct |
26 ms |
1872 KB |
Output is correct |
46 |
Correct |
148 ms |
1872 KB |
Output is correct |
47 |
Correct |
23 ms |
1616 KB |
Output is correct |
48 |
Correct |
53 ms |
1616 KB |
Output is correct |
49 |
Correct |
31 ms |
1760 KB |
Output is correct |
50 |
Correct |
699 ms |
1616 KB |
Output is correct |
51 |
Correct |
736 ms |
1872 KB |
Output is correct |
52 |
Correct |
690 ms |
1852 KB |
Output is correct |
53 |
Correct |
730 ms |
1872 KB |
Output is correct |
54 |
Correct |
671 ms |
1836 KB |
Output is correct |
55 |
Correct |
787 ms |
2000 KB |
Output is correct |
56 |
Correct |
655 ms |
1788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
18 ms |
1360 KB |
Output is correct |
10 |
Correct |
6 ms |
592 KB |
Output is correct |
11 |
Correct |
12 ms |
848 KB |
Output is correct |
12 |
Correct |
12 ms |
1176 KB |
Output is correct |
13 |
Correct |
15 ms |
1312 KB |
Output is correct |
14 |
Correct |
9 ms |
848 KB |
Output is correct |
15 |
Correct |
2 ms |
572 KB |
Output is correct |
16 |
Correct |
10 ms |
848 KB |
Output is correct |
17 |
Correct |
24 ms |
1616 KB |
Output is correct |
18 |
Correct |
14 ms |
1272 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
532 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
508 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
504 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
11 ms |
884 KB |
Output is correct |
35 |
Correct |
20 ms |
1616 KB |
Output is correct |
36 |
Correct |
35 ms |
1872 KB |
Output is correct |
37 |
Correct |
152 ms |
1824 KB |
Output is correct |
38 |
Correct |
170 ms |
1872 KB |
Output is correct |
39 |
Correct |
20 ms |
1724 KB |
Output is correct |
40 |
Correct |
1 ms |
336 KB |
Output is correct |
41 |
Correct |
25 ms |
1104 KB |
Output is correct |
42 |
Correct |
32 ms |
1616 KB |
Output is correct |
43 |
Correct |
6 ms |
336 KB |
Output is correct |
44 |
Correct |
21 ms |
1336 KB |
Output is correct |
45 |
Correct |
31 ms |
1360 KB |
Output is correct |
46 |
Correct |
36 ms |
1872 KB |
Output is correct |
47 |
Correct |
8 ms |
848 KB |
Output is correct |
48 |
Correct |
2 ms |
336 KB |
Output is correct |
49 |
Correct |
141 ms |
1616 KB |
Output is correct |
50 |
Correct |
38 ms |
1872 KB |
Output is correct |
51 |
Correct |
71 ms |
1616 KB |
Output is correct |
52 |
Correct |
37 ms |
1872 KB |
Output is correct |
53 |
Correct |
26 ms |
1872 KB |
Output is correct |
54 |
Correct |
148 ms |
1872 KB |
Output is correct |
55 |
Correct |
23 ms |
1616 KB |
Output is correct |
56 |
Correct |
53 ms |
1616 KB |
Output is correct |
57 |
Correct |
31 ms |
1760 KB |
Output is correct |
58 |
Correct |
699 ms |
1616 KB |
Output is correct |
59 |
Correct |
736 ms |
1872 KB |
Output is correct |
60 |
Correct |
690 ms |
1852 KB |
Output is correct |
61 |
Correct |
730 ms |
1872 KB |
Output is correct |
62 |
Correct |
671 ms |
1836 KB |
Output is correct |
63 |
Correct |
787 ms |
2000 KB |
Output is correct |
64 |
Correct |
655 ms |
1788 KB |
Output is correct |
65 |
Correct |
1 ms |
336 KB |
Output is correct |
66 |
Correct |
1 ms |
336 KB |
Output is correct |
67 |
Incorrect |
941 ms |
1896 KB |
Output isn't correct |
68 |
Halted |
0 ms |
0 KB |
- |