#include <bits/stdc++.h>
#define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << "Line(" << __LINE__ << ") -> " << #x << " is " << x << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
const int INF = 1e9 + 7;
const ll oo = 1e18;
signed main()
{
IO_OP;
int n, k;
cin >> n >> k;
V<array<int, 4>> a(k);
for(int i = 0; i < k; i++) {
for(int j = 0; j < 4; j++)
cin >> a[i][j], a[i][j]--;
}
ll ans = oo;
auto go = [&] (int b) {
assert(n % b == 0);
int m = n / b;
ll black = (ll) m * m / 2 * b * b, white = (ll) n * n - black;
int black_type = 1;
for(int _ = 0; _ < 2; _++) {
ll in_black = 0, in_white = 0;
for(int i = 0; i < k; i++) {
function<void(int, int, int, int)> solve = [&] (int x1, int y1, int x2, int y2) {
if(x1 > x2 || y1 > y2) return;
// int x1 = a[i][0], y1 = a[i][1];
// int x2 = a[i][2], y2 = a[i][3];
if(x1 / b == x2 / b && y1 / b == y2 / b) {
if((x1 / b + y1 / b) % 2 == black_type)
in_black += (ll) (x2 - x1 + 1) * (y2 - y1 + 1);
else
in_white += (ll) (x2 - x1 + 1) * (y2 - y1 + 1);
} else if(y1 / b == y2 / b || x1 / b == x2 / b) {
if(y1 / b == y2 / b) {
swap(x1, y1);
swap(x2, y2);
}
int bl = y1 / b + 1;
int br = y2 / b - 1;
ll l_rem = (ll) (bl * b - y1) * (x2 - x1 + 1);
ll r_rem = (ll) (y2 - br * b) * (x2 - x1 + 1);
if((x1 / b + y1 / b) % 2 == black_type) in_black += l_rem;
else in_white += l_rem;
if((x2 / b + y2 / b) % 2 == black_type) in_black += r_rem;
else in_white += r_rem;
if((br - bl + 1) % 2 == 1) {
if((bl + x1 / b) % 2 == black_type)
in_black += (ll) b * (x2 - x1 + 1);
else
in_white += (ll) b * (x2 - x1 + 1);
}
} else {
int bl = y1 / b + 1, br = y2 / b - 1;
int bu = x1 / b + 1, bd = x2 / b - 1;
solve(x1, bl * b, bu * b - 1, br * b + b - 1);
solve(br * b + b, bl * b, x2, br * b + b - 1);
solve(bu * b, y1, bd * b + b - 1, bl * b - 1);
solve(bu * b, br * b + b, bd * b + b - 1, y2);
solve(x1, y1, bl * b - 1, br * b - 1);
solve(x1, br * b + b, bl * b - 1, y2);
solve(bd * b + b, y1, x2, bl * b - 1);
solve(bd * b + b, br * b + b, x2, y2);
if((br - bl + 1) % 2 == 1 && (bd - bu + 1) % 2 == 1) {
if((bl + bu) % 2 == black_type)
in_black += (ll) b * b;
else
in_white += (ll) b * b;
}
}
};
int x1 = a[i][0], y1 = a[i][1];
int x2 = a[i][2], y2 = a[i][3];
solve(x1, y1, x2, y2);
}
assert(in_black <= black);
assert(in_white <= white);
ans = min(ans, in_white + black - in_black);
swap(black, white);
black_type ^= 1;
}
};
for(int i = 1; i * i <= n; i++) {
if(n % i == 0) {
go(i);
if(n / i != i && n / i != n) go(n / i);
}
}
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
1260 KB |
Output is correct |
2 |
Correct |
10 ms |
620 KB |
Output is correct |
3 |
Correct |
20 ms |
1004 KB |
Output is correct |
4 |
Correct |
26 ms |
1004 KB |
Output is correct |
5 |
Correct |
27 ms |
1132 KB |
Output is correct |
6 |
Correct |
17 ms |
876 KB |
Output is correct |
7 |
Correct |
5 ms |
492 KB |
Output is correct |
8 |
Correct |
17 ms |
876 KB |
Output is correct |
9 |
Correct |
45 ms |
1772 KB |
Output is correct |
10 |
Correct |
25 ms |
1132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
2 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
2 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
18 ms |
748 KB |
Output is correct |
17 |
Correct |
38 ms |
1676 KB |
Output is correct |
18 |
Correct |
68 ms |
1900 KB |
Output is correct |
19 |
Correct |
290 ms |
1772 KB |
Output is correct |
20 |
Correct |
323 ms |
1900 KB |
Output is correct |
21 |
Correct |
36 ms |
1644 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
59 ms |
1004 KB |
Output is correct |
24 |
Correct |
59 ms |
1772 KB |
Output is correct |
25 |
Correct |
10 ms |
492 KB |
Output is correct |
26 |
Correct |
40 ms |
1260 KB |
Output is correct |
27 |
Correct |
69 ms |
1388 KB |
Output is correct |
28 |
Correct |
61 ms |
1772 KB |
Output is correct |
29 |
Correct |
19 ms |
876 KB |
Output is correct |
30 |
Correct |
2 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
1260 KB |
Output is correct |
2 |
Correct |
10 ms |
620 KB |
Output is correct |
3 |
Correct |
20 ms |
1004 KB |
Output is correct |
4 |
Correct |
26 ms |
1004 KB |
Output is correct |
5 |
Correct |
27 ms |
1132 KB |
Output is correct |
6 |
Correct |
17 ms |
876 KB |
Output is correct |
7 |
Correct |
5 ms |
492 KB |
Output is correct |
8 |
Correct |
17 ms |
876 KB |
Output is correct |
9 |
Correct |
45 ms |
1772 KB |
Output is correct |
10 |
Correct |
25 ms |
1132 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
2 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
2 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
18 ms |
748 KB |
Output is correct |
27 |
Correct |
38 ms |
1676 KB |
Output is correct |
28 |
Correct |
68 ms |
1900 KB |
Output is correct |
29 |
Correct |
290 ms |
1772 KB |
Output is correct |
30 |
Correct |
323 ms |
1900 KB |
Output is correct |
31 |
Correct |
36 ms |
1644 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
59 ms |
1004 KB |
Output is correct |
34 |
Correct |
59 ms |
1772 KB |
Output is correct |
35 |
Correct |
10 ms |
492 KB |
Output is correct |
36 |
Correct |
40 ms |
1260 KB |
Output is correct |
37 |
Correct |
69 ms |
1388 KB |
Output is correct |
38 |
Correct |
61 ms |
1772 KB |
Output is correct |
39 |
Correct |
19 ms |
876 KB |
Output is correct |
40 |
Correct |
2 ms |
364 KB |
Output is correct |
41 |
Correct |
238 ms |
1644 KB |
Output is correct |
42 |
Correct |
71 ms |
1772 KB |
Output is correct |
43 |
Correct |
143 ms |
1644 KB |
Output is correct |
44 |
Correct |
69 ms |
1772 KB |
Output is correct |
45 |
Correct |
63 ms |
1900 KB |
Output is correct |
46 |
Correct |
278 ms |
1772 KB |
Output is correct |
47 |
Correct |
42 ms |
2284 KB |
Output is correct |
48 |
Correct |
100 ms |
2284 KB |
Output is correct |
49 |
Correct |
66 ms |
2284 KB |
Output is correct |
50 |
Correct |
1318 ms |
2540 KB |
Output is correct |
51 |
Correct |
1422 ms |
2668 KB |
Output is correct |
52 |
Correct |
1301 ms |
2540 KB |
Output is correct |
53 |
Correct |
1437 ms |
2540 KB |
Output is correct |
54 |
Correct |
1283 ms |
2412 KB |
Output is correct |
55 |
Correct |
1472 ms |
2668 KB |
Output is correct |
56 |
Correct |
1256 ms |
2548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
33 ms |
1260 KB |
Output is correct |
10 |
Correct |
10 ms |
620 KB |
Output is correct |
11 |
Correct |
20 ms |
1004 KB |
Output is correct |
12 |
Correct |
26 ms |
1004 KB |
Output is correct |
13 |
Correct |
27 ms |
1132 KB |
Output is correct |
14 |
Correct |
17 ms |
876 KB |
Output is correct |
15 |
Correct |
5 ms |
492 KB |
Output is correct |
16 |
Correct |
17 ms |
876 KB |
Output is correct |
17 |
Correct |
45 ms |
1772 KB |
Output is correct |
18 |
Correct |
25 ms |
1132 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
2 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
2 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
18 ms |
748 KB |
Output is correct |
35 |
Correct |
38 ms |
1676 KB |
Output is correct |
36 |
Correct |
68 ms |
1900 KB |
Output is correct |
37 |
Correct |
290 ms |
1772 KB |
Output is correct |
38 |
Correct |
323 ms |
1900 KB |
Output is correct |
39 |
Correct |
36 ms |
1644 KB |
Output is correct |
40 |
Correct |
1 ms |
364 KB |
Output is correct |
41 |
Correct |
59 ms |
1004 KB |
Output is correct |
42 |
Correct |
59 ms |
1772 KB |
Output is correct |
43 |
Correct |
10 ms |
492 KB |
Output is correct |
44 |
Correct |
40 ms |
1260 KB |
Output is correct |
45 |
Correct |
69 ms |
1388 KB |
Output is correct |
46 |
Correct |
61 ms |
1772 KB |
Output is correct |
47 |
Correct |
19 ms |
876 KB |
Output is correct |
48 |
Correct |
2 ms |
364 KB |
Output is correct |
49 |
Correct |
238 ms |
1644 KB |
Output is correct |
50 |
Correct |
71 ms |
1772 KB |
Output is correct |
51 |
Correct |
143 ms |
1644 KB |
Output is correct |
52 |
Correct |
69 ms |
1772 KB |
Output is correct |
53 |
Correct |
63 ms |
1900 KB |
Output is correct |
54 |
Correct |
278 ms |
1772 KB |
Output is correct |
55 |
Correct |
42 ms |
2284 KB |
Output is correct |
56 |
Correct |
100 ms |
2284 KB |
Output is correct |
57 |
Correct |
66 ms |
2284 KB |
Output is correct |
58 |
Correct |
1318 ms |
2540 KB |
Output is correct |
59 |
Correct |
1422 ms |
2668 KB |
Output is correct |
60 |
Correct |
1301 ms |
2540 KB |
Output is correct |
61 |
Correct |
1437 ms |
2540 KB |
Output is correct |
62 |
Correct |
1283 ms |
2412 KB |
Output is correct |
63 |
Correct |
1472 ms |
2668 KB |
Output is correct |
64 |
Correct |
1256 ms |
2548 KB |
Output is correct |
65 |
Correct |
1 ms |
364 KB |
Output is correct |
66 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
67 |
Halted |
0 ms |
0 KB |
- |