Submission #168584

# Submission time Handle Problem Language Result Execution time Memory
168584 2019-12-14T03:13:30 Z timothyhorsc Chessboard (IZhO18_chessboard) C++14
100 / 100
998 ms 9720 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define REP(i, l, r) for (int i=(l); i<(r); ++i)
#define RREP(i, r, l) for (int i=(r); i>(l); --i)
#define vi vector<int>
#define popcount __builtin_popcountll

const int INF = 1LL<<60;
const int MOD = 1000000007;

template<typename T> istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> t) {
    return "(" + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " + to_string(get<2>(t)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> t) {
    return "(" + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " + to_string(get<2>(t)) + ", " + to_string(get<3>(t)) + ")";
}

template <typename A>
string to_string(vector<A> v) {
    bool first = true;
    string result = "[";
    for (A i : v) {
        if (!first) result += ", ";
        first = false;
        result += to_string(i); 
    }
    return result + "]";
}

string bin_string(int x) {
    const int d = 64;
    string result(d, ' ');
    RREP(i, d-1, -1) result[d-1-i] = '0'+((x&1LL<<i)!=0);
    return result;
}

vector<int> getZ(string s) {
    int n = s.size();
    vector<int> z(n);
    int x = 0, y = 0;
    REP(i, 1, n) {
        z[i] = max(0LL, min(z[i-x], y-i+1));
        while (i+z[i] < n && s[z[i]] == s[i+z[i]]) {
            x = i;
            y = i+z[i];
            ++z[i];
        }
    }
    return z;
}

int powa(int base, int exp) {
    if (exp<=0) return 1;
    if (base<=0) return 0;
    int val = powa(base, exp>>1);
    val = val*val % MOD;
    if (exp&1) {
        val = val*base % MOD;
    }
    return val;
}

int n, k, res=INF;
vector<vi> blocks;

int bc(int d, int x1, int x2) {
    int firstb = x1/(2*d), lastb = x2/(2*d);
    int b = d*(lastb-firstb+1);
    firstb *= 2*d;
    lastb *= 2*d;
    b -= min(d, x1-firstb);
    b -= max(0LL, lastb+d-1-x2);
    return b;
}

void getcost(int d) {
    int bsq = ((n/d)*(n/d)+1)/2;
    int wsq = (n/d)*(n/d)/2;
    int mincost = (wsq-k) * d*d;
    if (mincost >= res) return;
    int cost = bsq * d*d;
    for (vi block : blocks) {
        int x1=block[0],y1=block[1],x2=block[2],y2=block[3];
        --x1; --x2; --y1; --y2;
        int h = bc(d, x1, x2);
        int v = bc(d, y1, y2);
        int b = h*v + (x2-x1+1-h)*(y2-y1+1-v);
        int w = (x2-x1+1)*(y2-y1+1)-b;
        cost -= b;
        cost += w;
    }
    res = min(res, cost);
    res = min(res, n*n-cost);
    // cout << d << ": " << cost << ' ' << n*n-cost << '\n';
}

main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    cin >> n >> k;
    blocks.assign(k, vi(4));
    cin >> blocks;

    vi factors;
    for (int d=1; d*d<=n; ++d) {
        if (n%d==0) {
            factors.push_back(d);
            if (d*d!=n && d != 1) factors.push_back(n/d);
        }
    }
    sort(factors.rbegin(), factors.rend());
    for (int d : factors) {
        getcost(d);
    }
    cout << res;
}

Compilation message

chessboard.cpp:109: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 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
# Verdict Execution time Memory Grader output
1 Correct 42 ms 5688 KB Output is correct
2 Correct 12 ms 1912 KB Output is correct
3 Correct 27 ms 4080 KB Output is correct
4 Correct 30 ms 4472 KB Output is correct
5 Correct 37 ms 5496 KB Output is correct
6 Correct 24 ms 3292 KB Output is correct
7 Correct 7 ms 1016 KB Output is correct
8 Correct 24 ms 3704 KB Output is correct
9 Correct 60 ms 8824 KB Output is correct
10 Correct 34 ms 5064 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 3 ms 376 KB Output is correct
5 Correct 11 ms 380 KB Output is correct
6 Correct 3 ms 376 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 3 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 3 ms 376 KB Output is correct
13 Correct 3 ms 376 KB Output is correct
14 Correct 3 ms 376 KB Output is correct
15 Correct 3 ms 380 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 3 ms 376 KB Output is correct
5 Correct 11 ms 380 KB Output is correct
6 Correct 3 ms 376 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 3 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 3 ms 376 KB Output is correct
13 Correct 3 ms 376 KB Output is correct
14 Correct 3 ms 376 KB Output is correct
15 Correct 3 ms 380 KB Output is correct
16 Correct 20 ms 2680 KB Output is correct
17 Correct 48 ms 7672 KB Output is correct
18 Correct 75 ms 8696 KB Output is correct
19 Correct 267 ms 7952 KB Output is correct
20 Correct 298 ms 8952 KB Output is correct
21 Correct 47 ms 7416 KB Output is correct
22 Correct 3 ms 376 KB Output is correct
23 Correct 49 ms 4088 KB Output is correct
24 Correct 69 ms 8184 KB Output is correct
25 Correct 11 ms 1144 KB Output is correct
26 Correct 44 ms 5240 KB Output is correct
27 Correct 65 ms 6264 KB Output is correct
28 Correct 73 ms 8568 KB Output is correct
29 Correct 21 ms 3320 KB Output is correct
30 Correct 4 ms 632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 5688 KB Output is correct
2 Correct 12 ms 1912 KB Output is correct
3 Correct 27 ms 4080 KB Output is correct
4 Correct 30 ms 4472 KB Output is correct
5 Correct 37 ms 5496 KB Output is correct
6 Correct 24 ms 3292 KB Output is correct
7 Correct 7 ms 1016 KB Output is correct
8 Correct 24 ms 3704 KB Output is correct
9 Correct 60 ms 8824 KB Output is correct
10 Correct 34 ms 5064 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 3 ms 376 KB Output is correct
15 Correct 11 ms 380 KB Output is correct
16 Correct 3 ms 376 KB Output is correct
17 Correct 3 ms 376 KB Output is correct
18 Correct 3 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 3 ms 376 KB Output is correct
23 Correct 3 ms 376 KB Output is correct
24 Correct 3 ms 376 KB Output is correct
25 Correct 3 ms 380 KB Output is correct
26 Correct 20 ms 2680 KB Output is correct
27 Correct 48 ms 7672 KB Output is correct
28 Correct 75 ms 8696 KB Output is correct
29 Correct 267 ms 7952 KB Output is correct
30 Correct 298 ms 8952 KB Output is correct
31 Correct 47 ms 7416 KB Output is correct
32 Correct 3 ms 376 KB Output is correct
33 Correct 49 ms 4088 KB Output is correct
34 Correct 69 ms 8184 KB Output is correct
35 Correct 11 ms 1144 KB Output is correct
36 Correct 44 ms 5240 KB Output is correct
37 Correct 65 ms 6264 KB Output is correct
38 Correct 73 ms 8568 KB Output is correct
39 Correct 21 ms 3320 KB Output is correct
40 Correct 4 ms 632 KB Output is correct
41 Correct 155 ms 8184 KB Output is correct
42 Correct 71 ms 9336 KB Output is correct
43 Correct 93 ms 8184 KB Output is correct
44 Correct 62 ms 8952 KB Output is correct
45 Correct 63 ms 9592 KB Output is correct
46 Correct 219 ms 8696 KB Output is correct
47 Correct 58 ms 8056 KB Output is correct
48 Correct 82 ms 8568 KB Output is correct
49 Correct 57 ms 7416 KB Output is correct
50 Correct 857 ms 8568 KB Output is correct
51 Correct 905 ms 9464 KB Output is correct
52 Correct 845 ms 8824 KB Output is correct
53 Correct 893 ms 9468 KB Output is correct
54 Correct 826 ms 8696 KB Output is correct
55 Correct 954 ms 9720 KB Output is correct
56 Correct 796 ms 8440 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 42 ms 5688 KB Output is correct
10 Correct 12 ms 1912 KB Output is correct
11 Correct 27 ms 4080 KB Output is correct
12 Correct 30 ms 4472 KB Output is correct
13 Correct 37 ms 5496 KB Output is correct
14 Correct 24 ms 3292 KB Output is correct
15 Correct 7 ms 1016 KB Output is correct
16 Correct 24 ms 3704 KB Output is correct
17 Correct 60 ms 8824 KB Output is correct
18 Correct 34 ms 5064 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 3 ms 376 KB Output is correct
23 Correct 11 ms 380 KB Output is correct
24 Correct 3 ms 376 KB Output is correct
25 Correct 3 ms 376 KB Output is correct
26 Correct 3 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 3 ms 376 KB Output is correct
31 Correct 3 ms 376 KB Output is correct
32 Correct 3 ms 376 KB Output is correct
33 Correct 3 ms 380 KB Output is correct
34 Correct 20 ms 2680 KB Output is correct
35 Correct 48 ms 7672 KB Output is correct
36 Correct 75 ms 8696 KB Output is correct
37 Correct 267 ms 7952 KB Output is correct
38 Correct 298 ms 8952 KB Output is correct
39 Correct 47 ms 7416 KB Output is correct
40 Correct 3 ms 376 KB Output is correct
41 Correct 49 ms 4088 KB Output is correct
42 Correct 69 ms 8184 KB Output is correct
43 Correct 11 ms 1144 KB Output is correct
44 Correct 44 ms 5240 KB Output is correct
45 Correct 65 ms 6264 KB Output is correct
46 Correct 73 ms 8568 KB Output is correct
47 Correct 21 ms 3320 KB Output is correct
48 Correct 4 ms 632 KB Output is correct
49 Correct 155 ms 8184 KB Output is correct
50 Correct 71 ms 9336 KB Output is correct
51 Correct 93 ms 8184 KB Output is correct
52 Correct 62 ms 8952 KB Output is correct
53 Correct 63 ms 9592 KB Output is correct
54 Correct 219 ms 8696 KB Output is correct
55 Correct 58 ms 8056 KB Output is correct
56 Correct 82 ms 8568 KB Output is correct
57 Correct 57 ms 7416 KB Output is correct
58 Correct 857 ms 8568 KB Output is correct
59 Correct 905 ms 9464 KB Output is correct
60 Correct 845 ms 8824 KB Output is correct
61 Correct 893 ms 9468 KB Output is correct
62 Correct 826 ms 8696 KB Output is correct
63 Correct 954 ms 9720 KB Output is correct
64 Correct 796 ms 8440 KB Output is correct
65 Correct 2 ms 376 KB Output is correct
66 Correct 2 ms 376 KB Output is correct
67 Correct 861 ms 9116 KB Output is correct
68 Correct 875 ms 9208 KB Output is correct
69 Correct 702 ms 8056 KB Output is correct
70 Correct 843 ms 8824 KB Output is correct
71 Correct 843 ms 8828 KB Output is correct
72 Correct 874 ms 8568 KB Output is correct
73 Correct 859 ms 8440 KB Output is correct
74 Correct 855 ms 9080 KB Output is correct
75 Correct 848 ms 8568 KB Output is correct
76 Correct 878 ms 9336 KB Output is correct
77 Correct 138 ms 9720 KB Output is correct
78 Correct 72 ms 8904 KB Output is correct
79 Correct 123 ms 8312 KB Output is correct
80 Correct 126 ms 8696 KB Output is correct
81 Correct 102 ms 8056 KB Output is correct
82 Correct 98 ms 9224 KB Output is correct
83 Correct 68 ms 8440 KB Output is correct
84 Correct 453 ms 9592 KB Output is correct
85 Correct 998 ms 9720 KB Output is correct
86 Correct 2 ms 376 KB Output is correct
87 Correct 2 ms 296 KB Output is correct
88 Correct 947 ms 9692 KB Output is correct
89 Correct 149 ms 2040 KB Output is correct
90 Correct 2 ms 376 KB Output is correct