#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct point {
ll x, y;
// point(ll x, ll y) : x(x), y(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) / 2) * ((b + 1) / 2) + (a / 2) * (b / 2);
return (a / 2) * ((b + 1) / 2) + ((a + 1) / 2) * (b / 2);
}
ll func(point p1, point p2, ll d, bool cl) {
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);
// cout << x1 << ' ' << y1 << ' ' << x2 << ' ' << y2 << '\n';
if((dx1 || dx2) && p1.x / d == p2.x / d) dx1 = p2.x - p1.x + 1, dx2 = 0;
x1 += dx1; x2 -= dx2;
if((dx1 || dx2) && p1.y / d == p2.y / d) dy1 = p2.y - p1.y + 1, dy2 = 0;
y1 += dy1; y2 -= dy2;
// cout << x1 << ' ' << y1 << ' ' << x2 << ' ' << y2 << '\n';
// cout << dx1 << ' ' << dy1 << ' ' << dx2 << ' ' << dy2 << '\n';
ll cnt = 0;
cnt += black_nums((x2 - x1 + 1) / d, (y2 - y1 + 1) / d, (cl ^ ((x1 / d + y1 / d) % 2))) * d * d;
cnt += black_nums((x2 - x1 + 1) / d, 1, (cl ^ ((x1 / d + p1.y / d) % 2))) * dy1 * d;
cnt += black_nums((x2 - x1 + 1) / d, 1, (cl ^ ((x1 / d + p2.y / d) % 2))) * dy2 * d;
cnt += black_nums(1, (y2 - y1 + 1) / d, (cl ^ ((p1.x / d + y1 / d) % 2))) * dx1 * d;
cnt += black_nums(1, (y2 - y1 + 1) / d, (cl ^ ((p2.x / d + y1 / d) % 2))) * dx2 * d;
cnt += (cl ^ ((p1.x / d + p1.y / d) % 2)) * dx1 * dy1;
if(p2.x > p1.x) cnt += (cl ^ ((p2.x / d + p1.y / d) % 2)) * dx2 * dy1;
if(p2.y > p1.y) cnt += (cl ^ ((p1.x / d + p2.y / d) % 2)) * dx1 * dy2;
if(p2.x > p1.x && p2.y > p1.y) cnt += (cl ^ ((p2.x / d + p2.y / d) % 2)) * dx2 * dy2;
return cnt;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
ll n, k;
cin >> n >> k;
vector<pair<point, point>> v;
for(int i = 1; i <= k; i++) {
point p1, p2;
cin >> p1.x >> p1.y;
p1.x--; p1.y--;
cin >> p2.x >> p2.y;
p2.x--; p2.y--;
v.push_back(make_pair(p1, p2));
}
ll ans = n * n;
for(ll D = 1; D * D <= n; D++) {
if(n % D) continue;
int d = D;
ll val = 0;
val = black_nums(n / d, n / d, 1) * d * d;
for(auto [p1, p2] : v) {
long long cnt = func(p1, p2, d, 1);
val += (p2.x - p1.x + 1) * (p2.y - p1.y + 1) - 2 * cnt;
}
ans = min(ans, val);
val = black_nums(n / d, n / d, 0) * d * d;
for(auto [p1, p2] : v) {
long long cnt = func(p1, p2, d, 0);
val += (p2.x - p1.x + 1) * (p2.y - p1.y + 1) - 2 * cnt;
}
ans = min(ans, val);
if(D == 1) continue;
d = n / D;
val = black_nums(n / d, n / d, 1) * d * d;
for(auto [p1, p2] : v) {
long long cnt = func(p1, p2, d, 1);
val += (p2.x - p1.x + 1) * (p2.y - p1.y + 1) - 2 * cnt;
}
ans = min(ans, val);
val = black_nums(n / d, n / d, 0) * d * d;
for(auto [p1, p2] : v) {
long long cnt = func(p1, p2, d, 0);
val += (p2.x - p1.x + 1) * (p2.y - p1.y + 1) - 2 * cnt;
}
ans = min(ans, val);
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
316 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
2684 KB |
Output is correct |
2 |
Correct |
8 ms |
1748 KB |
Output is correct |
3 |
Correct |
22 ms |
2752 KB |
Output is correct |
4 |
Correct |
19 ms |
2828 KB |
Output is correct |
5 |
Correct |
25 ms |
2820 KB |
Output is correct |
6 |
Correct |
17 ms |
2888 KB |
Output is correct |
7 |
Correct |
4 ms |
856 KB |
Output is correct |
8 |
Correct |
20 ms |
2816 KB |
Output is correct |
9 |
Correct |
43 ms |
4912 KB |
Output is correct |
10 |
Correct |
27 ms |
2760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
288 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
288 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
22 ms |
1796 KB |
Output is correct |
17 |
Correct |
34 ms |
5048 KB |
Output is correct |
18 |
Correct |
66 ms |
4840 KB |
Output is correct |
19 |
Correct |
372 ms |
4868 KB |
Output is correct |
20 |
Correct |
386 ms |
4980 KB |
Output is correct |
21 |
Correct |
35 ms |
5060 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
53 ms |
2888 KB |
Output is correct |
24 |
Correct |
75 ms |
4880 KB |
Output is correct |
25 |
Correct |
14 ms |
1112 KB |
Output is correct |
26 |
Correct |
43 ms |
2932 KB |
Output is correct |
27 |
Correct |
64 ms |
4888 KB |
Output is correct |
28 |
Correct |
68 ms |
4824 KB |
Output is correct |
29 |
Correct |
15 ms |
2788 KB |
Output is correct |
30 |
Correct |
3 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
2684 KB |
Output is correct |
2 |
Correct |
8 ms |
1748 KB |
Output is correct |
3 |
Correct |
22 ms |
2752 KB |
Output is correct |
4 |
Correct |
19 ms |
2828 KB |
Output is correct |
5 |
Correct |
25 ms |
2820 KB |
Output is correct |
6 |
Correct |
17 ms |
2888 KB |
Output is correct |
7 |
Correct |
4 ms |
856 KB |
Output is correct |
8 |
Correct |
20 ms |
2816 KB |
Output is correct |
9 |
Correct |
43 ms |
4912 KB |
Output is correct |
10 |
Correct |
27 ms |
2760 KB |
Output is correct |
11 |
Correct |
2 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
316 KB |
Output is correct |
13 |
Correct |
1 ms |
288 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
320 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
328 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
324 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
22 ms |
1796 KB |
Output is correct |
27 |
Correct |
34 ms |
5048 KB |
Output is correct |
28 |
Correct |
66 ms |
4840 KB |
Output is correct |
29 |
Correct |
372 ms |
4868 KB |
Output is correct |
30 |
Correct |
386 ms |
4980 KB |
Output is correct |
31 |
Correct |
35 ms |
5060 KB |
Output is correct |
32 |
Correct |
2 ms |
340 KB |
Output is correct |
33 |
Correct |
53 ms |
2888 KB |
Output is correct |
34 |
Correct |
75 ms |
4880 KB |
Output is correct |
35 |
Correct |
14 ms |
1112 KB |
Output is correct |
36 |
Correct |
43 ms |
2932 KB |
Output is correct |
37 |
Correct |
64 ms |
4888 KB |
Output is correct |
38 |
Correct |
68 ms |
4824 KB |
Output is correct |
39 |
Correct |
15 ms |
2788 KB |
Output is correct |
40 |
Correct |
3 ms |
468 KB |
Output is correct |
41 |
Correct |
316 ms |
5168 KB |
Output is correct |
42 |
Correct |
79 ms |
5100 KB |
Output is correct |
43 |
Correct |
164 ms |
5096 KB |
Output is correct |
44 |
Correct |
70 ms |
5064 KB |
Output is correct |
45 |
Correct |
45 ms |
5084 KB |
Output is correct |
46 |
Correct |
342 ms |
5264 KB |
Output is correct |
47 |
Correct |
43 ms |
5160 KB |
Output is correct |
48 |
Correct |
113 ms |
5056 KB |
Output is correct |
49 |
Correct |
64 ms |
5060 KB |
Output is correct |
50 |
Correct |
1648 ms |
4904 KB |
Output is correct |
51 |
Correct |
1771 ms |
5092 KB |
Output is correct |
52 |
Correct |
1648 ms |
5060 KB |
Output is correct |
53 |
Correct |
1784 ms |
4932 KB |
Output is correct |
54 |
Correct |
1647 ms |
5168 KB |
Output is correct |
55 |
Correct |
1806 ms |
4804 KB |
Output is correct |
56 |
Correct |
1533 ms |
5052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
316 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
31 ms |
2684 KB |
Output is correct |
10 |
Correct |
8 ms |
1748 KB |
Output is correct |
11 |
Correct |
22 ms |
2752 KB |
Output is correct |
12 |
Correct |
19 ms |
2828 KB |
Output is correct |
13 |
Correct |
25 ms |
2820 KB |
Output is correct |
14 |
Correct |
17 ms |
2888 KB |
Output is correct |
15 |
Correct |
4 ms |
856 KB |
Output is correct |
16 |
Correct |
20 ms |
2816 KB |
Output is correct |
17 |
Correct |
43 ms |
4912 KB |
Output is correct |
18 |
Correct |
27 ms |
2760 KB |
Output is correct |
19 |
Correct |
2 ms |
320 KB |
Output is correct |
20 |
Correct |
1 ms |
316 KB |
Output is correct |
21 |
Correct |
1 ms |
288 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
320 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
328 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
324 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
340 KB |
Output is correct |
32 |
Correct |
2 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
22 ms |
1796 KB |
Output is correct |
35 |
Correct |
34 ms |
5048 KB |
Output is correct |
36 |
Correct |
66 ms |
4840 KB |
Output is correct |
37 |
Correct |
372 ms |
4868 KB |
Output is correct |
38 |
Correct |
386 ms |
4980 KB |
Output is correct |
39 |
Correct |
35 ms |
5060 KB |
Output is correct |
40 |
Correct |
2 ms |
340 KB |
Output is correct |
41 |
Correct |
53 ms |
2888 KB |
Output is correct |
42 |
Correct |
75 ms |
4880 KB |
Output is correct |
43 |
Correct |
14 ms |
1112 KB |
Output is correct |
44 |
Correct |
43 ms |
2932 KB |
Output is correct |
45 |
Correct |
64 ms |
4888 KB |
Output is correct |
46 |
Correct |
68 ms |
4824 KB |
Output is correct |
47 |
Correct |
15 ms |
2788 KB |
Output is correct |
48 |
Correct |
3 ms |
468 KB |
Output is correct |
49 |
Correct |
316 ms |
5168 KB |
Output is correct |
50 |
Correct |
79 ms |
5100 KB |
Output is correct |
51 |
Correct |
164 ms |
5096 KB |
Output is correct |
52 |
Correct |
70 ms |
5064 KB |
Output is correct |
53 |
Correct |
45 ms |
5084 KB |
Output is correct |
54 |
Correct |
342 ms |
5264 KB |
Output is correct |
55 |
Correct |
43 ms |
5160 KB |
Output is correct |
56 |
Correct |
113 ms |
5056 KB |
Output is correct |
57 |
Correct |
64 ms |
5060 KB |
Output is correct |
58 |
Correct |
1648 ms |
4904 KB |
Output is correct |
59 |
Correct |
1771 ms |
5092 KB |
Output is correct |
60 |
Correct |
1648 ms |
5060 KB |
Output is correct |
61 |
Correct |
1784 ms |
4932 KB |
Output is correct |
62 |
Correct |
1647 ms |
5168 KB |
Output is correct |
63 |
Correct |
1806 ms |
4804 KB |
Output is correct |
64 |
Correct |
1533 ms |
5052 KB |
Output is correct |
65 |
Correct |
0 ms |
212 KB |
Output is correct |
66 |
Correct |
0 ms |
212 KB |
Output is correct |
67 |
Correct |
1862 ms |
6240 KB |
Output is correct |
68 |
Correct |
1837 ms |
6376 KB |
Output is correct |
69 |
Correct |
1595 ms |
6348 KB |
Output is correct |
70 |
Correct |
1808 ms |
6340 KB |
Output is correct |
71 |
Correct |
1771 ms |
6232 KB |
Output is correct |
72 |
Correct |
1708 ms |
6344 KB |
Output is correct |
73 |
Correct |
1687 ms |
6348 KB |
Output is correct |
74 |
Correct |
1795 ms |
6292 KB |
Output is correct |
75 |
Correct |
1736 ms |
6336 KB |
Output is correct |
76 |
Correct |
1834 ms |
6340 KB |
Output is correct |
77 |
Correct |
260 ms |
6236 KB |
Output is correct |
78 |
Correct |
69 ms |
6132 KB |
Output is correct |
79 |
Correct |
174 ms |
6328 KB |
Output is correct |
80 |
Correct |
182 ms |
6332 KB |
Output is correct |
81 |
Correct |
168 ms |
6260 KB |
Output is correct |
82 |
Correct |
129 ms |
6252 KB |
Output is correct |
83 |
Correct |
92 ms |
6232 KB |
Output is correct |
84 |
Correct |
1065 ms |
6236 KB |
Output is correct |
85 |
Correct |
1951 ms |
6244 KB |
Output is correct |
86 |
Correct |
2 ms |
212 KB |
Output is correct |
87 |
Correct |
1 ms |
212 KB |
Output is correct |
88 |
Correct |
1979 ms |
6284 KB |
Output is correct |
89 |
Correct |
371 ms |
1864 KB |
Output is correct |
90 |
Correct |
1 ms |
320 KB |
Output is correct |