#include <bits/stdc++.h>
#ifdef local
#define safe std::cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
#define debug(a...) qqbx(#a, a)
template <typename ...T> void qqbx(const char *s, T ...a) {
int cnt = sizeof...(T);
((std::cerr << "\033[1;32m(" << s << ") = (") , ... , (std::cerr << a << (--cnt ? ", " : ")\033[0m\n")));
}
#else
#define safe ((void)0)
#define debug(...) ((void)0)
#endif // local
using namespace std;
using ll = int64_t;
const int maxn = 100025;
const ll INF = 1e18;
bool isBlack(int x, int y) {
return (x + y) & 1;
}
ll calc2d(int x, int y, int k, bool c) {
if (x < 0 || y < 0) return 0;
++x, ++y;
ll qx = x / k, rx = x % k;
ll qy = y / k, ry = y % k;
ll ans = 0;
ans += ry * k * (isBlack(0, qy) == c ? (qx + 1) / 2 : qx / 2);
ans += rx * k * (isBlack(qx, 0) == c ? (qy + 1) / 2 : qy / 2);
ans += k * k * (isBlack(0, 0) == c ? (qx * qy + 1) / 2 : qx * qy / 2);
// debug((isBlack(0, qy+1) == c ? (qx + 1) / 2 : qx / 2));
debug(rx , k , (isBlack(qx, 0) == c ? (qy + 1) / 2 : qy / 2));
// debug((isBlack(0, 0) == c ? (qx * qy + 1) / 2 : qx * qy / 2));
if (isBlack(qx, qy) == c) ans += rx * ry, debug(rx, ry);
/*
int res = 0;
for (int i = 0; i < x; i++)
for (int j = 0; j < y; j++)
if (((i / k) + (j / k)) % 2 == c)
++res;
debug(x, y, k, c, ans, res);
return res;
*/
return ans;
}
ll calc(int L, vector<tuple<int,int,int,int>> &v, int k) {
ll res = INF;
for (int c: {0, 1}) {
// cerr << "============\n";
ll ans = calc2d(L-1, L-1, k, c);
for (auto [lx, ly, rx, ry]: v) {
debug(lx, ly, rx, ry);
ans += (rx - lx) * (ry - ly);
ans -= 2 * (calc2d(rx, ry, k, c) - calc2d(rx, ly, k, c) - calc2d(lx, ry, k, c) + calc2d(lx, ly, k, c));
}
debug(ans, k, c);
res = min(ans, res);
}
return res;
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int L, n;
cin >> L >> n;
vector<tuple<int,int,int,int>> rect(n);
for (auto &[a, b, c, d]: rect) cin >> a >> b >> c >> d, --a, --b, --a, --b, --c, --d;
ll ans = INF;
for (int i = 1; i*i <= L; i++) {
if (L % i == 0) {
if (i != L) ans = min(ans, calc(L, rect, i));
if (L/i != L) ans = min(ans, calc(L, rect, L / i));
}
if (i*i > L) break;
}
cout << ans << '\n';
}
/*
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
*/
Compilation message
chessboard.cpp: In function 'll calc(int, std::vector<std::tuple<int, int, int, int> >&, int)':
chessboard.cpp:51:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
51 | for (auto [lx, ly, rx, ry]: v) {
| ^
chessboard.cpp: In function 'int main()':
chessboard.cpp:66:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
66 | for (auto &[a, b, c, d]: rect) cin >> a >> b >> c >> d, --a, --b, --a, --b, --c, --d;
| ^
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
1260 KB |
Output is correct |
2 |
Correct |
9 ms |
620 KB |
Output is correct |
3 |
Correct |
20 ms |
1004 KB |
Output is correct |
4 |
Correct |
22 ms |
1004 KB |
Output is correct |
5 |
Correct |
27 ms |
1132 KB |
Output is correct |
6 |
Correct |
18 ms |
876 KB |
Output is correct |
7 |
Correct |
4 ms |
492 KB |
Output is correct |
8 |
Correct |
18 ms |
876 KB |
Output is correct |
9 |
Correct |
45 ms |
1772 KB |
Output is correct |
10 |
Correct |
25 ms |
1132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
2 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 |
1 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 |
2 ms |
364 KB |
Output is correct |
15 |
Correct |
2 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
2 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 |
1 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 |
2 ms |
364 KB |
Output is correct |
15 |
Correct |
2 ms |
364 KB |
Output is correct |
16 |
Correct |
19 ms |
748 KB |
Output is correct |
17 |
Correct |
37 ms |
1664 KB |
Output is correct |
18 |
Correct |
67 ms |
2028 KB |
Output is correct |
19 |
Correct |
321 ms |
1772 KB |
Output is correct |
20 |
Correct |
340 ms |
1900 KB |
Output is correct |
21 |
Correct |
35 ms |
1644 KB |
Output is correct |
22 |
Correct |
2 ms |
364 KB |
Output is correct |
23 |
Correct |
50 ms |
1004 KB |
Output is correct |
24 |
Correct |
62 ms |
1772 KB |
Output is correct |
25 |
Correct |
10 ms |
492 KB |
Output is correct |
26 |
Correct |
38 ms |
1260 KB |
Output is correct |
27 |
Correct |
65 ms |
1388 KB |
Output is correct |
28 |
Correct |
65 ms |
1772 KB |
Output is correct |
29 |
Correct |
15 ms |
876 KB |
Output is correct |
30 |
Correct |
2 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
1260 KB |
Output is correct |
2 |
Correct |
9 ms |
620 KB |
Output is correct |
3 |
Correct |
20 ms |
1004 KB |
Output is correct |
4 |
Correct |
22 ms |
1004 KB |
Output is correct |
5 |
Correct |
27 ms |
1132 KB |
Output is correct |
6 |
Correct |
18 ms |
876 KB |
Output is correct |
7 |
Correct |
4 ms |
492 KB |
Output is correct |
8 |
Correct |
18 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 |
2 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 |
1 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 |
2 ms |
364 KB |
Output is correct |
25 |
Correct |
2 ms |
364 KB |
Output is correct |
26 |
Correct |
19 ms |
748 KB |
Output is correct |
27 |
Correct |
37 ms |
1664 KB |
Output is correct |
28 |
Correct |
67 ms |
2028 KB |
Output is correct |
29 |
Correct |
321 ms |
1772 KB |
Output is correct |
30 |
Correct |
340 ms |
1900 KB |
Output is correct |
31 |
Correct |
35 ms |
1644 KB |
Output is correct |
32 |
Correct |
2 ms |
364 KB |
Output is correct |
33 |
Correct |
50 ms |
1004 KB |
Output is correct |
34 |
Correct |
62 ms |
1772 KB |
Output is correct |
35 |
Correct |
10 ms |
492 KB |
Output is correct |
36 |
Correct |
38 ms |
1260 KB |
Output is correct |
37 |
Correct |
65 ms |
1388 KB |
Output is correct |
38 |
Correct |
65 ms |
1772 KB |
Output is correct |
39 |
Correct |
15 ms |
876 KB |
Output is correct |
40 |
Correct |
2 ms |
364 KB |
Output is correct |
41 |
Incorrect |
268 ms |
1644 KB |
Output isn't correct |
42 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 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 |
32 ms |
1260 KB |
Output is correct |
10 |
Correct |
9 ms |
620 KB |
Output is correct |
11 |
Correct |
20 ms |
1004 KB |
Output is correct |
12 |
Correct |
22 ms |
1004 KB |
Output is correct |
13 |
Correct |
27 ms |
1132 KB |
Output is correct |
14 |
Correct |
18 ms |
876 KB |
Output is correct |
15 |
Correct |
4 ms |
492 KB |
Output is correct |
16 |
Correct |
18 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 |
2 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 |
1 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 |
2 ms |
364 KB |
Output is correct |
33 |
Correct |
2 ms |
364 KB |
Output is correct |
34 |
Correct |
19 ms |
748 KB |
Output is correct |
35 |
Correct |
37 ms |
1664 KB |
Output is correct |
36 |
Correct |
67 ms |
2028 KB |
Output is correct |
37 |
Correct |
321 ms |
1772 KB |
Output is correct |
38 |
Correct |
340 ms |
1900 KB |
Output is correct |
39 |
Correct |
35 ms |
1644 KB |
Output is correct |
40 |
Correct |
2 ms |
364 KB |
Output is correct |
41 |
Correct |
50 ms |
1004 KB |
Output is correct |
42 |
Correct |
62 ms |
1772 KB |
Output is correct |
43 |
Correct |
10 ms |
492 KB |
Output is correct |
44 |
Correct |
38 ms |
1260 KB |
Output is correct |
45 |
Correct |
65 ms |
1388 KB |
Output is correct |
46 |
Correct |
65 ms |
1772 KB |
Output is correct |
47 |
Correct |
15 ms |
876 KB |
Output is correct |
48 |
Correct |
2 ms |
364 KB |
Output is correct |
49 |
Incorrect |
268 ms |
1644 KB |
Output isn't correct |
50 |
Halted |
0 ms |
0 KB |
- |