/**
* LES GREATEABLES BRO TEAM
**/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define sz(x) (int)x.size()
const bool FLAG = false;
void setIO(const string &f = "");
string to_string(const string s) {
return '"' + s + '"';
}
string to_string(const char c) {
return char(39) + string(1, c) + char(39);
}
string to_string(const char* s) {
return to_string(string(s));
}
string to_string(bool f) {
return (f ? "true" : "false");
}
template<class A, class B>
string to_string(const pair<A, B> x) {
return "(" + to_string(x.first) + ", " + to_string(x.second) + ")";
}
template<class A, class B, class C>
string to_string(const tuple<A, B, C> x) {
return "(" + to_string(get<0>(x)) + ", " + to_string(get<1>(x)) + ", " + to_string(get<2>(x)) + ")";
}
template<class A, class B, class C, class D>
string to_string(const tuple<A, B, C, D> x) {
return "(" + to_string(get<0>(x)) + ", " + to_string(get<1>(x)) + ", " + to_string(get<2>(x)) + ", " + to_string(get<3>(x)) + ")";
}
template<class T>
string to_string(const T v) {
string res = "{"; bool f = true;
for (auto x: v)
res += (f ? to_string(x) : ", " + to_string(x)), f = false;
return res + "}";
}
void debug_args() { cerr << "]\n"; }
template<class H, class... T>
void debug_args(H x, T... y) {
cerr << to_string(x);
if (sizeof... (y))
cerr << ", ";
debug_args(y...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: [", debug_args(__VA_ARGS__);
#else
#define debug(...) 47
#endif
bool is_prime(int x) {
if (x <= 1)
return false;
for (int i = 2; i * i <= x; i++)
if (x % i == 0)
return false;
return true;
}
void solve() {
int n, k;
cin >> n >> k;
vector<int> up(k + 1), left(k + 1), down(k + 1), right(k + 1);
bool flag = 1;
vector<vector<int>> a(1020, vector<int>(1020));
for (int i = 1; i <= k; i++) {
cin >> up[i] >> left[i] >> down[i] >> right[i];
if (up[i] != down[i] || left[i] != right[i])
flag = 0;
if (n <= 1000) {
for (int ii = up[i]; ii <= down[i]; ii++)
for (int jj = left[i]; jj <= right[i]; jj++)
a[ii][jj] = 1;
}
}
if (n <= 1000) {
int ans = n * n * 3;
for (int len = 1; len * len <= n; len++) {
if (n % len == 0) {
int turn = 0, cur = 0, st_turn = 0;
for (int i = 1; i <= n; i += len) {
turn = st_turn;
st_turn ^= 1;
for (int j = 1; j <= n; j += len) {
for (int ii = i; ii < i + len; ii++) {
for (int jj = j; jj < j + len; jj++) {
if (a[ii][jj] != turn)
cur++;
}
}
turn ^= 1;
}
}
ans = min(ans, cur);
cur = 0, st_turn = 1;
for (int i = 1; i <= n; i += len) {
turn = st_turn;
st_turn ^= 1;
for (int j = 1; j <= n; j += len) {
for (int ii = i; ii < i + len; ii++) {
for (int jj = j; jj < j + len; jj++) {
if (a[ii][jj] != turn)
cur++;
}
}
turn ^= 1;
}
}
ans = min(ans, cur);
if (len != 1) {
int len2 = n / len;
st_turn = 0, cur = 0;
for (int i = 1; i <= n; i += len2) {
turn = st_turn;
st_turn ^= 1;
for (int j = 1; j <= n; j += len2) {
for (int ii = i; ii < i + len2; ii++) {
for (int jj = j; jj < j + len2; jj++) {
if (a[ii][jj] != turn)
cur++;
}
}
turn ^= 1;
}
}
ans = min(ans, cur);
cur = 0, st_turn = 1;
for (int i = 1; i <= n; i += len2) {
turn = st_turn;
st_turn ^= 1;
for (int j = 1; j <= n; j += len2) {
for (int ii = i; ii < i + len2; ii++) {
for (int jj = j; jj < j + len2; jj++) {
if (a[ii][jj] != turn)
cur++;
}
}
turn ^= 1;
}
}
ans = min(ans, cur);
}
}
}
cout << ans;
}
else if (is_prime(n)) {
vector<int> v(2);
for (int i = 1; i <= k; i++) {
v[(up[i] + left[i]) % 2]++;
}
cout << min(v[1] + (n / 2 - v[0]), v[0] + ((n + 1) / 2 - v[1]));
}
/*
else if (flag) {
for (int i = 1; i <= k; i++) {
a[up[i]][left[i]] = 1;
}
}*/
}
signed main() {
setIO();
int tt = 1;
if (FLAG) {
cin >> tt;
}
while (tt--) {
solve();
}
return 0;
}
void setIO(const string &f) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (fopen((f + ".in").c_str(), "r")) {
freopen((f + ".in").c_str(), "r", stdin);
freopen((f + ".out").c_str(), "w", stdout);
}
}
Compilation message
chessboard.cpp: In function 'void solve()':
chessboard.cpp:73:10: warning: variable 'flag' set but not used [-Wunused-but-set-variable]
73 | bool flag = 1;
| ^~~~
chessboard.cpp: In function 'void setIO(const string&)':
chessboard.cpp:196:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
196 | freopen((f + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
chessboard.cpp:197:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
197 | freopen((f + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4300 KB |
Output is correct |
2 |
Correct |
4 ms |
4364 KB |
Output is correct |
3 |
Correct |
2 ms |
4404 KB |
Output is correct |
4 |
Correct |
2 ms |
4300 KB |
Output is correct |
5 |
Correct |
2 ms |
4428 KB |
Output is correct |
6 |
Correct |
4 ms |
4428 KB |
Output is correct |
7 |
Correct |
3 ms |
4300 KB |
Output is correct |
8 |
Correct |
2 ms |
4300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
22 ms |
5324 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4428 KB |
Output is correct |
2 |
Correct |
2 ms |
4404 KB |
Output is correct |
3 |
Correct |
2 ms |
4428 KB |
Output is correct |
4 |
Correct |
3 ms |
4428 KB |
Output is correct |
5 |
Correct |
2 ms |
4428 KB |
Output is correct |
6 |
Correct |
2 ms |
4428 KB |
Output is correct |
7 |
Correct |
3 ms |
4428 KB |
Output is correct |
8 |
Correct |
3 ms |
4416 KB |
Output is correct |
9 |
Correct |
3 ms |
4428 KB |
Output is correct |
10 |
Correct |
3 ms |
4428 KB |
Output is correct |
11 |
Correct |
3 ms |
4404 KB |
Output is correct |
12 |
Correct |
2 ms |
4416 KB |
Output is correct |
13 |
Correct |
2 ms |
4428 KB |
Output is correct |
14 |
Correct |
3 ms |
4416 KB |
Output is correct |
15 |
Correct |
3 ms |
4428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4428 KB |
Output is correct |
2 |
Correct |
2 ms |
4404 KB |
Output is correct |
3 |
Correct |
2 ms |
4428 KB |
Output is correct |
4 |
Correct |
3 ms |
4428 KB |
Output is correct |
5 |
Correct |
2 ms |
4428 KB |
Output is correct |
6 |
Correct |
2 ms |
4428 KB |
Output is correct |
7 |
Correct |
3 ms |
4428 KB |
Output is correct |
8 |
Correct |
3 ms |
4416 KB |
Output is correct |
9 |
Correct |
3 ms |
4428 KB |
Output is correct |
10 |
Correct |
3 ms |
4428 KB |
Output is correct |
11 |
Correct |
3 ms |
4404 KB |
Output is correct |
12 |
Correct |
2 ms |
4416 KB |
Output is correct |
13 |
Correct |
2 ms |
4428 KB |
Output is correct |
14 |
Correct |
3 ms |
4416 KB |
Output is correct |
15 |
Correct |
3 ms |
4428 KB |
Output is correct |
16 |
Correct |
16 ms |
5104 KB |
Output is correct |
17 |
Correct |
33 ms |
6028 KB |
Output is correct |
18 |
Correct |
61 ms |
6236 KB |
Output is correct |
19 |
Correct |
85 ms |
6300 KB |
Output is correct |
20 |
Correct |
82 ms |
6320 KB |
Output is correct |
21 |
Correct |
40 ms |
5936 KB |
Output is correct |
22 |
Correct |
31 ms |
4548 KB |
Output is correct |
23 |
Correct |
36 ms |
5392 KB |
Output is correct |
24 |
Correct |
41 ms |
6192 KB |
Output is correct |
25 |
Correct |
19 ms |
4676 KB |
Output is correct |
26 |
Correct |
24 ms |
5652 KB |
Output is correct |
27 |
Correct |
37 ms |
5772 KB |
Output is correct |
28 |
Correct |
48 ms |
6268 KB |
Output is correct |
29 |
Correct |
24 ms |
5244 KB |
Output is correct |
30 |
Correct |
11 ms |
4508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
22 ms |
5324 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4300 KB |
Output is correct |
2 |
Correct |
4 ms |
4364 KB |
Output is correct |
3 |
Correct |
2 ms |
4404 KB |
Output is correct |
4 |
Correct |
2 ms |
4300 KB |
Output is correct |
5 |
Correct |
2 ms |
4428 KB |
Output is correct |
6 |
Correct |
4 ms |
4428 KB |
Output is correct |
7 |
Correct |
3 ms |
4300 KB |
Output is correct |
8 |
Correct |
2 ms |
4300 KB |
Output is correct |
9 |
Incorrect |
22 ms |
5324 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |