#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: In function 'void getcost(long long int)':
chessboard.cpp:91:9: warning: unused variable 'mincost' [-Wunused-variable]
int mincost = (wsq-k) * d*d;
^~~~~~~
chessboard.cpp: At global scope:
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 |
41 ms |
5752 KB |
Output is correct |
2 |
Correct |
12 ms |
1912 KB |
Output is correct |
3 |
Correct |
26 ms |
3320 KB |
Output is correct |
4 |
Correct |
29 ms |
4472 KB |
Output is correct |
5 |
Correct |
37 ms |
5724 KB |
Output is correct |
6 |
Correct |
24 ms |
3320 KB |
Output is correct |
7 |
Correct |
7 ms |
1024 KB |
Output is correct |
8 |
Correct |
24 ms |
3576 KB |
Output is correct |
9 |
Correct |
59 ms |
8796 KB |
Output is correct |
10 |
Correct |
34 ms |
4984 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 |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
372 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
3 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
3 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
3 ms |
376 KB |
Output is correct |
14 |
Correct |
3 ms |
380 KB |
Output is correct |
15 |
Correct |
3 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 |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
372 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
3 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
3 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
3 ms |
376 KB |
Output is correct |
14 |
Correct |
3 ms |
380 KB |
Output is correct |
15 |
Correct |
3 ms |
376 KB |
Output is correct |
16 |
Correct |
23 ms |
2680 KB |
Output is correct |
17 |
Correct |
48 ms |
7644 KB |
Output is correct |
18 |
Correct |
75 ms |
8696 KB |
Output is correct |
19 |
Correct |
268 ms |
8008 KB |
Output is correct |
20 |
Correct |
299 ms |
8840 KB |
Output is correct |
21 |
Correct |
47 ms |
7288 KB |
Output is correct |
22 |
Correct |
3 ms |
376 KB |
Output is correct |
23 |
Correct |
50 ms |
3960 KB |
Output is correct |
24 |
Correct |
68 ms |
8244 KB |
Output is correct |
25 |
Correct |
12 ms |
1016 KB |
Output is correct |
26 |
Correct |
44 ms |
5112 KB |
Output is correct |
27 |
Correct |
65 ms |
6264 KB |
Output is correct |
28 |
Correct |
73 ms |
8696 KB |
Output is correct |
29 |
Correct |
20 ms |
3292 KB |
Output is correct |
30 |
Correct |
4 ms |
620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
5752 KB |
Output is correct |
2 |
Correct |
12 ms |
1912 KB |
Output is correct |
3 |
Correct |
26 ms |
3320 KB |
Output is correct |
4 |
Correct |
29 ms |
4472 KB |
Output is correct |
5 |
Correct |
37 ms |
5724 KB |
Output is correct |
6 |
Correct |
24 ms |
3320 KB |
Output is correct |
7 |
Correct |
7 ms |
1024 KB |
Output is correct |
8 |
Correct |
24 ms |
3576 KB |
Output is correct |
9 |
Correct |
59 ms |
8796 KB |
Output is correct |
10 |
Correct |
34 ms |
4984 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 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 |
376 KB |
Output is correct |
16 |
Correct |
3 ms |
372 KB |
Output is correct |
17 |
Correct |
3 ms |
376 KB |
Output is correct |
18 |
Correct |
3 ms |
376 KB |
Output is correct |
19 |
Correct |
3 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
3 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
3 ms |
376 KB |
Output is correct |
24 |
Correct |
3 ms |
380 KB |
Output is correct |
25 |
Correct |
3 ms |
376 KB |
Output is correct |
26 |
Correct |
23 ms |
2680 KB |
Output is correct |
27 |
Correct |
48 ms |
7644 KB |
Output is correct |
28 |
Correct |
75 ms |
8696 KB |
Output is correct |
29 |
Correct |
268 ms |
8008 KB |
Output is correct |
30 |
Correct |
299 ms |
8840 KB |
Output is correct |
31 |
Correct |
47 ms |
7288 KB |
Output is correct |
32 |
Correct |
3 ms |
376 KB |
Output is correct |
33 |
Correct |
50 ms |
3960 KB |
Output is correct |
34 |
Correct |
68 ms |
8244 KB |
Output is correct |
35 |
Correct |
12 ms |
1016 KB |
Output is correct |
36 |
Correct |
44 ms |
5112 KB |
Output is correct |
37 |
Correct |
65 ms |
6264 KB |
Output is correct |
38 |
Correct |
73 ms |
8696 KB |
Output is correct |
39 |
Correct |
20 ms |
3292 KB |
Output is correct |
40 |
Correct |
4 ms |
620 KB |
Output is correct |
41 |
Correct |
228 ms |
8184 KB |
Output is correct |
42 |
Correct |
83 ms |
9208 KB |
Output is correct |
43 |
Correct |
133 ms |
8184 KB |
Output is correct |
44 |
Correct |
76 ms |
8952 KB |
Output is correct |
45 |
Correct |
64 ms |
9464 KB |
Output is correct |
46 |
Correct |
254 ms |
8700 KB |
Output is correct |
47 |
Correct |
57 ms |
8056 KB |
Output is correct |
48 |
Correct |
107 ms |
8568 KB |
Output is correct |
49 |
Correct |
71 ms |
7416 KB |
Output is correct |
50 |
Correct |
1142 ms |
8696 KB |
Output is correct |
51 |
Correct |
1239 ms |
9492 KB |
Output is correct |
52 |
Correct |
1131 ms |
8824 KB |
Output is correct |
53 |
Correct |
1209 ms |
9464 KB |
Output is correct |
54 |
Correct |
1112 ms |
8700 KB |
Output is correct |
55 |
Correct |
1252 ms |
9720 KB |
Output is correct |
56 |
Correct |
1087 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 |
41 ms |
5752 KB |
Output is correct |
10 |
Correct |
12 ms |
1912 KB |
Output is correct |
11 |
Correct |
26 ms |
3320 KB |
Output is correct |
12 |
Correct |
29 ms |
4472 KB |
Output is correct |
13 |
Correct |
37 ms |
5724 KB |
Output is correct |
14 |
Correct |
24 ms |
3320 KB |
Output is correct |
15 |
Correct |
7 ms |
1024 KB |
Output is correct |
16 |
Correct |
24 ms |
3576 KB |
Output is correct |
17 |
Correct |
59 ms |
8796 KB |
Output is correct |
18 |
Correct |
34 ms |
4984 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
3 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 |
372 KB |
Output is correct |
25 |
Correct |
3 ms |
376 KB |
Output is correct |
26 |
Correct |
3 ms |
376 KB |
Output is correct |
27 |
Correct |
3 ms |
376 KB |
Output is correct |
28 |
Correct |
2 ms |
376 KB |
Output is correct |
29 |
Correct |
3 ms |
376 KB |
Output is correct |
30 |
Correct |
2 ms |
376 KB |
Output is correct |
31 |
Correct |
3 ms |
376 KB |
Output is correct |
32 |
Correct |
3 ms |
380 KB |
Output is correct |
33 |
Correct |
3 ms |
376 KB |
Output is correct |
34 |
Correct |
23 ms |
2680 KB |
Output is correct |
35 |
Correct |
48 ms |
7644 KB |
Output is correct |
36 |
Correct |
75 ms |
8696 KB |
Output is correct |
37 |
Correct |
268 ms |
8008 KB |
Output is correct |
38 |
Correct |
299 ms |
8840 KB |
Output is correct |
39 |
Correct |
47 ms |
7288 KB |
Output is correct |
40 |
Correct |
3 ms |
376 KB |
Output is correct |
41 |
Correct |
50 ms |
3960 KB |
Output is correct |
42 |
Correct |
68 ms |
8244 KB |
Output is correct |
43 |
Correct |
12 ms |
1016 KB |
Output is correct |
44 |
Correct |
44 ms |
5112 KB |
Output is correct |
45 |
Correct |
65 ms |
6264 KB |
Output is correct |
46 |
Correct |
73 ms |
8696 KB |
Output is correct |
47 |
Correct |
20 ms |
3292 KB |
Output is correct |
48 |
Correct |
4 ms |
620 KB |
Output is correct |
49 |
Correct |
228 ms |
8184 KB |
Output is correct |
50 |
Correct |
83 ms |
9208 KB |
Output is correct |
51 |
Correct |
133 ms |
8184 KB |
Output is correct |
52 |
Correct |
76 ms |
8952 KB |
Output is correct |
53 |
Correct |
64 ms |
9464 KB |
Output is correct |
54 |
Correct |
254 ms |
8700 KB |
Output is correct |
55 |
Correct |
57 ms |
8056 KB |
Output is correct |
56 |
Correct |
107 ms |
8568 KB |
Output is correct |
57 |
Correct |
71 ms |
7416 KB |
Output is correct |
58 |
Correct |
1142 ms |
8696 KB |
Output is correct |
59 |
Correct |
1239 ms |
9492 KB |
Output is correct |
60 |
Correct |
1131 ms |
8824 KB |
Output is correct |
61 |
Correct |
1209 ms |
9464 KB |
Output is correct |
62 |
Correct |
1112 ms |
8700 KB |
Output is correct |
63 |
Correct |
1252 ms |
9720 KB |
Output is correct |
64 |
Correct |
1087 ms |
8440 KB |
Output is correct |
65 |
Correct |
2 ms |
376 KB |
Output is correct |
66 |
Correct |
2 ms |
248 KB |
Output is correct |
67 |
Correct |
1173 ms |
9116 KB |
Output is correct |
68 |
Correct |
1180 ms |
9208 KB |
Output is correct |
69 |
Correct |
1014 ms |
7988 KB |
Output is correct |
70 |
Correct |
1124 ms |
8868 KB |
Output is correct |
71 |
Correct |
1109 ms |
8696 KB |
Output is correct |
72 |
Correct |
1091 ms |
8696 KB |
Output is correct |
73 |
Correct |
1073 ms |
8440 KB |
Output is correct |
74 |
Correct |
1162 ms |
9080 KB |
Output is correct |
75 |
Correct |
1097 ms |
8696 KB |
Output is correct |
76 |
Correct |
1183 ms |
9208 KB |
Output is correct |
77 |
Correct |
193 ms |
9592 KB |
Output is correct |
78 |
Correct |
77 ms |
8876 KB |
Output is correct |
79 |
Correct |
135 ms |
8312 KB |
Output is correct |
80 |
Correct |
141 ms |
8696 KB |
Output is correct |
81 |
Correct |
133 ms |
8056 KB |
Output is correct |
82 |
Correct |
117 ms |
9208 KB |
Output is correct |
83 |
Correct |
88 ms |
8440 KB |
Output is correct |
84 |
Correct |
705 ms |
9464 KB |
Output is correct |
85 |
Correct |
1400 ms |
9720 KB |
Output is correct |
86 |
Correct |
3 ms |
376 KB |
Output is correct |
87 |
Correct |
2 ms |
376 KB |
Output is correct |
88 |
Correct |
1247 ms |
9720 KB |
Output is correct |
89 |
Correct |
233 ms |
2168 KB |
Output is correct |
90 |
Correct |
2 ms |
508 KB |
Output is correct |