#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fi first
#define se second
#define ll long long
#define ld long double
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mpp make_pair
#define ve vector
using namespace std;
using namespace __gnu_pbds;
template<class T> using oset = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
const ll inf = 1e18; const int iinf = 1e9;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T> inline bool chmin(T& a, T b) { return (a > b ? a = b, 1 : 0); }
template <typename T> inline bool chmax(T& a, T b) { return (a < b ? a = b, 1 : 0); }
inline void solve() {
int n, m;
cin >> n >> m;
ve<pair<pii,pii>> a(m);
for (auto &[a1, a2] : a) {
auto &[x1, y1] = a1;
auto &[x2, y2] = a2;
cin >> x1 >> y1 >> x2 >> y2;
--x1, --y1, --x2, --y2;
}
auto even = [](ll l, ll r) { if (l > r) {return 0ll;} return r / 2 - (l + 1) / 2 + 1; };
auto odd = [&even](ll l, ll r) { if (l > r) {return 0ll;} return r - l + 1 - even(l, r); };
auto run = [&](ll d) {
ll ans1 = 0, ans2 = 0;
for (auto &[a1, a2] : a) {
auto &[x1, y1] = a1;
auto &[x2, y2] = a2;
ll evx, odx, evy, ody;
{
ll mn = x1 / d, mx = x2 / d;
if (mn == mx) {
if (mn & 1) evx = 0, odx = x2 - x1 + 1;
else odx = 0, evx = x2 - x1 + 1;
}
else {
evx = odx = 0;
evx += d * even(mn + 1, mx - 1);
odx += d * odd(mn + 1, mx - 1);
if (mn & 1) odx += (mn + 1) * d - x1;
if (mx & 1) odx += x2 - mx * d + 1;
if (mn & 1 ^ 1) evx += (mn + 1) * d - x1;
if (mx & 1 ^ 1) evx += x2 - mx * d + 1;
}
}
{
ll mn = y1 / d, mx = y2 / d;
if (mn == mx) {
if (mn & 1) evy = 0, ody = y2 - y1 + 1;
else ody = 0, evy = y2 - y1 + 1;
}
else {
evy = ody = 0;
evy += d * even(mn + 1, mx - 1);
ody += d * odd(mn + 1, mx - 1);
if (mn & 1) ody += (mn + 1) * d - y1;
if (mx & 1) ody += y2 - mx * d + 1;
if (mn & 1 ^ 1) evy += (mn + 1) * d - y1;
if (mx & 1 ^ 1) evy += y2 - mx * d + 1;
}
}
// corner is white
ans1 += evx * evy + odx * ody;
ans1 -= (evx * ody + evy * odx);
// corner is black
ans2 += evx * ody + evy * odx;
ans2 -= (evx * evy + odx * ody);
}
ll e = even(1, n / d), o = odd(1, n / d);
ans1 += d * d * (e * o + o * e);
ans2 += d * d * (e * e + o * o);
return min(ans1, ans2);
};
ll res = inf;
for (int d = 1; d * d <= n; ++d) {
if (n % d) continue;
chmin(res, run(d));
if (d * d != n && d > 1) chmin(res, run(n / d));
}
cout << res;
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int q = 1; // cin >> q;
while (q--) solve();
cerr << fixed << setprecision(3) << "Time execution: " << (double)clock() / CLOCKS_PER_SEC << endl;
}
Compilation message
chessboard.cpp: In lambda function:
chessboard.cpp:59:13: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
59 | if (mn & 1 ^ 1) evx += (mn + 1) * d - x1;
| ~~~^~~
chessboard.cpp:60:13: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
60 | if (mx & 1 ^ 1) evx += x2 - mx * d + 1;
| ~~~^~~
chessboard.cpp:77:13: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
77 | if (mn & 1 ^ 1) evy += (mn + 1) * d - y1;
| ~~~^~~
chessboard.cpp:78:13: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
78 | if (mx & 1 ^ 1) evy += y2 - mx * d + 1;
| ~~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
1236 KB |
Output is correct |
2 |
Correct |
6 ms |
560 KB |
Output is correct |
3 |
Correct |
13 ms |
852 KB |
Output is correct |
4 |
Correct |
15 ms |
980 KB |
Output is correct |
5 |
Correct |
19 ms |
1108 KB |
Output is correct |
6 |
Correct |
12 ms |
852 KB |
Output is correct |
7 |
Correct |
3 ms |
440 KB |
Output is correct |
8 |
Correct |
12 ms |
852 KB |
Output is correct |
9 |
Correct |
32 ms |
1620 KB |
Output is correct |
10 |
Correct |
16 ms |
1108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 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 |
212 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 |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 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 |
212 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 |
340 KB |
Output is correct |
16 |
Correct |
12 ms |
724 KB |
Output is correct |
17 |
Correct |
23 ms |
1620 KB |
Output is correct |
18 |
Correct |
31 ms |
1876 KB |
Output is correct |
19 |
Correct |
76 ms |
1620 KB |
Output is correct |
20 |
Correct |
86 ms |
1876 KB |
Output is correct |
21 |
Correct |
27 ms |
1492 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
18 ms |
980 KB |
Output is correct |
24 |
Correct |
43 ms |
1620 KB |
Output is correct |
25 |
Correct |
4 ms |
340 KB |
Output is correct |
26 |
Correct |
18 ms |
1108 KB |
Output is correct |
27 |
Correct |
25 ms |
1364 KB |
Output is correct |
28 |
Correct |
28 ms |
1748 KB |
Output is correct |
29 |
Correct |
10 ms |
852 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
1236 KB |
Output is correct |
2 |
Correct |
6 ms |
560 KB |
Output is correct |
3 |
Correct |
13 ms |
852 KB |
Output is correct |
4 |
Correct |
15 ms |
980 KB |
Output is correct |
5 |
Correct |
19 ms |
1108 KB |
Output is correct |
6 |
Correct |
12 ms |
852 KB |
Output is correct |
7 |
Correct |
3 ms |
440 KB |
Output is correct |
8 |
Correct |
12 ms |
852 KB |
Output is correct |
9 |
Correct |
32 ms |
1620 KB |
Output is correct |
10 |
Correct |
16 ms |
1108 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 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 |
212 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 |
12 ms |
724 KB |
Output is correct |
27 |
Correct |
23 ms |
1620 KB |
Output is correct |
28 |
Correct |
31 ms |
1876 KB |
Output is correct |
29 |
Correct |
76 ms |
1620 KB |
Output is correct |
30 |
Correct |
86 ms |
1876 KB |
Output is correct |
31 |
Correct |
27 ms |
1492 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
18 ms |
980 KB |
Output is correct |
34 |
Correct |
43 ms |
1620 KB |
Output is correct |
35 |
Correct |
4 ms |
340 KB |
Output is correct |
36 |
Correct |
18 ms |
1108 KB |
Output is correct |
37 |
Correct |
25 ms |
1364 KB |
Output is correct |
38 |
Correct |
28 ms |
1748 KB |
Output is correct |
39 |
Correct |
10 ms |
852 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
76 ms |
1620 KB |
Output is correct |
42 |
Correct |
49 ms |
3956 KB |
Output is correct |
43 |
Correct |
48 ms |
3536 KB |
Output is correct |
44 |
Correct |
33 ms |
3920 KB |
Output is correct |
45 |
Correct |
33 ms |
4156 KB |
Output is correct |
46 |
Correct |
79 ms |
3912 KB |
Output is correct |
47 |
Correct |
40 ms |
3696 KB |
Output is correct |
48 |
Correct |
50 ms |
3784 KB |
Output is correct |
49 |
Correct |
33 ms |
3532 KB |
Output is correct |
50 |
Correct |
309 ms |
3788 KB |
Output is correct |
51 |
Correct |
357 ms |
4044 KB |
Output is correct |
52 |
Correct |
305 ms |
3772 KB |
Output is correct |
53 |
Correct |
346 ms |
4112 KB |
Output is correct |
54 |
Correct |
305 ms |
3756 KB |
Output is correct |
55 |
Correct |
351 ms |
4168 KB |
Output is correct |
56 |
Correct |
292 ms |
3712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
19 ms |
1236 KB |
Output is correct |
10 |
Correct |
6 ms |
560 KB |
Output is correct |
11 |
Correct |
13 ms |
852 KB |
Output is correct |
12 |
Correct |
15 ms |
980 KB |
Output is correct |
13 |
Correct |
19 ms |
1108 KB |
Output is correct |
14 |
Correct |
12 ms |
852 KB |
Output is correct |
15 |
Correct |
3 ms |
440 KB |
Output is correct |
16 |
Correct |
12 ms |
852 KB |
Output is correct |
17 |
Correct |
32 ms |
1620 KB |
Output is correct |
18 |
Correct |
16 ms |
1108 KB |
Output is correct |
19 |
Correct |
0 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 |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
0 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 |
212 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 |
340 KB |
Output is correct |
34 |
Correct |
12 ms |
724 KB |
Output is correct |
35 |
Correct |
23 ms |
1620 KB |
Output is correct |
36 |
Correct |
31 ms |
1876 KB |
Output is correct |
37 |
Correct |
76 ms |
1620 KB |
Output is correct |
38 |
Correct |
86 ms |
1876 KB |
Output is correct |
39 |
Correct |
27 ms |
1492 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
18 ms |
980 KB |
Output is correct |
42 |
Correct |
43 ms |
1620 KB |
Output is correct |
43 |
Correct |
4 ms |
340 KB |
Output is correct |
44 |
Correct |
18 ms |
1108 KB |
Output is correct |
45 |
Correct |
25 ms |
1364 KB |
Output is correct |
46 |
Correct |
28 ms |
1748 KB |
Output is correct |
47 |
Correct |
10 ms |
852 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
76 ms |
1620 KB |
Output is correct |
50 |
Correct |
49 ms |
3956 KB |
Output is correct |
51 |
Correct |
48 ms |
3536 KB |
Output is correct |
52 |
Correct |
33 ms |
3920 KB |
Output is correct |
53 |
Correct |
33 ms |
4156 KB |
Output is correct |
54 |
Correct |
79 ms |
3912 KB |
Output is correct |
55 |
Correct |
40 ms |
3696 KB |
Output is correct |
56 |
Correct |
50 ms |
3784 KB |
Output is correct |
57 |
Correct |
33 ms |
3532 KB |
Output is correct |
58 |
Correct |
309 ms |
3788 KB |
Output is correct |
59 |
Correct |
357 ms |
4044 KB |
Output is correct |
60 |
Correct |
305 ms |
3772 KB |
Output is correct |
61 |
Correct |
346 ms |
4112 KB |
Output is correct |
62 |
Correct |
305 ms |
3756 KB |
Output is correct |
63 |
Correct |
351 ms |
4168 KB |
Output is correct |
64 |
Correct |
292 ms |
3712 KB |
Output is correct |
65 |
Correct |
1 ms |
212 KB |
Output is correct |
66 |
Correct |
1 ms |
216 KB |
Output is correct |
67 |
Correct |
394 ms |
3944 KB |
Output is correct |
68 |
Correct |
367 ms |
3940 KB |
Output is correct |
69 |
Correct |
336 ms |
3540 KB |
Output is correct |
70 |
Correct |
341 ms |
3920 KB |
Output is correct |
71 |
Correct |
375 ms |
3756 KB |
Output is correct |
72 |
Correct |
343 ms |
3720 KB |
Output is correct |
73 |
Correct |
350 ms |
3704 KB |
Output is correct |
74 |
Correct |
361 ms |
3916 KB |
Output is correct |
75 |
Correct |
345 ms |
3716 KB |
Output is correct |
76 |
Correct |
371 ms |
3964 KB |
Output is correct |
77 |
Correct |
77 ms |
4160 KB |
Output is correct |
78 |
Correct |
36 ms |
3788 KB |
Output is correct |
79 |
Correct |
57 ms |
3548 KB |
Output is correct |
80 |
Correct |
71 ms |
3748 KB |
Output is correct |
81 |
Correct |
62 ms |
3532 KB |
Output is correct |
82 |
Correct |
47 ms |
3968 KB |
Output is correct |
83 |
Correct |
40 ms |
3692 KB |
Output is correct |
84 |
Correct |
253 ms |
4116 KB |
Output is correct |
85 |
Correct |
375 ms |
4180 KB |
Output is correct |
86 |
Correct |
1 ms |
212 KB |
Output is correct |
87 |
Correct |
1 ms |
212 KB |
Output is correct |
88 |
Correct |
400 ms |
4176 KB |
Output is correct |
89 |
Correct |
73 ms |
980 KB |
Output is correct |
90 |
Correct |
0 ms |
324 KB |
Output is correct |