#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 10;
#define debug(...) fprintf(stderr, __VA_ARGS__)
//#define debug(...)
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
#define fillchar(a, s) memset((a), (s), sizeof(a))
ll sqr (ll x) {
return x * x;
}
void setmin (ll &a, ll b) {
if (b < a) {
a = b;
}
}
ll N, D;
ll dsqr;
int K;
ll X1[MAXN], X2[MAXN], Y1[MAXN], Y2[MAXN];
ll xmx[2], ymx[2];
ll xbound[2], ybound[2];
ll xodd[2], yodd[2], xeven[2], yeven[2];
ll xlen[2], ylen[2];
ll totrect;
ll delta (int i, int j) {
if (xmx[i] == 0 || ymx[j] == 0) {
return 0ll;
}
//which one is covered?
//0 <= x <= xbound, 0 <= y <= ybound -- how many ways x + y is even?
ll ans = (xeven[i] * yeven[j] + xodd[i] * yodd[j]) * dsqr;
if (xlen[i]) {
//one
//0 <= y < ybound * D
ans += ((xbound[i] & 1) ? yodd[j] : yeven[j]) * D * xlen[i];
//both
if (ylen[j]) {
if ((xbound[i] & 1) == (ybound[j] & 1)) {
ans += xlen[i] * ylen[j];
}
}
}
if (ylen[j]) {
ans += ((ybound[j] & 1) ? xodd[i] : xeven[i]) * D * ylen[j];
}
return ans;
}
ll go() {
dsqr = sqr(D);
ll ans = dsqr * (sqr(N / D) >> 1) - totrect;
for (int i = 0; i < K; i++) {
xmx[0] = X1[i];
xmx[1] = X2[i];
ymx[0] = Y1[i];
ymx[1] = Y2[i];
xbound[0] = xmx[0] / D;
xbound[1] = xmx[1] / D;
ybound[0] = ymx[0] / D;
ybound[1] = ymx[1] / D;
xodd[0] = xbound[0] >> 1;
xeven[0] = xbound[0] - xodd[0];
yodd[0] = ybound[0] >> 1;
yeven[0] = ybound[0] - yodd[0];
xodd[1] = xbound[1] >> 1;
xeven[1] = xbound[1] - xodd[1];
yodd[1] = ybound[1] >> 1;
yeven[1] = ybound[1] - yodd[1];
xlen[0] = xmx[0] - xbound[0] * D;
xlen[1] = xmx[1] - xbound[1] * D;
ylen[0] = ymx[0] - ybound[0] * D;
ylen[1] = ymx[1] - ybound[1] * D;
ans += (delta(0, 0) + delta(1, 1) - delta(0, 1) - delta(1, 0)) << 1;
}
return min(N * N - ans, ans);
}
int main() {
scanf("%lld %d", &N, &K);
for (int i = 0; i < K; i++) {
scanf("%lld %lld %lld %lld", &X1[i], &Y1[i], &X2[i], &Y2[i]);
X1[i]--;
Y1[i]--;
totrect += (X2[i] - X1[i]) * (Y2[i] - Y1[i]);
}
ll ans = ll(N) * N;
for (D = 1; D < N; D++) {
if (N % D == 0) {
setmin(ans, go());
}
}
printf("%lld\n", ans);
}
Compilation message
chessboard.cpp: In function 'int main()':
chessboard.cpp:95:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld %d", &N, &K);
~~~~~^~~~~~~~~~~~~~~~~~~
chessboard.cpp:97:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld %lld %lld %lld", &X1[i], &Y1[i], &X2[i], &Y2[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
356 KB |
Output is correct |
4 |
Correct |
2 ms |
356 KB |
Output is correct |
5 |
Correct |
2 ms |
536 KB |
Output is correct |
6 |
Correct |
2 ms |
536 KB |
Output is correct |
7 |
Correct |
2 ms |
536 KB |
Output is correct |
8 |
Correct |
2 ms |
552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
2580 KB |
Output is correct |
2 |
Correct |
18 ms |
2580 KB |
Output is correct |
3 |
Correct |
29 ms |
2580 KB |
Output is correct |
4 |
Correct |
29 ms |
2580 KB |
Output is correct |
5 |
Correct |
39 ms |
2580 KB |
Output is correct |
6 |
Correct |
25 ms |
2580 KB |
Output is correct |
7 |
Correct |
8 ms |
2580 KB |
Output is correct |
8 |
Correct |
27 ms |
2580 KB |
Output is correct |
9 |
Correct |
61 ms |
3452 KB |
Output is correct |
10 |
Correct |
33 ms |
3452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3452 KB |
Output is correct |
2 |
Correct |
3 ms |
3452 KB |
Output is correct |
3 |
Correct |
2 ms |
3452 KB |
Output is correct |
4 |
Correct |
4 ms |
3452 KB |
Output is correct |
5 |
Correct |
4 ms |
3452 KB |
Output is correct |
6 |
Correct |
3 ms |
3452 KB |
Output is correct |
7 |
Correct |
3 ms |
3452 KB |
Output is correct |
8 |
Correct |
3 ms |
3452 KB |
Output is correct |
9 |
Correct |
3 ms |
3452 KB |
Output is correct |
10 |
Correct |
3 ms |
3452 KB |
Output is correct |
11 |
Correct |
4 ms |
3452 KB |
Output is correct |
12 |
Correct |
3 ms |
3452 KB |
Output is correct |
13 |
Correct |
5 ms |
3452 KB |
Output is correct |
14 |
Correct |
4 ms |
3452 KB |
Output is correct |
15 |
Correct |
4 ms |
3452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3452 KB |
Output is correct |
2 |
Correct |
3 ms |
3452 KB |
Output is correct |
3 |
Correct |
2 ms |
3452 KB |
Output is correct |
4 |
Correct |
4 ms |
3452 KB |
Output is correct |
5 |
Correct |
4 ms |
3452 KB |
Output is correct |
6 |
Correct |
3 ms |
3452 KB |
Output is correct |
7 |
Correct |
3 ms |
3452 KB |
Output is correct |
8 |
Correct |
3 ms |
3452 KB |
Output is correct |
9 |
Correct |
3 ms |
3452 KB |
Output is correct |
10 |
Correct |
3 ms |
3452 KB |
Output is correct |
11 |
Correct |
4 ms |
3452 KB |
Output is correct |
12 |
Correct |
3 ms |
3452 KB |
Output is correct |
13 |
Correct |
5 ms |
3452 KB |
Output is correct |
14 |
Correct |
4 ms |
3452 KB |
Output is correct |
15 |
Correct |
4 ms |
3452 KB |
Output is correct |
16 |
Correct |
25 ms |
3452 KB |
Output is correct |
17 |
Correct |
47 ms |
3452 KB |
Output is correct |
18 |
Correct |
77 ms |
3696 KB |
Output is correct |
19 |
Correct |
359 ms |
3696 KB |
Output is correct |
20 |
Correct |
299 ms |
3836 KB |
Output is correct |
21 |
Correct |
46 ms |
3836 KB |
Output is correct |
22 |
Correct |
4 ms |
3836 KB |
Output is correct |
23 |
Correct |
54 ms |
3836 KB |
Output is correct |
24 |
Correct |
86 ms |
3836 KB |
Output is correct |
25 |
Correct |
11 ms |
3836 KB |
Output is correct |
26 |
Correct |
64 ms |
3836 KB |
Output is correct |
27 |
Correct |
59 ms |
3836 KB |
Output is correct |
28 |
Correct |
74 ms |
3836 KB |
Output is correct |
29 |
Correct |
20 ms |
3836 KB |
Output is correct |
30 |
Correct |
4 ms |
3836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
2580 KB |
Output is correct |
2 |
Correct |
18 ms |
2580 KB |
Output is correct |
3 |
Correct |
29 ms |
2580 KB |
Output is correct |
4 |
Correct |
29 ms |
2580 KB |
Output is correct |
5 |
Correct |
39 ms |
2580 KB |
Output is correct |
6 |
Correct |
25 ms |
2580 KB |
Output is correct |
7 |
Correct |
8 ms |
2580 KB |
Output is correct |
8 |
Correct |
27 ms |
2580 KB |
Output is correct |
9 |
Correct |
61 ms |
3452 KB |
Output is correct |
10 |
Correct |
33 ms |
3452 KB |
Output is correct |
11 |
Correct |
3 ms |
3452 KB |
Output is correct |
12 |
Correct |
3 ms |
3452 KB |
Output is correct |
13 |
Correct |
2 ms |
3452 KB |
Output is correct |
14 |
Correct |
4 ms |
3452 KB |
Output is correct |
15 |
Correct |
4 ms |
3452 KB |
Output is correct |
16 |
Correct |
3 ms |
3452 KB |
Output is correct |
17 |
Correct |
3 ms |
3452 KB |
Output is correct |
18 |
Correct |
3 ms |
3452 KB |
Output is correct |
19 |
Correct |
3 ms |
3452 KB |
Output is correct |
20 |
Correct |
3 ms |
3452 KB |
Output is correct |
21 |
Correct |
4 ms |
3452 KB |
Output is correct |
22 |
Correct |
3 ms |
3452 KB |
Output is correct |
23 |
Correct |
5 ms |
3452 KB |
Output is correct |
24 |
Correct |
4 ms |
3452 KB |
Output is correct |
25 |
Correct |
4 ms |
3452 KB |
Output is correct |
26 |
Correct |
25 ms |
3452 KB |
Output is correct |
27 |
Correct |
47 ms |
3452 KB |
Output is correct |
28 |
Correct |
77 ms |
3696 KB |
Output is correct |
29 |
Correct |
359 ms |
3696 KB |
Output is correct |
30 |
Correct |
299 ms |
3836 KB |
Output is correct |
31 |
Correct |
46 ms |
3836 KB |
Output is correct |
32 |
Correct |
4 ms |
3836 KB |
Output is correct |
33 |
Correct |
54 ms |
3836 KB |
Output is correct |
34 |
Correct |
86 ms |
3836 KB |
Output is correct |
35 |
Correct |
11 ms |
3836 KB |
Output is correct |
36 |
Correct |
64 ms |
3836 KB |
Output is correct |
37 |
Correct |
59 ms |
3836 KB |
Output is correct |
38 |
Correct |
74 ms |
3836 KB |
Output is correct |
39 |
Correct |
20 ms |
3836 KB |
Output is correct |
40 |
Correct |
4 ms |
3836 KB |
Output is correct |
41 |
Correct |
226 ms |
3836 KB |
Output is correct |
42 |
Correct |
77 ms |
3836 KB |
Output is correct |
43 |
Correct |
125 ms |
3836 KB |
Output is correct |
44 |
Correct |
76 ms |
3836 KB |
Output is correct |
45 |
Correct |
64 ms |
3836 KB |
Output is correct |
46 |
Correct |
236 ms |
3836 KB |
Output is correct |
47 |
Correct |
62 ms |
3836 KB |
Output is correct |
48 |
Correct |
101 ms |
3836 KB |
Output is correct |
49 |
Correct |
67 ms |
3836 KB |
Output is correct |
50 |
Correct |
1082 ms |
3836 KB |
Output is correct |
51 |
Correct |
1223 ms |
3836 KB |
Output is correct |
52 |
Correct |
1106 ms |
5628 KB |
Output is correct |
53 |
Correct |
1061 ms |
6372 KB |
Output is correct |
54 |
Correct |
1016 ms |
6372 KB |
Output is correct |
55 |
Correct |
1147 ms |
6508 KB |
Output is correct |
56 |
Correct |
1016 ms |
6508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
356 KB |
Output is correct |
4 |
Correct |
2 ms |
356 KB |
Output is correct |
5 |
Correct |
2 ms |
536 KB |
Output is correct |
6 |
Correct |
2 ms |
536 KB |
Output is correct |
7 |
Correct |
2 ms |
536 KB |
Output is correct |
8 |
Correct |
2 ms |
552 KB |
Output is correct |
9 |
Correct |
53 ms |
2580 KB |
Output is correct |
10 |
Correct |
18 ms |
2580 KB |
Output is correct |
11 |
Correct |
29 ms |
2580 KB |
Output is correct |
12 |
Correct |
29 ms |
2580 KB |
Output is correct |
13 |
Correct |
39 ms |
2580 KB |
Output is correct |
14 |
Correct |
25 ms |
2580 KB |
Output is correct |
15 |
Correct |
8 ms |
2580 KB |
Output is correct |
16 |
Correct |
27 ms |
2580 KB |
Output is correct |
17 |
Correct |
61 ms |
3452 KB |
Output is correct |
18 |
Correct |
33 ms |
3452 KB |
Output is correct |
19 |
Correct |
3 ms |
3452 KB |
Output is correct |
20 |
Correct |
3 ms |
3452 KB |
Output is correct |
21 |
Correct |
2 ms |
3452 KB |
Output is correct |
22 |
Correct |
4 ms |
3452 KB |
Output is correct |
23 |
Correct |
4 ms |
3452 KB |
Output is correct |
24 |
Correct |
3 ms |
3452 KB |
Output is correct |
25 |
Correct |
3 ms |
3452 KB |
Output is correct |
26 |
Correct |
3 ms |
3452 KB |
Output is correct |
27 |
Correct |
3 ms |
3452 KB |
Output is correct |
28 |
Correct |
3 ms |
3452 KB |
Output is correct |
29 |
Correct |
4 ms |
3452 KB |
Output is correct |
30 |
Correct |
3 ms |
3452 KB |
Output is correct |
31 |
Correct |
5 ms |
3452 KB |
Output is correct |
32 |
Correct |
4 ms |
3452 KB |
Output is correct |
33 |
Correct |
4 ms |
3452 KB |
Output is correct |
34 |
Correct |
25 ms |
3452 KB |
Output is correct |
35 |
Correct |
47 ms |
3452 KB |
Output is correct |
36 |
Correct |
77 ms |
3696 KB |
Output is correct |
37 |
Correct |
359 ms |
3696 KB |
Output is correct |
38 |
Correct |
299 ms |
3836 KB |
Output is correct |
39 |
Correct |
46 ms |
3836 KB |
Output is correct |
40 |
Correct |
4 ms |
3836 KB |
Output is correct |
41 |
Correct |
54 ms |
3836 KB |
Output is correct |
42 |
Correct |
86 ms |
3836 KB |
Output is correct |
43 |
Correct |
11 ms |
3836 KB |
Output is correct |
44 |
Correct |
64 ms |
3836 KB |
Output is correct |
45 |
Correct |
59 ms |
3836 KB |
Output is correct |
46 |
Correct |
74 ms |
3836 KB |
Output is correct |
47 |
Correct |
20 ms |
3836 KB |
Output is correct |
48 |
Correct |
4 ms |
3836 KB |
Output is correct |
49 |
Correct |
226 ms |
3836 KB |
Output is correct |
50 |
Correct |
77 ms |
3836 KB |
Output is correct |
51 |
Correct |
125 ms |
3836 KB |
Output is correct |
52 |
Correct |
76 ms |
3836 KB |
Output is correct |
53 |
Correct |
64 ms |
3836 KB |
Output is correct |
54 |
Correct |
236 ms |
3836 KB |
Output is correct |
55 |
Correct |
62 ms |
3836 KB |
Output is correct |
56 |
Correct |
101 ms |
3836 KB |
Output is correct |
57 |
Correct |
67 ms |
3836 KB |
Output is correct |
58 |
Correct |
1082 ms |
3836 KB |
Output is correct |
59 |
Correct |
1223 ms |
3836 KB |
Output is correct |
60 |
Correct |
1106 ms |
5628 KB |
Output is correct |
61 |
Correct |
1061 ms |
6372 KB |
Output is correct |
62 |
Correct |
1016 ms |
6372 KB |
Output is correct |
63 |
Correct |
1147 ms |
6508 KB |
Output is correct |
64 |
Correct |
1016 ms |
6508 KB |
Output is correct |
65 |
Correct |
3 ms |
6508 KB |
Output is correct |
66 |
Correct |
2 ms |
6508 KB |
Output is correct |
67 |
Correct |
1143 ms |
6508 KB |
Output is correct |
68 |
Correct |
1173 ms |
6508 KB |
Output is correct |
69 |
Correct |
1042 ms |
6508 KB |
Output is correct |
70 |
Correct |
1071 ms |
6508 KB |
Output is correct |
71 |
Correct |
1040 ms |
6508 KB |
Output is correct |
72 |
Correct |
1141 ms |
6508 KB |
Output is correct |
73 |
Correct |
1031 ms |
6508 KB |
Output is correct |
74 |
Correct |
1332 ms |
6508 KB |
Output is correct |
75 |
Correct |
1065 ms |
6508 KB |
Output is correct |
76 |
Correct |
1140 ms |
6508 KB |
Output is correct |
77 |
Correct |
203 ms |
6508 KB |
Output is correct |
78 |
Correct |
78 ms |
6508 KB |
Output is correct |
79 |
Correct |
135 ms |
6508 KB |
Output is correct |
80 |
Correct |
142 ms |
6508 KB |
Output is correct |
81 |
Correct |
132 ms |
6508 KB |
Output is correct |
82 |
Correct |
123 ms |
6508 KB |
Output is correct |
83 |
Correct |
92 ms |
6508 KB |
Output is correct |
84 |
Correct |
637 ms |
6508 KB |
Output is correct |
85 |
Correct |
1198 ms |
6508 KB |
Output is correct |
86 |
Correct |
5 ms |
6508 KB |
Output is correct |
87 |
Correct |
4 ms |
6508 KB |
Output is correct |
88 |
Correct |
1156 ms |
8944 KB |
Output is correct |
89 |
Correct |
234 ms |
8944 KB |
Output is correct |
90 |
Correct |
4 ms |
8944 KB |
Output is correct |