#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct point {
ll x, y;
};
// returns number of black ceils on chess board a x b, f - color of first ceil (0 - white, 1 - black)
ll black_nums(ll a, ll b, bool f) {
if(a < 0 || b < 0) return 0;
if(f) return ((a + 1) >> 1) * ((b + 1) >> 1) + (a >> 1) * (b >> 1);
return (a >> 1) * ((b + 1) >> 1) + ((a + 1) >> 1) * (b >> 1);
}
ll func(point p1, point p2, ll d, bool cl) {
if(p1.x == p2.x && p1.y == p2.y) {
return (cl ^ ((p1.x / d + p1.y / d) & 1));
}
ll cp1y = p1.y / d, cp1x = p1.x / d, cp2x = p2.x / d, cp2y = p2.y / d;
ll x1 = p1.x, x2 = p2.x, y1 = p1.y, y2 = p2.y;
ll dx1 = ((d - p1.x % d) % d);
ll dy1 = ((d - p1.y % d) % d);
ll dx2 = ((p2.x + 1) % d);
ll dy2 = ((p2.y + 1) % d);
if((dx1 || dx2) && cp1x == cp2x) dx1 = p2.x - p1.x + 1, dx2 = 0;
x1 += dx1; x2 -= dx2;
if((dx1 || dx2) && cp1y == cp2y) dy1 = p2.y - p1.y + 1, dy2 = 0;
y1 += dy1; y2 -= dy2;
ll cx1 = x1 / d, cy1 = y1 / d;
ll cnt = 0;
cnt += black_nums((x2 - x1 + 1) / d, (y2 - y1 + 1) / d, (cl ^ ((cx1 + cy1) & 1))) * d * d;
cnt += black_nums((x2 - x1 + 1) / d, 1, (cl ^ ((cx1 + cp1y) & 1))) * dy1 * d;
cnt += black_nums((x2 - x1 + 1) / d, 1, (cl ^ ((cx1 + cp2y) & 1))) * dy2 * d;
cnt += black_nums(1, (y2 - y1 + 1) / d, (cl ^ ((cp1x + cy1) & 1))) * dx1 * d;
cnt += black_nums(1, (y2 - y1 + 1) / d, (cl ^ ((cp2x + cy1) & 1))) * dx2 * d;
cnt += (cl ^ ((cp1x + cp1y) & 1)) * dx1 * dy1;
if(p2.x > p1.x) cnt += (cl ^ ((cp2x + cp1y) & 1)) * dx2 * dy1;
if(p2.y > p1.y) cnt += (cl ^ ((cp1x + cp2y) & 1)) * dx1 * dy2;
if(p2.x > p1.x && p2.y > p1.y) cnt += (cl ^ ((cp2x + cp2y) & 1)) * dx2 * dy2;
return cnt;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
ll n, k;
cin >> n >> k;
point v1[k], v2[k];
for(int i = 0; i < k; i++) {
point p1, p2;
cin >> p1.x >> p1.y;
p1.x--; p1.y--;
cin >> p2.x >> p2.y;
p2.x--; p2.y--;
v1[i] = p1; v2[i] = p2;
}
ll ans = n * n;
for(ll D = 1; D < n; D++) {
if(n % D) continue;
ll d = D;
ll val1 = 0, val2 = 0;
val1 = black_nums(n / d, n / d, 1) * d * d;
val2 = black_nums(n / d, n / d, 0) * d * d;
for(int i = 0; i < k; i++) {
val1 += (v2[i].x - v1[i].x + 1) * (v2[i].y - v1[i].y + 1) - 2 * func(v1[i], v2[i], d, 1);
val2 += (v2[i].x - v1[i].x + 1) * (v2[i].y - v1[i].y + 1) - 2 * func(v1[i], v2[i], d, 0);
}
ans = min(ans, val1);
ans = min(ans, val2);
// if(D == 1) continue;
// d = n / D;
// val1 = black_nums(n / d, n / d, 1) * d * d;
// val2 = black_nums(n / d, n / d, 0) * d * d;
// for(int i = 0; i < k; i++) {
// val1 += (v2[i].x - v1[i].x + 1) * (v2[i].y - v1[i].y + 1) - 2 * func(v1[i], v2[i], d, 1);
// val2 += (v2[i].x - v1[i].x + 1) * (v2[i].y - v1[i].y + 1) - 2 * func(v1[i], v2[i], d, 0);
// }
// ans = min(ans, val1);
// ans = min(ans, val2);
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
2276 KB |
Output is correct |
2 |
Correct |
7 ms |
724 KB |
Output is correct |
3 |
Correct |
16 ms |
1548 KB |
Output is correct |
4 |
Correct |
13 ms |
1748 KB |
Output is correct |
5 |
Correct |
17 ms |
2020 KB |
Output is correct |
6 |
Correct |
11 ms |
1364 KB |
Output is correct |
7 |
Correct |
3 ms |
468 KB |
Output is correct |
8 |
Correct |
11 ms |
1364 KB |
Output is correct |
9 |
Correct |
29 ms |
3144 KB |
Output is correct |
10 |
Correct |
17 ms |
1888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
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 |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
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 |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 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 |
9 ms |
1180 KB |
Output is correct |
17 |
Correct |
23 ms |
2952 KB |
Output is correct |
18 |
Correct |
37 ms |
3284 KB |
Output is correct |
19 |
Correct |
71 ms |
3028 KB |
Output is correct |
20 |
Correct |
79 ms |
3380 KB |
Output is correct |
21 |
Correct |
22 ms |
2856 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
18 ms |
1640 KB |
Output is correct |
24 |
Correct |
26 ms |
3028 KB |
Output is correct |
25 |
Correct |
4 ms |
468 KB |
Output is correct |
26 |
Correct |
22 ms |
2004 KB |
Output is correct |
27 |
Correct |
23 ms |
2444 KB |
Output is correct |
28 |
Correct |
28 ms |
3284 KB |
Output is correct |
29 |
Correct |
14 ms |
1356 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
2276 KB |
Output is correct |
2 |
Correct |
7 ms |
724 KB |
Output is correct |
3 |
Correct |
16 ms |
1548 KB |
Output is correct |
4 |
Correct |
13 ms |
1748 KB |
Output is correct |
5 |
Correct |
17 ms |
2020 KB |
Output is correct |
6 |
Correct |
11 ms |
1364 KB |
Output is correct |
7 |
Correct |
3 ms |
468 KB |
Output is correct |
8 |
Correct |
11 ms |
1364 KB |
Output is correct |
9 |
Correct |
29 ms |
3144 KB |
Output is correct |
10 |
Correct |
17 ms |
1888 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
0 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 |
9 ms |
1180 KB |
Output is correct |
27 |
Correct |
23 ms |
2952 KB |
Output is correct |
28 |
Correct |
37 ms |
3284 KB |
Output is correct |
29 |
Correct |
71 ms |
3028 KB |
Output is correct |
30 |
Correct |
79 ms |
3380 KB |
Output is correct |
31 |
Correct |
22 ms |
2856 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
18 ms |
1640 KB |
Output is correct |
34 |
Correct |
26 ms |
3028 KB |
Output is correct |
35 |
Correct |
4 ms |
468 KB |
Output is correct |
36 |
Correct |
22 ms |
2004 KB |
Output is correct |
37 |
Correct |
23 ms |
2444 KB |
Output is correct |
38 |
Correct |
28 ms |
3284 KB |
Output is correct |
39 |
Correct |
14 ms |
1356 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
67 ms |
2900 KB |
Output is correct |
42 |
Correct |
33 ms |
3156 KB |
Output is correct |
43 |
Correct |
54 ms |
2900 KB |
Output is correct |
44 |
Correct |
32 ms |
3156 KB |
Output is correct |
45 |
Correct |
29 ms |
3364 KB |
Output is correct |
46 |
Correct |
82 ms |
3156 KB |
Output is correct |
47 |
Correct |
26 ms |
2900 KB |
Output is correct |
48 |
Correct |
40 ms |
3032 KB |
Output is correct |
49 |
Correct |
32 ms |
2916 KB |
Output is correct |
50 |
Correct |
284 ms |
3132 KB |
Output is correct |
51 |
Correct |
308 ms |
3332 KB |
Output is correct |
52 |
Correct |
286 ms |
3028 KB |
Output is correct |
53 |
Correct |
301 ms |
3304 KB |
Output is correct |
54 |
Correct |
295 ms |
3028 KB |
Output is correct |
55 |
Correct |
304 ms |
3416 KB |
Output is correct |
56 |
Correct |
286 ms |
2988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
22 ms |
2276 KB |
Output is correct |
10 |
Correct |
7 ms |
724 KB |
Output is correct |
11 |
Correct |
16 ms |
1548 KB |
Output is correct |
12 |
Correct |
13 ms |
1748 KB |
Output is correct |
13 |
Correct |
17 ms |
2020 KB |
Output is correct |
14 |
Correct |
11 ms |
1364 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
11 ms |
1364 KB |
Output is correct |
17 |
Correct |
29 ms |
3144 KB |
Output is correct |
18 |
Correct |
17 ms |
1888 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
0 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 |
9 ms |
1180 KB |
Output is correct |
35 |
Correct |
23 ms |
2952 KB |
Output is correct |
36 |
Correct |
37 ms |
3284 KB |
Output is correct |
37 |
Correct |
71 ms |
3028 KB |
Output is correct |
38 |
Correct |
79 ms |
3380 KB |
Output is correct |
39 |
Correct |
22 ms |
2856 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
18 ms |
1640 KB |
Output is correct |
42 |
Correct |
26 ms |
3028 KB |
Output is correct |
43 |
Correct |
4 ms |
468 KB |
Output is correct |
44 |
Correct |
22 ms |
2004 KB |
Output is correct |
45 |
Correct |
23 ms |
2444 KB |
Output is correct |
46 |
Correct |
28 ms |
3284 KB |
Output is correct |
47 |
Correct |
14 ms |
1356 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
67 ms |
2900 KB |
Output is correct |
50 |
Correct |
33 ms |
3156 KB |
Output is correct |
51 |
Correct |
54 ms |
2900 KB |
Output is correct |
52 |
Correct |
32 ms |
3156 KB |
Output is correct |
53 |
Correct |
29 ms |
3364 KB |
Output is correct |
54 |
Correct |
82 ms |
3156 KB |
Output is correct |
55 |
Correct |
26 ms |
2900 KB |
Output is correct |
56 |
Correct |
40 ms |
3032 KB |
Output is correct |
57 |
Correct |
32 ms |
2916 KB |
Output is correct |
58 |
Correct |
284 ms |
3132 KB |
Output is correct |
59 |
Correct |
308 ms |
3332 KB |
Output is correct |
60 |
Correct |
286 ms |
3028 KB |
Output is correct |
61 |
Correct |
301 ms |
3304 KB |
Output is correct |
62 |
Correct |
295 ms |
3028 KB |
Output is correct |
63 |
Correct |
304 ms |
3416 KB |
Output is correct |
64 |
Correct |
286 ms |
2988 KB |
Output is correct |
65 |
Correct |
1 ms |
212 KB |
Output is correct |
66 |
Correct |
0 ms |
212 KB |
Output is correct |
67 |
Correct |
1430 ms |
3232 KB |
Output is correct |
68 |
Correct |
1425 ms |
3212 KB |
Output is correct |
69 |
Correct |
1235 ms |
2836 KB |
Output is correct |
70 |
Correct |
1278 ms |
3148 KB |
Output is correct |
71 |
Correct |
1315 ms |
3064 KB |
Output is correct |
72 |
Correct |
1300 ms |
3000 KB |
Output is correct |
73 |
Correct |
1298 ms |
2956 KB |
Output is correct |
74 |
Correct |
1435 ms |
3188 KB |
Output is correct |
75 |
Correct |
1341 ms |
3028 KB |
Output is correct |
76 |
Correct |
1443 ms |
3228 KB |
Output is correct |
77 |
Correct |
208 ms |
3372 KB |
Output is correct |
78 |
Correct |
58 ms |
3156 KB |
Output is correct |
79 |
Correct |
134 ms |
2960 KB |
Output is correct |
80 |
Correct |
164 ms |
3064 KB |
Output is correct |
81 |
Correct |
132 ms |
2852 KB |
Output is correct |
82 |
Correct |
105 ms |
3252 KB |
Output is correct |
83 |
Correct |
76 ms |
2972 KB |
Output is correct |
84 |
Correct |
833 ms |
3308 KB |
Output is correct |
85 |
Correct |
1471 ms |
3412 KB |
Output is correct |
86 |
Correct |
3 ms |
212 KB |
Output is correct |
87 |
Correct |
1 ms |
212 KB |
Output is correct |
88 |
Correct |
1490 ms |
3412 KB |
Output is correct |
89 |
Correct |
304 ms |
852 KB |
Output is correct |
90 |
Correct |
1 ms |
212 KB |
Output is correct |