Submission #90351

# Submission time Handle Problem Language Result Execution time Memory
90351 2018-12-21T10:07:38 Z HardNut Chessboard (IZhO18_chessboard) C++17
70 / 100
1105 ms 3708 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 5;
const long long INF = 1e18 + 5;
const int MAXN = 1e6;
const int base = 317;

typedef long long ll;

const ll bs = 1e5 + 1;
const ll mod = 998244353;

#define int ll

ll ans = INF, n, k, xx[N], yy[N], xx1[N], yy1[N];

int fndl(int cor, int x) {
    if (cor % x == 0)
        return cor;
    return (cor / x + 1) * x;
}

int fndr(int cor, int x) {
    if (cor % x == x - 1)
        return cor;
    return (cor / x) * x - 1;
}

void clc(int x, int y, int xx, int yy, ll &res1, ll &res2, int len) {
    if (yy < y || xx < x)
        return;
    int rs1 = 0, rs2 = 0;
    int n1 = xx - x + 1, n2 = yy - y + 1;
    rs1 = (n1 / len) * (n2 / len) / 2 * len * len;
    rs2 = n1 * n2 - rs1;
    swap(rs1, rs2);
    if ((x / len) % 2 != (y / len) % 2) {
        swap(rs1, rs2);
    }
    res1 += rs1;
    res2 += rs2;
}

void clcx(int x, int y, int xx, int yy, ll &res1, ll &res2, int len) {
    if (yy < y || xx < x)
        return;
    int nn = xx - x + 1;
    int ne = yy - y + 1;
    ll rs1 = (nn / len) / 2 * len * ne,
    rs2 = nn * ne - rs1;
    swap(rs1, rs2);
    if ((x / len) % 2 != (y / len) % 2)
        swap(rs1, rs2);
    res1 += rs1;
    res2 += rs2;
}

void clcy(int x, int y, int xx, int yy, ll &res1, ll &res2, int len) {
    if (yy < y || xx < x)
        return;
    int nn = yy - y + 1;
    int ne = xx - x + 1;
    ll rs1 = (nn / len) / 2 * len * ne,
    rs2 = nn * ne - rs1;
    swap(rs1, rs2);
    if ((x / len) % 2 != (y / len) % 2)
        swap(rs1, rs2);
    res1 += rs1;
    res2 += rs2;
}

void clc_cor(int x, int y, int xx, int yy, ll &res1, ll &res2, int len) {
    if (yy < y || xx < x)
        return;
    if ((x / len) % 2 == (y / len) % 2) {
        res1 += (xx - x + 1) * (yy - y + 1);
    } else {
        res2 += (xx - x + 1) * (yy - y + 1);
    }
}

ll calc(ll x) {
    ll res1 = 0, res2 = 0;
    for (int i = 1; i <= k; i++) {
        int x1 = fndl(xx[i], x),
        y1 = fndl(yy[i], x),
        x2 = fndr(xx1[i], x),
        y2 = fndr(yy1[i], x);
        if (x1 > x2 && y1 > y2) {
            bool us = 0;
            if (xx[i] <= x2 && yy[i] <= y2)
                clc_cor(xx[i], yy[i], x2, y2, res1, res2, x), us = 1;
            if (xx[i] <= x2 && y1 <= yy1[i])
                clc_cor(xx[i], y1, x2, yy1[i], res1, res2, x), us = 1;
            if (x1 <= xx1[i] && yy1[i] <= y2)
                clc_cor(x1, yy[i], xx1[i], y2, res1, res2, x), us = 1;
            if (x1 <= xx1[i] && y1 <= yy1[i])
                clc_cor(x1, y1, xx1[i], yy1[i], res1, res2, x), us = 1;
            if (!us) {
                clc_cor(xx[i], yy[i], xx1[i], yy1[i], res1, res2, x);
            }
            continue;
        }
        clc(x1, y1, x2, y2, res1, res2, x);
        clcx(x1, yy[i], x2, y1 - 1, res1, res2, x);
        clcx(x1, y2 + 1, x2, yy1[i], res1, res2, x);
        clcy(xx[i], y1, x1 - 1, y2, res1, res2, x);
        clcy(x2 + 1, y1, xx1[i], y2, res1, res2, x);
        if (x1 > xx[i] && y1 > yy[i])
            clc_cor(xx[i], yy[i], x1 - 1, y1 - 1, res1, res2, x);
        if (x2 < xx1[i] && y1 > yy[i])
            clc_cor(x2 + 1, yy[i], xx1[i], y1 - 1, res1, res2, x);
        if (xx[i] < x1 && yy1[i] > y2)
            clc_cor(xx[i], y2 + 1, x1 - 1, yy1[i], res1, res2, x);
        if (x2 < xx1[i] && yy1[i] > y2)
            clc_cor(x2 + 1, y2 + 1, xx1[i], yy1[i], res1, res2, x);
    }
    ll c1 = (n / x) * (n / x) / 2 * x * x;
    ll c2 = n * n - c1;
    return min(c1 - res2 + res1, c2 - res1 + res2);
}

main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> k;
    for (int i = 1; i <= k; i++) {
        cin >> xx[i] >> yy[i] >> xx1[i] >> yy1[i];
        xx[i]--;
        yy[i]--;
        xx1[i]--;
        yy1[i]--;
    }
    for (int i = 1; i <= sqrt(n); i++) {
        if (n % i == 0) {
            ans = min(ans, calc(i));
            if (i != 1)
                ans = min(ans, calc(n / i));
        }
    }
    cout << ans;
    return 0;
}
/*

*/

Compilation message

chessboard.cpp:125:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main() {
      ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 2 ms 476 KB Output is correct
4 Correct 2 ms 476 KB Output is correct
5 Correct 2 ms 492 KB Output is correct
6 Correct 2 ms 536 KB Output is correct
7 Correct 1 ms 544 KB Output is correct
8 Correct 1 ms 544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 2464 KB Output is correct
2 Correct 8 ms 2464 KB Output is correct
3 Correct 18 ms 2464 KB Output is correct
4 Correct 20 ms 2464 KB Output is correct
5 Correct 25 ms 2464 KB Output is correct
6 Correct 17 ms 2464 KB Output is correct
7 Correct 5 ms 2464 KB Output is correct
8 Correct 18 ms 2464 KB Output is correct
9 Correct 41 ms 3504 KB Output is correct
10 Correct 24 ms 3504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3504 KB Output is correct
2 Correct 2 ms 3504 KB Output is correct
3 Correct 2 ms 3504 KB Output is correct
4 Correct 3 ms 3504 KB Output is correct
5 Correct 2 ms 3504 KB Output is correct
6 Correct 2 ms 3504 KB Output is correct
7 Correct 2 ms 3504 KB Output is correct
8 Correct 2 ms 3504 KB Output is correct
9 Correct 2 ms 3504 KB Output is correct
10 Correct 2 ms 3504 KB Output is correct
11 Correct 2 ms 3504 KB Output is correct
12 Correct 2 ms 3504 KB Output is correct
13 Correct 3 ms 3504 KB Output is correct
14 Correct 2 ms 3504 KB Output is correct
15 Correct 2 ms 3504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3504 KB Output is correct
2 Correct 2 ms 3504 KB Output is correct
3 Correct 2 ms 3504 KB Output is correct
4 Correct 3 ms 3504 KB Output is correct
5 Correct 2 ms 3504 KB Output is correct
6 Correct 2 ms 3504 KB Output is correct
7 Correct 2 ms 3504 KB Output is correct
8 Correct 2 ms 3504 KB Output is correct
9 Correct 2 ms 3504 KB Output is correct
10 Correct 2 ms 3504 KB Output is correct
11 Correct 2 ms 3504 KB Output is correct
12 Correct 2 ms 3504 KB Output is correct
13 Correct 3 ms 3504 KB Output is correct
14 Correct 2 ms 3504 KB Output is correct
15 Correct 2 ms 3504 KB Output is correct
16 Correct 16 ms 3504 KB Output is correct
17 Correct 35 ms 3504 KB Output is correct
18 Correct 57 ms 3708 KB Output is correct
19 Correct 217 ms 3708 KB Output is correct
20 Correct 236 ms 3708 KB Output is correct
21 Correct 32 ms 3708 KB Output is correct
22 Correct 2 ms 3708 KB Output is correct
23 Correct 38 ms 3708 KB Output is correct
24 Correct 50 ms 3708 KB Output is correct
25 Correct 9 ms 3708 KB Output is correct
26 Correct 32 ms 3708 KB Output is correct
27 Correct 49 ms 3708 KB Output is correct
28 Correct 52 ms 3708 KB Output is correct
29 Correct 14 ms 3708 KB Output is correct
30 Correct 3 ms 3708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 2464 KB Output is correct
2 Correct 8 ms 2464 KB Output is correct
3 Correct 18 ms 2464 KB Output is correct
4 Correct 20 ms 2464 KB Output is correct
5 Correct 25 ms 2464 KB Output is correct
6 Correct 17 ms 2464 KB Output is correct
7 Correct 5 ms 2464 KB Output is correct
8 Correct 18 ms 2464 KB Output is correct
9 Correct 41 ms 3504 KB Output is correct
10 Correct 24 ms 3504 KB Output is correct
11 Correct 2 ms 3504 KB Output is correct
12 Correct 2 ms 3504 KB Output is correct
13 Correct 2 ms 3504 KB Output is correct
14 Correct 3 ms 3504 KB Output is correct
15 Correct 2 ms 3504 KB Output is correct
16 Correct 2 ms 3504 KB Output is correct
17 Correct 2 ms 3504 KB Output is correct
18 Correct 2 ms 3504 KB Output is correct
19 Correct 2 ms 3504 KB Output is correct
20 Correct 2 ms 3504 KB Output is correct
21 Correct 2 ms 3504 KB Output is correct
22 Correct 2 ms 3504 KB Output is correct
23 Correct 3 ms 3504 KB Output is correct
24 Correct 2 ms 3504 KB Output is correct
25 Correct 2 ms 3504 KB Output is correct
26 Correct 16 ms 3504 KB Output is correct
27 Correct 35 ms 3504 KB Output is correct
28 Correct 57 ms 3708 KB Output is correct
29 Correct 217 ms 3708 KB Output is correct
30 Correct 236 ms 3708 KB Output is correct
31 Correct 32 ms 3708 KB Output is correct
32 Correct 2 ms 3708 KB Output is correct
33 Correct 38 ms 3708 KB Output is correct
34 Correct 50 ms 3708 KB Output is correct
35 Correct 9 ms 3708 KB Output is correct
36 Correct 32 ms 3708 KB Output is correct
37 Correct 49 ms 3708 KB Output is correct
38 Correct 52 ms 3708 KB Output is correct
39 Correct 14 ms 3708 KB Output is correct
40 Correct 3 ms 3708 KB Output is correct
41 Correct 177 ms 3708 KB Output is correct
42 Correct 57 ms 3708 KB Output is correct
43 Correct 102 ms 3708 KB Output is correct
44 Correct 58 ms 3708 KB Output is correct
45 Correct 45 ms 3708 KB Output is correct
46 Correct 203 ms 3708 KB Output is correct
47 Correct 39 ms 3708 KB Output is correct
48 Correct 79 ms 3708 KB Output is correct
49 Correct 50 ms 3708 KB Output is correct
50 Correct 895 ms 3708 KB Output is correct
51 Correct 962 ms 3708 KB Output is correct
52 Correct 902 ms 3708 KB Output is correct
53 Correct 941 ms 3708 KB Output is correct
54 Correct 870 ms 3708 KB Output is correct
55 Correct 1000 ms 3708 KB Output is correct
56 Correct 884 ms 3708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 2 ms 476 KB Output is correct
4 Correct 2 ms 476 KB Output is correct
5 Correct 2 ms 492 KB Output is correct
6 Correct 2 ms 536 KB Output is correct
7 Correct 1 ms 544 KB Output is correct
8 Correct 1 ms 544 KB Output is correct
9 Correct 29 ms 2464 KB Output is correct
10 Correct 8 ms 2464 KB Output is correct
11 Correct 18 ms 2464 KB Output is correct
12 Correct 20 ms 2464 KB Output is correct
13 Correct 25 ms 2464 KB Output is correct
14 Correct 17 ms 2464 KB Output is correct
15 Correct 5 ms 2464 KB Output is correct
16 Correct 18 ms 2464 KB Output is correct
17 Correct 41 ms 3504 KB Output is correct
18 Correct 24 ms 3504 KB Output is correct
19 Correct 2 ms 3504 KB Output is correct
20 Correct 2 ms 3504 KB Output is correct
21 Correct 2 ms 3504 KB Output is correct
22 Correct 3 ms 3504 KB Output is correct
23 Correct 2 ms 3504 KB Output is correct
24 Correct 2 ms 3504 KB Output is correct
25 Correct 2 ms 3504 KB Output is correct
26 Correct 2 ms 3504 KB Output is correct
27 Correct 2 ms 3504 KB Output is correct
28 Correct 2 ms 3504 KB Output is correct
29 Correct 2 ms 3504 KB Output is correct
30 Correct 2 ms 3504 KB Output is correct
31 Correct 3 ms 3504 KB Output is correct
32 Correct 2 ms 3504 KB Output is correct
33 Correct 2 ms 3504 KB Output is correct
34 Correct 16 ms 3504 KB Output is correct
35 Correct 35 ms 3504 KB Output is correct
36 Correct 57 ms 3708 KB Output is correct
37 Correct 217 ms 3708 KB Output is correct
38 Correct 236 ms 3708 KB Output is correct
39 Correct 32 ms 3708 KB Output is correct
40 Correct 2 ms 3708 KB Output is correct
41 Correct 38 ms 3708 KB Output is correct
42 Correct 50 ms 3708 KB Output is correct
43 Correct 9 ms 3708 KB Output is correct
44 Correct 32 ms 3708 KB Output is correct
45 Correct 49 ms 3708 KB Output is correct
46 Correct 52 ms 3708 KB Output is correct
47 Correct 14 ms 3708 KB Output is correct
48 Correct 3 ms 3708 KB Output is correct
49 Correct 177 ms 3708 KB Output is correct
50 Correct 57 ms 3708 KB Output is correct
51 Correct 102 ms 3708 KB Output is correct
52 Correct 58 ms 3708 KB Output is correct
53 Correct 45 ms 3708 KB Output is correct
54 Correct 203 ms 3708 KB Output is correct
55 Correct 39 ms 3708 KB Output is correct
56 Correct 79 ms 3708 KB Output is correct
57 Correct 50 ms 3708 KB Output is correct
58 Correct 895 ms 3708 KB Output is correct
59 Correct 962 ms 3708 KB Output is correct
60 Correct 902 ms 3708 KB Output is correct
61 Correct 941 ms 3708 KB Output is correct
62 Correct 870 ms 3708 KB Output is correct
63 Correct 1000 ms 3708 KB Output is correct
64 Correct 884 ms 3708 KB Output is correct
65 Correct 2 ms 3708 KB Output is correct
66 Correct 2 ms 3708 KB Output is correct
67 Incorrect 1105 ms 3708 KB Output isn't correct
68 Halted 0 ms 0 KB -