#include <bits/stdc++.h>
using namespace std;
const int K = 1e5 + 10, SQ = 700;
int n, k;
int x1[K], Y1[K], x2[K], y2[K];
vector<int> divs;
long long sum[SQ], P;
long long get(int x, int y, int k) {
if(x == 0 || y == 0) {
return 0;
}
int X = (x / k) * k, Y = (y / k) * k;
long long ans = ((X / k) * 1LL * (Y / k) + 1) / 2 * k * k;
// cout << k << "\n";
// cout << x << " " << y << "\n";
// cout << X << " " << Y << "\n";
// cout << ans << "\n";
ans += (x - X) * 1LL * k * (((Y / k) + ((X / k) % 2 == 0)) / 2);
// cout << ans << "\n";
ans += (y - Y) * 1LL * k * (((X / k) + ((Y / k) % 2 == 0)) / 2);
// cout << ans << "\n";
if((X / k + Y / k) % 2 == 0) {
ans += (x - X) * 1LL * (y - Y);
}
return ans;
}
long long get(int x1, int Y1, int x2, int y2, int k) {
return get(x2, y2, k) - get(x2, Y1 - 1, k) - get(x1 - 1, y2, k) + get(x1 - 1, Y1 - 1, k);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for(int i = 0; i < k; i++) {
cin >> x1[i] >> Y1[i] >> x2[i] >> y2[i];
P += (x2[i] - x1[i] + 1) * 1LL * (y2[i] - Y1[i] + 1);
}
divs.push_back(1);
for(int i = 2; i * i < n; i++) {
if(n % i == 0) {
divs.push_back(i);
divs.push_back(n / i);
}
}
if(n % (int)sqrt(n) == 0) {
divs.push_back(sqrt(n));
}
sort(divs.begin(), divs.end());
for(int i = 0; i < k; i++) {
for(int d = 0; d < divs.size(); d++) {
sum[d] += get(x1[i], Y1[i], x2[i], y2[i], divs[d]);
// cout << x1[i] << " " << Y1[i] << " " << x2[i] << " " << y2[i] << " " << divs[d] << " " << get(x1[i], Y1[i], x2[i], y2[i], divs[d]) << "\n";
}
}
long long ans = 1e18;
// cout << P << "\n\n";
for(int i = 0; i < divs.size(); i++) {
long long all = (((n / divs[i]) * 1LL * (n / divs[i]) + 1) / 2) * divs[i] * divs[i];
ans = min(ans, all + P - 2 * sum[i]);
// cout << i << ": " << ans << "\n";
all = ((n / divs[i]) * 1LL * (n / divs[i]) / 2) * divs[i] * divs[i];
ans = min(ans, all - P + 2 * sum[i]);
// cout << i << ": " << ans << "\n";
}
cout << ans;
return 0;
}
/*
6 8
3 3 3 3
1 2 1 2
3 4 3 4
5 5 5 5
4 3 4 3
4 4 4 4
2 1 2 1
3 6 3 6
4 1
4 1 4 4
*/
Compilation message
chessboard.cpp: In function 'int main()':
chessboard.cpp:55:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for(int d = 0; d < divs.size(); d++) {
| ~~^~~~~~~~~~~~~
chessboard.cpp:62:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for(int i = 0; i < divs.size(); i++) {
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
2776 KB |
Output is correct |
2 |
Correct |
6 ms |
992 KB |
Output is correct |
3 |
Correct |
12 ms |
1924 KB |
Output is correct |
4 |
Correct |
12 ms |
1884 KB |
Output is correct |
5 |
Correct |
22 ms |
2548 KB |
Output is correct |
6 |
Correct |
10 ms |
1840 KB |
Output is correct |
7 |
Correct |
3 ms |
600 KB |
Output is correct |
8 |
Correct |
10 ms |
1628 KB |
Output is correct |
9 |
Correct |
26 ms |
3924 KB |
Output is correct |
10 |
Correct |
15 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
464 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
464 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
10 ms |
1116 KB |
Output is correct |
17 |
Correct |
24 ms |
2920 KB |
Output is correct |
18 |
Correct |
33 ms |
3412 KB |
Output is correct |
19 |
Correct |
126 ms |
3160 KB |
Output is correct |
20 |
Correct |
141 ms |
3600 KB |
Output is correct |
21 |
Correct |
20 ms |
2904 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
22 ms |
1628 KB |
Output is correct |
24 |
Correct |
34 ms |
3328 KB |
Output is correct |
25 |
Correct |
5 ms |
600 KB |
Output is correct |
26 |
Correct |
24 ms |
2136 KB |
Output is correct |
27 |
Correct |
35 ms |
2396 KB |
Output is correct |
28 |
Correct |
32 ms |
3412 KB |
Output is correct |
29 |
Correct |
8 ms |
1372 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
2776 KB |
Output is correct |
2 |
Correct |
6 ms |
992 KB |
Output is correct |
3 |
Correct |
12 ms |
1924 KB |
Output is correct |
4 |
Correct |
12 ms |
1884 KB |
Output is correct |
5 |
Correct |
22 ms |
2548 KB |
Output is correct |
6 |
Correct |
10 ms |
1840 KB |
Output is correct |
7 |
Correct |
3 ms |
600 KB |
Output is correct |
8 |
Correct |
10 ms |
1628 KB |
Output is correct |
9 |
Correct |
26 ms |
3924 KB |
Output is correct |
10 |
Correct |
15 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
464 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
10 ms |
1116 KB |
Output is correct |
27 |
Correct |
24 ms |
2920 KB |
Output is correct |
28 |
Correct |
33 ms |
3412 KB |
Output is correct |
29 |
Correct |
126 ms |
3160 KB |
Output is correct |
30 |
Correct |
141 ms |
3600 KB |
Output is correct |
31 |
Correct |
20 ms |
2904 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
22 ms |
1628 KB |
Output is correct |
34 |
Correct |
34 ms |
3328 KB |
Output is correct |
35 |
Correct |
5 ms |
600 KB |
Output is correct |
36 |
Correct |
24 ms |
2136 KB |
Output is correct |
37 |
Correct |
35 ms |
2396 KB |
Output is correct |
38 |
Correct |
32 ms |
3412 KB |
Output is correct |
39 |
Correct |
8 ms |
1372 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
112 ms |
3736 KB |
Output is correct |
42 |
Correct |
36 ms |
4188 KB |
Output is correct |
43 |
Correct |
65 ms |
3676 KB |
Output is correct |
44 |
Correct |
36 ms |
3932 KB |
Output is correct |
45 |
Correct |
30 ms |
4188 KB |
Output is correct |
46 |
Correct |
125 ms |
3928 KB |
Output is correct |
47 |
Correct |
25 ms |
3668 KB |
Output is correct |
48 |
Correct |
50 ms |
3672 KB |
Output is correct |
49 |
Correct |
33 ms |
3764 KB |
Output is correct |
50 |
Correct |
563 ms |
4188 KB |
Output is correct |
51 |
Correct |
601 ms |
4436 KB |
Output is correct |
52 |
Correct |
564 ms |
3924 KB |
Output is correct |
53 |
Correct |
600 ms |
4436 KB |
Output is correct |
54 |
Correct |
553 ms |
3972 KB |
Output is correct |
55 |
Correct |
627 ms |
4432 KB |
Output is correct |
56 |
Correct |
535 ms |
3860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
18 ms |
2776 KB |
Output is correct |
10 |
Correct |
6 ms |
992 KB |
Output is correct |
11 |
Correct |
12 ms |
1924 KB |
Output is correct |
12 |
Correct |
12 ms |
1884 KB |
Output is correct |
13 |
Correct |
22 ms |
2548 KB |
Output is correct |
14 |
Correct |
10 ms |
1840 KB |
Output is correct |
15 |
Correct |
3 ms |
600 KB |
Output is correct |
16 |
Correct |
10 ms |
1628 KB |
Output is correct |
17 |
Correct |
26 ms |
3924 KB |
Output is correct |
18 |
Correct |
15 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
464 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
10 ms |
1116 KB |
Output is correct |
35 |
Correct |
24 ms |
2920 KB |
Output is correct |
36 |
Correct |
33 ms |
3412 KB |
Output is correct |
37 |
Correct |
126 ms |
3160 KB |
Output is correct |
38 |
Correct |
141 ms |
3600 KB |
Output is correct |
39 |
Correct |
20 ms |
2904 KB |
Output is correct |
40 |
Correct |
1 ms |
344 KB |
Output is correct |
41 |
Correct |
22 ms |
1628 KB |
Output is correct |
42 |
Correct |
34 ms |
3328 KB |
Output is correct |
43 |
Correct |
5 ms |
600 KB |
Output is correct |
44 |
Correct |
24 ms |
2136 KB |
Output is correct |
45 |
Correct |
35 ms |
2396 KB |
Output is correct |
46 |
Correct |
32 ms |
3412 KB |
Output is correct |
47 |
Correct |
8 ms |
1372 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
112 ms |
3736 KB |
Output is correct |
50 |
Correct |
36 ms |
4188 KB |
Output is correct |
51 |
Correct |
65 ms |
3676 KB |
Output is correct |
52 |
Correct |
36 ms |
3932 KB |
Output is correct |
53 |
Correct |
30 ms |
4188 KB |
Output is correct |
54 |
Correct |
125 ms |
3928 KB |
Output is correct |
55 |
Correct |
25 ms |
3668 KB |
Output is correct |
56 |
Correct |
50 ms |
3672 KB |
Output is correct |
57 |
Correct |
33 ms |
3764 KB |
Output is correct |
58 |
Correct |
563 ms |
4188 KB |
Output is correct |
59 |
Correct |
601 ms |
4436 KB |
Output is correct |
60 |
Correct |
564 ms |
3924 KB |
Output is correct |
61 |
Correct |
600 ms |
4436 KB |
Output is correct |
62 |
Correct |
553 ms |
3972 KB |
Output is correct |
63 |
Correct |
627 ms |
4432 KB |
Output is correct |
64 |
Correct |
535 ms |
3860 KB |
Output is correct |
65 |
Correct |
0 ms |
348 KB |
Output is correct |
66 |
Correct |
1 ms |
348 KB |
Output is correct |
67 |
Correct |
594 ms |
4144 KB |
Output is correct |
68 |
Correct |
591 ms |
4128 KB |
Output is correct |
69 |
Correct |
517 ms |
3664 KB |
Output is correct |
70 |
Correct |
579 ms |
4032 KB |
Output is correct |
71 |
Correct |
582 ms |
3928 KB |
Output is correct |
72 |
Correct |
561 ms |
3908 KB |
Output is correct |
73 |
Correct |
550 ms |
3924 KB |
Output is correct |
74 |
Correct |
585 ms |
3932 KB |
Output is correct |
75 |
Correct |
567 ms |
3948 KB |
Output is correct |
76 |
Correct |
612 ms |
4276 KB |
Output is correct |
77 |
Correct |
98 ms |
4180 KB |
Output is correct |
78 |
Correct |
36 ms |
3888 KB |
Output is correct |
79 |
Correct |
67 ms |
3676 KB |
Output is correct |
80 |
Correct |
72 ms |
3992 KB |
Output is correct |
81 |
Correct |
66 ms |
3868 KB |
Output is correct |
82 |
Correct |
55 ms |
3928 KB |
Output is correct |
83 |
Correct |
43 ms |
3664 KB |
Output is correct |
84 |
Correct |
361 ms |
4512 KB |
Output is correct |
85 |
Correct |
623 ms |
4436 KB |
Output is correct |
86 |
Correct |
1 ms |
344 KB |
Output is correct |
87 |
Correct |
1 ms |
348 KB |
Output is correct |
88 |
Correct |
637 ms |
4396 KB |
Output is correct |
89 |
Correct |
120 ms |
1116 KB |
Output is correct |
90 |
Correct |
0 ms |
348 KB |
Output is correct |