답안 #852396

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
852396 2023-09-21T18:16:24 Z vjudge1 Chessboard (IZhO18_chessboard) C++17
100 / 100
367 ms 4412 KB
#include <bits/stdc++.h>
using namespace std;

int32_t main() {
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        int n, k;
        cin >> n >> k;
        vector<int> a(k), b(k), aa(k), bb(k);
        for (int i = 0; i < k; i++) {
                cin >> a[i] >> b[i] >> aa[i] >> bb[i];
                a[i]--, b[i]--, aa[i]--, bb[i]--;
        }

        int64_t res = 1ll * n * n;

        for (int i = 1; i < n; i++) {
                if (n % i) continue;

                vector<int64_t> cntb(2, 0);
                vector<int64_t> need(2, 0);

                need[0] = 1ll * (n / i) * (n / i);
                need[1] = need[0] / 2;
                need[0] -= need[1];
                need[0] *= 1ll * i * i;
                need[1] *= 1ll * i * i;

                auto add_same = [&](int id, int u, int v, int length) {
                        if (u > v) return;
                        int uu = u / i, vv = v / i;
                        if (uu == vv) {
                                cntb[(id ^ uu) & 1] += 1ll * (v - u + 1) * length;
                                return;
                        }
                        assert(uu < vv);
                        int x = i - u % i;
                        cntb[(uu ^ id) & 1] += 1ll * length * x;
                        int y = v % i + 1;
                        cntb[(vv ^ id) & 1] += 1ll * length * y;
                        uu++, vv--;
                        if (uu <= vv) {
                                int64_t addu = 1ll * length * ((vv - uu + 1) / 2 + (((uu ^ vv) & 1) == 0)) * i;
                                cntb[(uu ^ id) & 1] += addu;
                                cntb[(uu ^ id) & 1 ^ 1] += 1ll * (vv - uu + 1) * length * i - addu;
                        }
                };

                auto add = [&](int x, int y, int u, int v) {
                        int up = x / i;
                        int dn = u / i;
                        int lt = y / i;
                        int rt = v / i;
                        if (up == dn) {
                                if (lt == rt) {
                                        cntb[(up ^ lt) & 1] += 1ll * (u - x + 1) * (v - y + 1);
                                } else {
                                        add_same(up, y, v, u - x + 1);
                                }
                        } else {
                                if (lt == rt) {
                                        add_same(lt, x, u, v - y + 1);
                                } else {
                                        add_same(up, y, v, i - x % i);
                                        add_same(dn, y, v, u % i + 1);
                                        add_same(lt, x + (i - x % i), u - (u % i + 1), i - y % i);
                                        add_same(rt, x + (i - x % i), u - (u % i + 1), v % i + 1);
                                        up++, dn--, lt++, rt--;
                                        int64_t p = 1ll * (dn - up + 1) * (rt - lt + 1);
                                        int64_t q = p / 2;
                                        p -= q;
                                        cntb[(up ^ lt) & 1] += 1ll * p * i * i;
                                        cntb[(up ^ lt) & 1 ^ 1] += 1ll * q * i * i;
                                }
                        }
                };

                for (int j = 0; j < k; j++) add(a[j], b[j], aa[j], bb[j]);
                /// (0, 0) white
                res = min(res, cntb[0] + need[1] - cntb[1]);
                res = min(res, need[0] - cntb[0] + cntb[1]);
        }

        cout << res;
}

Compilation message

chessboard.cpp: In lambda function:
chessboard.cpp:45:48: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
   45 |                                 cntb[(uu ^ id) & 1 ^ 1] += 1ll * (vv - uu + 1) * length * i - addu;
      |                                      ~~~~~~~~~~^~~
chessboard.cpp: In lambda function:
chessboard.cpp:73:56: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
   73 |                                         cntb[(up ^ lt) & 1 ^ 1] += 1ll * q * i * i;
      |                                              ~~~~~~~~~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 496 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 2348 KB Output is correct
2 Correct 5 ms 856 KB Output is correct
3 Correct 11 ms 1836 KB Output is correct
4 Correct 12 ms 1880 KB Output is correct
5 Correct 20 ms 2344 KB Output is correct
6 Correct 9 ms 1624 KB Output is correct
7 Correct 3 ms 856 KB Output is correct
8 Correct 10 ms 1624 KB Output is correct
9 Correct 24 ms 3164 KB Output is correct
10 Correct 13 ms 1880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 352 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 544 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 352 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 544 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 7 ms 1112 KB Output is correct
17 Correct 18 ms 2648 KB Output is correct
18 Correct 25 ms 2904 KB Output is correct
19 Correct 63 ms 2648 KB Output is correct
20 Correct 71 ms 3024 KB Output is correct
21 Correct 18 ms 2648 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 14 ms 1628 KB Output is correct
24 Correct 23 ms 2880 KB Output is correct
25 Correct 4 ms 600 KB Output is correct
26 Correct 15 ms 2136 KB Output is correct
27 Correct 20 ms 2140 KB Output is correct
28 Correct 27 ms 2904 KB Output is correct
29 Correct 8 ms 1360 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 2348 KB Output is correct
2 Correct 5 ms 856 KB Output is correct
3 Correct 11 ms 1836 KB Output is correct
4 Correct 12 ms 1880 KB Output is correct
5 Correct 20 ms 2344 KB Output is correct
6 Correct 9 ms 1624 KB Output is correct
7 Correct 3 ms 856 KB Output is correct
8 Correct 10 ms 1624 KB Output is correct
9 Correct 24 ms 3164 KB Output is correct
10 Correct 13 ms 1880 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 352 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 544 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 7 ms 1112 KB Output is correct
27 Correct 18 ms 2648 KB Output is correct
28 Correct 25 ms 2904 KB Output is correct
29 Correct 63 ms 2648 KB Output is correct
30 Correct 71 ms 3024 KB Output is correct
31 Correct 18 ms 2648 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 14 ms 1628 KB Output is correct
34 Correct 23 ms 2880 KB Output is correct
35 Correct 4 ms 600 KB Output is correct
36 Correct 15 ms 2136 KB Output is correct
37 Correct 20 ms 2140 KB Output is correct
38 Correct 27 ms 2904 KB Output is correct
39 Correct 8 ms 1360 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 60 ms 3024 KB Output is correct
42 Correct 29 ms 3160 KB Output is correct
43 Correct 40 ms 3320 KB Output is correct
44 Correct 28 ms 3160 KB Output is correct
45 Correct 25 ms 3408 KB Output is correct
46 Correct 67 ms 3160 KB Output is correct
47 Correct 22 ms 2904 KB Output is correct
48 Correct 34 ms 3088 KB Output is correct
49 Correct 26 ms 2904 KB Output is correct
50 Correct 266 ms 3416 KB Output is correct
51 Correct 280 ms 3420 KB Output is correct
52 Correct 261 ms 3144 KB Output is correct
53 Correct 287 ms 3236 KB Output is correct
54 Correct 257 ms 3112 KB Output is correct
55 Correct 291 ms 3416 KB Output is correct
56 Correct 250 ms 2904 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 496 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 17 ms 2348 KB Output is correct
10 Correct 5 ms 856 KB Output is correct
11 Correct 11 ms 1836 KB Output is correct
12 Correct 12 ms 1880 KB Output is correct
13 Correct 20 ms 2344 KB Output is correct
14 Correct 9 ms 1624 KB Output is correct
15 Correct 3 ms 856 KB Output is correct
16 Correct 10 ms 1624 KB Output is correct
17 Correct 24 ms 3164 KB Output is correct
18 Correct 13 ms 1880 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 0 ms 344 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 352 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 344 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 1 ms 544 KB Output is correct
30 Correct 1 ms 344 KB Output is correct
31 Correct 1 ms 344 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 7 ms 1112 KB Output is correct
35 Correct 18 ms 2648 KB Output is correct
36 Correct 25 ms 2904 KB Output is correct
37 Correct 63 ms 2648 KB Output is correct
38 Correct 71 ms 3024 KB Output is correct
39 Correct 18 ms 2648 KB Output is correct
40 Correct 1 ms 344 KB Output is correct
41 Correct 14 ms 1628 KB Output is correct
42 Correct 23 ms 2880 KB Output is correct
43 Correct 4 ms 600 KB Output is correct
44 Correct 15 ms 2136 KB Output is correct
45 Correct 20 ms 2140 KB Output is correct
46 Correct 27 ms 2904 KB Output is correct
47 Correct 8 ms 1360 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 60 ms 3024 KB Output is correct
50 Correct 29 ms 3160 KB Output is correct
51 Correct 40 ms 3320 KB Output is correct
52 Correct 28 ms 3160 KB Output is correct
53 Correct 25 ms 3408 KB Output is correct
54 Correct 67 ms 3160 KB Output is correct
55 Correct 22 ms 2904 KB Output is correct
56 Correct 34 ms 3088 KB Output is correct
57 Correct 26 ms 2904 KB Output is correct
58 Correct 266 ms 3416 KB Output is correct
59 Correct 280 ms 3420 KB Output is correct
60 Correct 261 ms 3144 KB Output is correct
61 Correct 287 ms 3236 KB Output is correct
62 Correct 257 ms 3112 KB Output is correct
63 Correct 291 ms 3416 KB Output is correct
64 Correct 250 ms 2904 KB Output is correct
65 Correct 0 ms 344 KB Output is correct
66 Correct 0 ms 344 KB Output is correct
67 Correct 351 ms 3196 KB Output is correct
68 Correct 347 ms 4128 KB Output is correct
69 Correct 302 ms 3680 KB Output is correct
70 Correct 317 ms 4036 KB Output is correct
71 Correct 329 ms 3980 KB Output is correct
72 Correct 318 ms 3664 KB Output is correct
73 Correct 323 ms 4056 KB Output is correct
74 Correct 348 ms 4104 KB Output is correct
75 Correct 331 ms 3948 KB Output is correct
76 Correct 352 ms 4184 KB Output is correct
77 Correct 71 ms 4176 KB Output is correct
78 Correct 32 ms 3932 KB Output is correct
79 Correct 52 ms 3664 KB Output is correct
80 Correct 56 ms 3996 KB Output is correct
81 Correct 52 ms 3668 KB Output is correct
82 Correct 46 ms 3920 KB Output is correct
83 Correct 42 ms 3888 KB Output is correct
84 Correct 221 ms 4280 KB Output is correct
85 Correct 365 ms 4328 KB Output is correct
86 Correct 2 ms 344 KB Output is correct
87 Correct 1 ms 344 KB Output is correct
88 Correct 367 ms 4412 KB Output is correct
89 Correct 77 ms 1176 KB Output is correct
90 Correct 1 ms 344 KB Output is correct