Submission #378881

#TimeUsernameProblemLanguageResultExecution timeMemory
378881cheissmartChessboard (IZhO18_chessboard)C++14
100 / 100
741 ms2176 KiB
#include <bits/stdc++.h>
#define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << "Line(" << __LINE__ << ") -> " << #x << " is " << x << endl

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;

const int INF = 1e9 + 7, N = 1e5 + 7;
const ll oo = 1e18;

array<int, 4> a[N];

signed main()
{
	IO_OP;

	int n, k;
	cin >> n >> k;

	for(int i = 0; i < k; i++) {
		for(int j = 0; j < 4; j++)
			cin >> a[i][j], a[i][j]--;
	}

	ll ans = oo;
	auto go = [&] (int b) {
		int m = n / b;
		ll white = (ll) m * m / 2 * b * b, black = (ll) n * n - white;
		ll wb = 0;
		auto solve = [&] (int x, int y) {
			if(x < 0 || y < 0) return 0LL;
			int l = (x + 1) % (2 * b), r = (y + 1) % (2 * b);
			ll W = 0, B = 0;
			B += (ll) min(l, b) * min(r, b);
			B += (ll) max(0, l - b) * max(0, r - b);
			W += (ll) min(l, b) * max(0, r - b);
			W += (ll) max(0, l - b) * min(r, b);
			if((x / (2 * b) * 2 + y / (2 * b) * 2) & 1)
				swap(W, B);
			return W - B;
		};
		for(int i = 0; i < k; i++) {
			wb += solve(a[i][2], a[i][3]) - solve(a[i][0] - 1, a[i][3]) - solve(a[i][2], a[i][1] - 1) + solve(a[i][0] - 1, a[i][1] - 1);
		}
		return black + wb;
	};

	for(int i = 1; i < n; i++) {
		if(n % i == 0) {
			ll t = go(i);
			t = min(t, (ll) n * n - t);
			ans = min(ans, t);
		}
	}
	cout << ans << '\n';

}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...