Submission #91746

# Submission time Handle Problem Language Result Execution time Memory
91746 2018-12-29T19:16:48 Z popovicirobert Chessboard (IZhO18_chessboard) C++14
100 / 100
406 ms 4380 KB
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
#define ld long double
// 217
// 44

using namespace std;

const int MAXN = (int) 1e5;

struct Data {
    int x1, y1, x2, y2;
}arr[MAXN + 1];

inline void add(vector <ll> &fr, ll a, ll b) {
    fr[0] += a;
    fr[1] += b;
}

inline int get(int x, int len) {
    if(x - len + 1 < 0) {
        return -1;
    }
    return ((x - len + 1) / len) * len + len - 1;
}

int main() {
    //ifstream cin("A.in");
    //ofstream cout("A.out");
    int i, n, k;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin >> n >> k;
    for(i = 1; i <= k; i++) {
        cin >> arr[i].x1 >> arr[i].y1 >> arr[i].x2 >> arr[i].y2;
        arr[i].x1--, arr[i].y1--;
        arr[i].x2--, arr[i].y2--;
    }
    ll ans = 1LL * n * n;
    for(int len = 1; len < n; len++) {
        if(n % len == 0) {
            vector <ll> fr(2);
            for(i = 1; i <= k; i++) {
                if(arr[i].x2 / len == arr[i].x1 / len && arr[i].y2 / len == arr[i].y1 / len) {
                    if((arr[i].x1 / len + arr[i].y1 / len) % 2 == 0) {
                        fr[0] += 1LL * (arr[i].x2 - arr[i].x1 + 1) * (arr[i].y2 - arr[i].y1 + 1);
                    }
                    else {
                        fr[1] += 1LL * (arr[i].x2 - arr[i].x1 + 1) * (arr[i].y2 - arr[i].y1 + 1);
                    }
                    continue;
                }
                int a = ((arr[i].x1 + len - 1) / len) * len - arr[i].x1;
                int A = ((arr[i].y1 + len - 1) / len) * len - arr[i].y1;
                int b = arr[i].x2 - get(arr[i].x2, len);
                int B = arr[i].y2 - get(arr[i].y2, len);
                if(arr[i].y2 / len == arr[i].y1 / len) {
                    if((arr[i].x1 / len + arr[i].y1 / len) % 2 == 0) {
                        add(fr, 1LL * a * (arr[i].y2 - arr[i].y1 + 1), 0);
                    }
                    else {
                        add(fr, 0, 1LL * a * (arr[i].y2 - arr[i].y1 + 1));
                    }
                    if(((arr[i].x1 + a) / len + arr[i].y1 / len) % 2 == 0) {
                        int cur = (arr[i].x2 - arr[i].x1 + 1 - a - b) / len;
                        add(fr, 1LL * len * ((cur + 1) / 2) * (arr[i].y2 - arr[i].y1 + 1), 1LL * len * (cur / 2) * (arr[i].y2 - arr[i].y1 + 1));
                    }
                    else {
                        int cur = (arr[i].x2 - arr[i].x1 + 1 - a - b) / len;
                        add(fr, 1LL * len * (cur / 2) * (arr[i].y2 - arr[i].y1 + 1), 1LL * len * ((cur + 1) / 2) * (arr[i].y2 - arr[i].y1 + 1));
                    }
                    if((arr[i].x2 / len + arr[i].y1 / len) % 2 == 0) {
                        add(fr, 1LL * b * (arr[i].y2 - arr[i].y1 + 1), 0);
                    }
                    else {
                        add(fr, 0, 1LL * b * (arr[i].y2 - arr[i].y1 + 1));
                    }
                    continue;
                }
                if(arr[i].x2 / len == arr[i].x1 / len) {
                    if((arr[i].x1 / len + arr[i].y1 / len) % 2 == 0) {
                        add(fr, 1LL * A * (arr[i].x2 - arr[i].x1 + 1), 0);
                    }
                    else {
                        add(fr, 0, 1LL * A * (arr[i].x2 - arr[i].x1 + 1));
                    }
                    if(((arr[i].y1 + A) / len + arr[i].x1 / len) % 2 == 0) {
                        int cur = (arr[i].y2 - arr[i].y1 + 1 - A - B) / len;
                        add(fr, 1LL * len * ((cur + 1) / 2) * (arr[i].x2 - arr[i].x1 + 1), 1LL * len * (cur / 2) * (arr[i].x2 - arr[i].x1 + 1));
                    }
                    else {
                        int cur = (arr[i].y2 - arr[i].y1 + 1 - A - B) / len;
                        add(fr, 1LL * len * (cur / 2) * (arr[i].x2 - arr[i].x1 + 1), 1LL * len * ((cur + 1) / 2) * (arr[i].x2 - arr[i].x1 + 1));
                    }
                    if((arr[i].x1 / len + arr[i].y2 / len) % 2 == 0) {
                        add(fr, 1LL * B * (arr[i].x2 - arr[i].x1 + 1), 0);
                    }
                    else {
                        add(fr, 0, 1LL * B * (arr[i].x2 - arr[i].x1 + 1));
                    }
                    continue;
                }
                /*if(len == 2) {
                    cerr << a << " " << A << " " << b << " " << B << "\n";
                }*/

                if((arr[i].x1 / len + arr[i].y1 / len) % 2 == 0) {
                    add(fr, 1LL * a * A, 0);
                }
                else {
                    add(fr, 0, 1LL * a * A);
                }
                int cur;
                if((arr[i].x1 / len + (arr[i].y1 + A) / len) % 2 == 1) {
                    cur = (arr[i].y2 - arr[i].y1 + 1 - A - B) / len;
                    add(fr, 1LL * len * (cur / 2) * a, 1LL * len * ((cur + 1) / 2) * a);
                }
                else {
                    cur = (arr[i].y2 - arr[i].y1 + 1 - A - B) / len;
                    add(fr, 1LL * len * ((cur + 1) / 2) * a, 1LL * len * (cur / 2) * a);
                }
                if(((arr[i].x1 + a) / len + arr[i].y1 / len) % 2 == 1) {
                    cur = (arr[i].x2 - arr[i].x1 + 1 - a - b) / len;
                    add(fr, 1LL * len * (cur / 2) * A, 1LL * len * ((cur + 1) / 2) * A);
                }
                else {
                    cur = (arr[i].x2 - arr[i].x1 + 1 - a - b) / len;
                    add(fr, 1LL * len * ((cur + 1) / 2) * A, 1LL * len * (cur / 2) * A);
                }

                //////////////////////////////////
                if((arr[i].x1 / len + arr[i].y2 / len) % 2 == 0) {
                    add(fr, 1LL * a * B, 0);
                }
                else {
                    add(fr, 0, 1LL * a * B);
                }
                if((arr[i].x2 / len + arr[i].y1 / len) % 2 == 0) {
                    add(fr, 1LL * b * A, 0);
                }
                else {
                    add(fr, 0, 1LL * b * A);
                }

                //////////////////////////////////////
                if((arr[i].x2 / len + arr[i].y2 / len) % 2 == 0) {
                    add(fr, 1LL * b * B, 0);
                }
                else {
                    add(fr, 0, 1LL * b * B);
                }
                if(((arr[i].x2 - b) / len + arr[i].y2 / len) % 2 == 1) {
                    cur = (arr[i].x2 - arr[i].x1 + 1 - a - b) / len;
                    add(fr, 1LL * len * (cur / 2) * B, 1LL * len * ((cur + 1) / 2) * B);
                }
                else {
                    cur = (arr[i].x2 - arr[i].x1 + 1 - a - b) / len;
                    add(fr, 1LL * len * ((cur + 1) / 2) * B, 1LL * len * (cur / 2) * B);
                }
                if((arr[i].x2 / len + (arr[i].y2 - B) / len) % 2 == 1) {
                    cur = (arr[i].y2 - arr[i].y1 + 1 - A - B) / len;
                    add(fr, 1LL * len * (cur / 2) * b, 1LL * len * ((cur + 1) / 2) * b);
                }
                else {
                    cur = (arr[i].y2 - arr[i].y1 + 1 - A - B) / len;
                    add(fr, 1LL * len * ((cur + 1) / 2) * b, 1LL * len * (cur / 2) * b);
                }
                ///////////////////////////////

                ll cnt0, cnt1;
                ll s = 1LL * (arr[i].x2 - arr[i].x1 + 1 - a - b) * (arr[i].y2 - arr[i].y1 + 1 - A - B);
                if((s / (1LL * len * len)) % 2 == 0) {
                    cnt0 = cnt1 = (s / (1LL * len * len)) / 2;
                }
                else {
                    cnt0 = cnt1 = (s / (1LL * len * len)) / 2;
                    if((arr[i].x1 / len + arr[i].y1 / len) % 2 == 0) {
                        cnt0++;
                    }
                    else {
                        cnt1++;
                    }
                }
                fr[0] += 1LL * len * len * cnt0;
                fr[1] += 1LL * len * len * cnt1;
            }
            ll cnt0, cnt1;
            if((n / len) % 2 == 0) {
                cnt0 = cnt1 = ((1LL * n * n) / (1LL * len * len)) / 2;
            }
            else {
                cnt0 = cnt1 = ((1LL * n * n) / (1LL * len * len)) / 2;
                cnt0++;
            }
            cnt0 *= 1LL * len * len;
            cnt1 *= 1LL * len * len;
            //cerr << len << " " << cnt0 << " " << cnt1 << " " << fr[0] << " " << fr[1] << "\n";
            ans = min(ans, min(cnt0 - fr[0] + fr[1], cnt1 - fr[1] + fr[0]));
        }
    }
    cout << ans;
    //cin.close();
    //cout.close();
    return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 380 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 1400 KB Output is correct
2 Correct 7 ms 632 KB Output is correct
3 Correct 16 ms 1016 KB Output is correct
4 Correct 17 ms 1016 KB Output is correct
5 Correct 21 ms 1272 KB Output is correct
6 Correct 14 ms 888 KB Output is correct
7 Correct 4 ms 504 KB Output is correct
8 Correct 15 ms 888 KB Output is correct
9 Correct 35 ms 1756 KB Output is correct
10 Correct 26 ms 1116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 11 ms 760 KB Output is correct
17 Correct 26 ms 1656 KB Output is correct
18 Correct 34 ms 1912 KB Output is correct
19 Correct 72 ms 1656 KB Output is correct
20 Correct 82 ms 1932 KB Output is correct
21 Correct 25 ms 1656 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 19 ms 1020 KB Output is correct
24 Correct 31 ms 1756 KB Output is correct
25 Correct 5 ms 504 KB Output is correct
26 Correct 21 ms 1176 KB Output is correct
27 Correct 28 ms 1376 KB Output is correct
28 Correct 34 ms 1784 KB Output is correct
29 Correct 11 ms 860 KB Output is correct
30 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 1400 KB Output is correct
2 Correct 7 ms 632 KB Output is correct
3 Correct 16 ms 1016 KB Output is correct
4 Correct 17 ms 1016 KB Output is correct
5 Correct 21 ms 1272 KB Output is correct
6 Correct 14 ms 888 KB Output is correct
7 Correct 4 ms 504 KB Output is correct
8 Correct 15 ms 888 KB Output is correct
9 Correct 35 ms 1756 KB Output is correct
10 Correct 26 ms 1116 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 11 ms 760 KB Output is correct
27 Correct 26 ms 1656 KB Output is correct
28 Correct 34 ms 1912 KB Output is correct
29 Correct 72 ms 1656 KB Output is correct
30 Correct 82 ms 1932 KB Output is correct
31 Correct 25 ms 1656 KB Output is correct
32 Correct 2 ms 376 KB Output is correct
33 Correct 19 ms 1020 KB Output is correct
34 Correct 31 ms 1756 KB Output is correct
35 Correct 5 ms 504 KB Output is correct
36 Correct 21 ms 1176 KB Output is correct
37 Correct 28 ms 1376 KB Output is correct
38 Correct 34 ms 1784 KB Output is correct
39 Correct 11 ms 860 KB Output is correct
40 Correct 2 ms 376 KB Output is correct
41 Correct 66 ms 1684 KB Output is correct
42 Correct 39 ms 1756 KB Output is correct
43 Correct 47 ms 1628 KB Output is correct
44 Correct 38 ms 1784 KB Output is correct
45 Correct 39 ms 1912 KB Output is correct
46 Correct 76 ms 1784 KB Output is correct
47 Correct 32 ms 1784 KB Output is correct
48 Correct 43 ms 1656 KB Output is correct
49 Correct 35 ms 1656 KB Output is correct
50 Correct 266 ms 1800 KB Output is correct
51 Correct 277 ms 1912 KB Output is correct
52 Correct 260 ms 1784 KB Output is correct
53 Correct 274 ms 1912 KB Output is correct
54 Correct 261 ms 1808 KB Output is correct
55 Correct 290 ms 1912 KB Output is correct
56 Correct 250 ms 1784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 380 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 24 ms 1400 KB Output is correct
10 Correct 7 ms 632 KB Output is correct
11 Correct 16 ms 1016 KB Output is correct
12 Correct 17 ms 1016 KB Output is correct
13 Correct 21 ms 1272 KB Output is correct
14 Correct 14 ms 888 KB Output is correct
15 Correct 4 ms 504 KB Output is correct
16 Correct 15 ms 888 KB Output is correct
17 Correct 35 ms 1756 KB Output is correct
18 Correct 26 ms 1116 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 376 KB Output is correct
28 Correct 2 ms 376 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 2 ms 376 KB Output is correct
31 Correct 2 ms 376 KB Output is correct
32 Correct 2 ms 376 KB Output is correct
33 Correct 2 ms 376 KB Output is correct
34 Correct 11 ms 760 KB Output is correct
35 Correct 26 ms 1656 KB Output is correct
36 Correct 34 ms 1912 KB Output is correct
37 Correct 72 ms 1656 KB Output is correct
38 Correct 82 ms 1932 KB Output is correct
39 Correct 25 ms 1656 KB Output is correct
40 Correct 2 ms 376 KB Output is correct
41 Correct 19 ms 1020 KB Output is correct
42 Correct 31 ms 1756 KB Output is correct
43 Correct 5 ms 504 KB Output is correct
44 Correct 21 ms 1176 KB Output is correct
45 Correct 28 ms 1376 KB Output is correct
46 Correct 34 ms 1784 KB Output is correct
47 Correct 11 ms 860 KB Output is correct
48 Correct 2 ms 376 KB Output is correct
49 Correct 66 ms 1684 KB Output is correct
50 Correct 39 ms 1756 KB Output is correct
51 Correct 47 ms 1628 KB Output is correct
52 Correct 38 ms 1784 KB Output is correct
53 Correct 39 ms 1912 KB Output is correct
54 Correct 76 ms 1784 KB Output is correct
55 Correct 32 ms 1784 KB Output is correct
56 Correct 43 ms 1656 KB Output is correct
57 Correct 35 ms 1656 KB Output is correct
58 Correct 266 ms 1800 KB Output is correct
59 Correct 277 ms 1912 KB Output is correct
60 Correct 260 ms 1784 KB Output is correct
61 Correct 274 ms 1912 KB Output is correct
62 Correct 261 ms 1808 KB Output is correct
63 Correct 290 ms 1912 KB Output is correct
64 Correct 250 ms 1784 KB Output is correct
65 Correct 2 ms 376 KB Output is correct
66 Correct 2 ms 376 KB Output is correct
67 Correct 381 ms 1784 KB Output is correct
68 Correct 381 ms 2040 KB Output is correct
69 Correct 320 ms 1784 KB Output is correct
70 Correct 336 ms 1784 KB Output is correct
71 Correct 346 ms 1912 KB Output is correct
72 Correct 342 ms 1912 KB Output is correct
73 Correct 350 ms 1912 KB Output is correct
74 Correct 374 ms 2056 KB Output is correct
75 Correct 347 ms 1912 KB Output is correct
76 Correct 372 ms 2040 KB Output is correct
77 Correct 85 ms 2100 KB Output is correct
78 Correct 43 ms 1912 KB Output is correct
79 Correct 64 ms 3832 KB Output is correct
80 Correct 65 ms 3832 KB Output is correct
81 Correct 62 ms 3576 KB Output is correct
82 Correct 57 ms 4088 KB Output is correct
83 Correct 52 ms 3856 KB Output is correct
84 Correct 237 ms 4216 KB Output is correct
85 Correct 403 ms 4256 KB Output is correct
86 Correct 3 ms 376 KB Output is correct
87 Correct 2 ms 376 KB Output is correct
88 Correct 406 ms 4380 KB Output is correct
89 Correct 83 ms 1144 KB Output is correct
90 Correct 2 ms 376 KB Output is correct