답안 #495214

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
495214 2021-12-18T06:42:02 Z Ziel Chessboard (IZhO18_chessboard) C++17
70 / 100
238 ms 12284 KB
/**
 * 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

#define int ll

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)) {
    	assert(flag);
    	vector<ll> v(2);
    	for (int i = 1; i <= k; i++) {
    		v[(up[i] + left[i]) % 2]++;
    	}
    	cout << min(v[0] + ((n * n) / 2 - v[1]), v[1] + ((n * n + 1) / 2 - v[0]));
    }

    else {
    	vector<ll> vv(2);
    	for (int i = 1; i <= k; i++) {
    		vv[(up[i] + left[i]) % 2]++;
    	}
    	ll ans = min(vv[0] + ((n * n) / 2 - vv[1]), vv[1] + ((n * n + 1) / 2 - vv[0]));
    	for (int len = 2; len * len <= n; len++) {
    		if (n % len == 0) {
    			vector<ll> v(2);
	    		for (int i = 1; i <= k; i++) {
	    			int x = (up[i] + len - 1) / len;
	    			int y = (left[i] + len - 1) / len;
	    			v[(x + y) % 2]++;
	    		}
	    		ll cur = min(v[0] + ((n / len) * (n / len) / 2 * len * len - v[1]), v[1] + (((n / len) * (n / len) + 1) / 2 * len * len - v[0]));
	    		ans = min(ans, cur);

	    		ll len2 = n / len;
	    		v[0] = v[1] = 0;
	    		for (int i = 1; i <= k; i++) {
	    			int x = (up[i] + len2 - 1) / len2;
	    			int y = (left[i] + len2 - 1) / len2;
	    			v[(x + y) % 2]++;
	    		}
	    		cur = min(v[0] + ((n / len2) * (n / len2) / 2 * len2 * len2 - v[1]), v[1] + (((n / len2) * (n / len2) + 1) / 2 * len2 * len2 - v[0]));
	    		ans = min(ans, cur);
    		}
    	}
    	cout << ans;
    }
}

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 setIO(const string&)':
chessboard.cpp:223:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  223 |         freopen((f + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
chessboard.cpp:224:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  224 |         freopen((f + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8536 KB Output is correct
2 Correct 4 ms 8396 KB Output is correct
3 Correct 4 ms 8396 KB Output is correct
4 Correct 4 ms 8504 KB Output is correct
5 Correct 4 ms 8396 KB Output is correct
6 Correct 4 ms 8396 KB Output is correct
7 Correct 4 ms 8396 KB Output is correct
8 Correct 4 ms 8504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 10444 KB Output is correct
2 Correct 9 ms 9036 KB Output is correct
3 Correct 17 ms 9676 KB Output is correct
4 Correct 20 ms 9964 KB Output is correct
5 Correct 23 ms 10240 KB Output is correct
6 Correct 14 ms 9632 KB Output is correct
7 Correct 6 ms 8728 KB Output is correct
8 Correct 16 ms 9548 KB Output is correct
9 Correct 33 ms 11400 KB Output is correct
10 Correct 45 ms 10132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 8396 KB Output is correct
2 Correct 4 ms 8396 KB Output is correct
3 Correct 4 ms 8396 KB Output is correct
4 Correct 8 ms 8524 KB Output is correct
5 Correct 5 ms 8524 KB Output is correct
6 Correct 6 ms 8524 KB Output is correct
7 Correct 6 ms 8524 KB Output is correct
8 Correct 4 ms 8524 KB Output is correct
9 Correct 4 ms 8524 KB Output is correct
10 Correct 4 ms 8504 KB Output is correct
11 Correct 7 ms 8524 KB Output is correct
12 Correct 6 ms 8524 KB Output is correct
13 Correct 4 ms 8524 KB Output is correct
14 Correct 4 ms 8524 KB Output is correct
15 Correct 5 ms 8524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 8396 KB Output is correct
2 Correct 4 ms 8396 KB Output is correct
3 Correct 4 ms 8396 KB Output is correct
4 Correct 8 ms 8524 KB Output is correct
5 Correct 5 ms 8524 KB Output is correct
6 Correct 6 ms 8524 KB Output is correct
7 Correct 6 ms 8524 KB Output is correct
8 Correct 4 ms 8524 KB Output is correct
9 Correct 4 ms 8524 KB Output is correct
10 Correct 4 ms 8504 KB Output is correct
11 Correct 7 ms 8524 KB Output is correct
12 Correct 6 ms 8524 KB Output is correct
13 Correct 4 ms 8524 KB Output is correct
14 Correct 4 ms 8524 KB Output is correct
15 Correct 5 ms 8524 KB Output is correct
16 Correct 31 ms 9412 KB Output is correct
17 Correct 49 ms 11196 KB Output is correct
18 Correct 47 ms 11592 KB Output is correct
19 Correct 104 ms 11308 KB Output is correct
20 Correct 92 ms 11540 KB Output is correct
21 Correct 116 ms 11096 KB Output is correct
22 Correct 37 ms 8500 KB Output is correct
23 Correct 34 ms 9840 KB Output is correct
24 Correct 45 ms 11340 KB Output is correct
25 Correct 23 ms 8768 KB Output is correct
26 Correct 64 ms 10424 KB Output is correct
27 Correct 66 ms 10800 KB Output is correct
28 Correct 51 ms 11796 KB Output is correct
29 Correct 21 ms 9768 KB Output is correct
30 Correct 20 ms 8524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 10444 KB Output is correct
2 Correct 9 ms 9036 KB Output is correct
3 Correct 17 ms 9676 KB Output is correct
4 Correct 20 ms 9964 KB Output is correct
5 Correct 23 ms 10240 KB Output is correct
6 Correct 14 ms 9632 KB Output is correct
7 Correct 6 ms 8728 KB Output is correct
8 Correct 16 ms 9548 KB Output is correct
9 Correct 33 ms 11400 KB Output is correct
10 Correct 45 ms 10132 KB Output is correct
11 Correct 3 ms 8396 KB Output is correct
12 Correct 4 ms 8396 KB Output is correct
13 Correct 4 ms 8396 KB Output is correct
14 Correct 8 ms 8524 KB Output is correct
15 Correct 5 ms 8524 KB Output is correct
16 Correct 6 ms 8524 KB Output is correct
17 Correct 6 ms 8524 KB Output is correct
18 Correct 4 ms 8524 KB Output is correct
19 Correct 4 ms 8524 KB Output is correct
20 Correct 4 ms 8504 KB Output is correct
21 Correct 7 ms 8524 KB Output is correct
22 Correct 6 ms 8524 KB Output is correct
23 Correct 4 ms 8524 KB Output is correct
24 Correct 4 ms 8524 KB Output is correct
25 Correct 5 ms 8524 KB Output is correct
26 Correct 31 ms 9412 KB Output is correct
27 Correct 49 ms 11196 KB Output is correct
28 Correct 47 ms 11592 KB Output is correct
29 Correct 104 ms 11308 KB Output is correct
30 Correct 92 ms 11540 KB Output is correct
31 Correct 116 ms 11096 KB Output is correct
32 Correct 37 ms 8500 KB Output is correct
33 Correct 34 ms 9840 KB Output is correct
34 Correct 45 ms 11340 KB Output is correct
35 Correct 23 ms 8768 KB Output is correct
36 Correct 64 ms 10424 KB Output is correct
37 Correct 66 ms 10800 KB Output is correct
38 Correct 51 ms 11796 KB Output is correct
39 Correct 21 ms 9768 KB Output is correct
40 Correct 20 ms 8524 KB Output is correct
41 Correct 73 ms 11528 KB Output is correct
42 Correct 43 ms 12044 KB Output is correct
43 Correct 83 ms 11556 KB Output is correct
44 Correct 40 ms 11964 KB Output is correct
45 Correct 51 ms 12228 KB Output is correct
46 Correct 71 ms 11988 KB Output is correct
47 Correct 37 ms 11608 KB Output is correct
48 Correct 43 ms 11636 KB Output is correct
49 Correct 34 ms 11576 KB Output is correct
50 Correct 218 ms 11944 KB Output is correct
51 Correct 232 ms 12184 KB Output is correct
52 Correct 224 ms 11896 KB Output is correct
53 Correct 218 ms 12220 KB Output is correct
54 Correct 216 ms 11924 KB Output is correct
55 Correct 238 ms 12284 KB Output is correct
56 Correct 214 ms 11816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8536 KB Output is correct
2 Correct 4 ms 8396 KB Output is correct
3 Correct 4 ms 8396 KB Output is correct
4 Correct 4 ms 8504 KB Output is correct
5 Correct 4 ms 8396 KB Output is correct
6 Correct 4 ms 8396 KB Output is correct
7 Correct 4 ms 8396 KB Output is correct
8 Correct 4 ms 8504 KB Output is correct
9 Correct 24 ms 10444 KB Output is correct
10 Correct 9 ms 9036 KB Output is correct
11 Correct 17 ms 9676 KB Output is correct
12 Correct 20 ms 9964 KB Output is correct
13 Correct 23 ms 10240 KB Output is correct
14 Correct 14 ms 9632 KB Output is correct
15 Correct 6 ms 8728 KB Output is correct
16 Correct 16 ms 9548 KB Output is correct
17 Correct 33 ms 11400 KB Output is correct
18 Correct 45 ms 10132 KB Output is correct
19 Correct 3 ms 8396 KB Output is correct
20 Correct 4 ms 8396 KB Output is correct
21 Correct 4 ms 8396 KB Output is correct
22 Correct 8 ms 8524 KB Output is correct
23 Correct 5 ms 8524 KB Output is correct
24 Correct 6 ms 8524 KB Output is correct
25 Correct 6 ms 8524 KB Output is correct
26 Correct 4 ms 8524 KB Output is correct
27 Correct 4 ms 8524 KB Output is correct
28 Correct 4 ms 8504 KB Output is correct
29 Correct 7 ms 8524 KB Output is correct
30 Correct 6 ms 8524 KB Output is correct
31 Correct 4 ms 8524 KB Output is correct
32 Correct 4 ms 8524 KB Output is correct
33 Correct 5 ms 8524 KB Output is correct
34 Correct 31 ms 9412 KB Output is correct
35 Correct 49 ms 11196 KB Output is correct
36 Correct 47 ms 11592 KB Output is correct
37 Correct 104 ms 11308 KB Output is correct
38 Correct 92 ms 11540 KB Output is correct
39 Correct 116 ms 11096 KB Output is correct
40 Correct 37 ms 8500 KB Output is correct
41 Correct 34 ms 9840 KB Output is correct
42 Correct 45 ms 11340 KB Output is correct
43 Correct 23 ms 8768 KB Output is correct
44 Correct 64 ms 10424 KB Output is correct
45 Correct 66 ms 10800 KB Output is correct
46 Correct 51 ms 11796 KB Output is correct
47 Correct 21 ms 9768 KB Output is correct
48 Correct 20 ms 8524 KB Output is correct
49 Correct 73 ms 11528 KB Output is correct
50 Correct 43 ms 12044 KB Output is correct
51 Correct 83 ms 11556 KB Output is correct
52 Correct 40 ms 11964 KB Output is correct
53 Correct 51 ms 12228 KB Output is correct
54 Correct 71 ms 11988 KB Output is correct
55 Correct 37 ms 11608 KB Output is correct
56 Correct 43 ms 11636 KB Output is correct
57 Correct 34 ms 11576 KB Output is correct
58 Correct 218 ms 11944 KB Output is correct
59 Correct 232 ms 12184 KB Output is correct
60 Correct 224 ms 11896 KB Output is correct
61 Correct 218 ms 12220 KB Output is correct
62 Correct 216 ms 11924 KB Output is correct
63 Correct 238 ms 12284 KB Output is correct
64 Correct 214 ms 11816 KB Output is correct
65 Correct 4 ms 8396 KB Output is correct
66 Correct 8 ms 8508 KB Output is correct
67 Incorrect 216 ms 11900 KB Output isn't correct
68 Halted 0 ms 0 KB -