답안 #343846

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
343846 2021-01-04T14:50:02 Z emil_physmath Chessboard (IZhO18_chessboard) C++17
100 / 100
1515 ms 4728 KB
/*#include <iostream>
#include <stdio.h>
using namespace std;
const int MAXK=100005;

long long mini[MAXK], maxi[MAXK], minj[MAXK], maxj[MAXK];

long long NumWhite(long long, long long, long long, long long, long long cellLen);
long long FindAns(long long n, long long k, long long cellLen);
int main()
{
	long long n, k;
	cin>>n>>k;
	for (int i=0; i<k; i++)
		scanf("%lld%lld%lld%lld", mini+i, minj+i, maxi+i, maxj+i);
	long long  ans=n*n;
	for (int cellLen=1; cellLen*cellLen<=n; cellLen++)
		if (n%cellLen==0)
		{
			ans=min(ans, FindAns(n, k, cellLen));
			ans=min(ans, FindAns(n, k, n/cellLen));
		}
	cout<<ans<<'\n';

	char I;
	cin >> I;
	return 0;
}

long long FindAns(long long n, long long k, long long cellLen)
{
	if (cellLen>=n) return n*n;
	long long shouldWhite=0, shouldBlack=0, isBlack=0;
	for (int i=0; i<k; i++)
	{
		shouldWhite+=NumWhite(mini[i], minj[i], maxi[i], maxj[i], cellLen);
		isBlack+=(maxi[i]-mini[i]+1)*(maxj[i]-minj[i]+1);
	}
	shouldBlack=(n*n-NumWhite(1, 1, n, n, cellLen))-(isBlack-shouldWhite);
	return min(shouldWhite+shouldBlack, n*n-(shouldWhite+shouldBlack));
}
using ll = long long;
struct el {
	ll a, b, c, d;
};
ll prefans(ll x, ll y, ll k, ll f)
{
	ll ans;
	ll ab = y / k, a;
	a = (ab + 1) / 2;
	a *= k;
	if (ab % 2 == 0)
		a += y % k;
	if (x <= k)
		ans = x * a;
	else
	{
		ll aa = y - a;
		ll q = x / k;
		ans = aa * (q / 2) * k;
		ans += a * ((q + 1) / 2) * k;
		if (q % 2)
			ans += aa * (x % k);
		else ans += a * (x % k);
	}
	if (f == 1)
		return ans;
	else return x * y - ans;
}
ll rect(el s, ll d, int f) {
	ll mec = prefans(s.c, s.d, d, f) - (s.c * s.d - prefans(s.c, s.d, d, f));
	ll poqr = prefans(s.a, s.b, d, f) - (s.a * s.b - prefans(s.a, s.b, d, f));;
	return mec - poqr;
}
long long NumWhite(long long mini, long long minj, long long maxi, long long maxj, long long len)
{
	if (!mini) mini=1;
	if (!minj) minj=1;
	return rect(el{mini, minj, maxi, maxj}, len, 1);
	if (mini==1 && minj==1)
	{
        return prefans(maxi, maxj, len, 1);
		long long  ans=0, i=maxi, j=maxj;
		ans+=len*len*(((i/len)*(j/len)+1)/2);
		if ((i/len)%2)
			ans+=(i%len)*((j/len)/2)*len;
		else
			ans+=(i%len)*((j/len+1)/2)*len;
		if ((j/len)%2)
			ans+=(j%len)*((i/len)/2)*len;
		else
			ans+=(j%len)*((i/len+1)/2)*len;
		if ((i/len)%2==(j/len)%2)
			ans+=(i%len)*(j%len);
		return ans;
	}
	return NumWhite(1, 1, maxi, maxj, len)-
		NumWhite(1, 1, maxi, minj-1, len)-NumWhite(1, 1, mini-1, maxj, len)+
		NumWhite(1, 1, mini-1, minj-1, len);
}*/

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
using ll = long long;
struct el {
	ll a, b, c, d;
};
vector<ll> dv;
vector<el> points;
ll ans;
ll prefans(ll x, ll y, ll k, int f)
{
	ll ans;
	ll ab = y / k, a;
	a = (ab + 1) / 2;
	a *= k;
	if (ab % 2 == 0)
		a += y % k;
	if (x <= k)
		ans = x * a;
	else
	{
		ll aa = y - a;
		ll q = x / k;
		ans = aa * (q / 2) * k;
		ans += a * ((q + 1) / 2) * k;
		if (q % 2)
			ans += aa * (x % k);
		else ans += a * (x % k);
	}
	if (f == 1)
		return ans;
	else return x * y - ans;
}
ll rect(ll x, ll y, ll d, int f) {
	return prefans(x, y, d, f) - (x * y - prefans(x, y, d, f));
}
int main()
{
	ll n, k;
	cin >> n >> k;
	ans = n * n;
	for (int i = 1; i < n; i++)
		if (n % i == 0)dv.push_back(i);
	for (int i = 0; i < k; i++)
	{
		ll a, b, c, d;
		cin >> a >> b >> c >> d;
		points.push_back({ a,b,c,d });
	}/*
	for (auto p : points)
		cerr << p.first << ' ' << p.second << '\n';*/
	for (ll d : dv)
	{
		ll blackans;
		ll q = n / d;
		if (q % 2)
			blackans = (n * n - d * d) / 2 + d * d;
		else blackans = n * n / 2;
		ll whiteans = n * n - blackans;
		whiteans = prefans(n, n, d, 1);
		blackans = prefans(n, n, d, 0);
		for (auto p : points)
		{
			whiteans += rect(p.c, p.d, d, 0) - rect(p.a - 1, p.d, d, 0) - rect(p.c, p.b - 1, d, 0) + rect(p.a - 1, p.b - 1, d, 0);
			blackans += rect(p.c, p.d, d, 1) - rect(p.a - 1, p.d, d, 1) - rect(p.c, p.b - 1, d, 1) + rect(p.a - 1, p.b - 1, d, 1);
		}
		ans = min(ans, min(blackans, whiteans));
	}
	cout << ans;
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 0 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 84 ms 2528 KB Output is correct
2 Correct 22 ms 1508 KB Output is correct
3 Correct 53 ms 2528 KB Output is correct
4 Correct 54 ms 2528 KB Output is correct
5 Correct 71 ms 2528 KB Output is correct
6 Correct 48 ms 2528 KB Output is correct
7 Correct 10 ms 748 KB Output is correct
8 Correct 46 ms 2528 KB Output is correct
9 Correct 116 ms 4592 KB Output is correct
10 Correct 66 ms 2528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 2 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 2 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 2 ms 364 KB Output is correct
14 Correct 2 ms 364 KB Output is correct
15 Correct 2 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 2 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 2 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 2 ms 364 KB Output is correct
14 Correct 2 ms 364 KB Output is correct
15 Correct 2 ms 364 KB Output is correct
16 Correct 34 ms 1508 KB Output is correct
17 Correct 91 ms 4508 KB Output is correct
18 Correct 126 ms 4572 KB Output is correct
19 Correct 317 ms 4572 KB Output is correct
20 Correct 349 ms 4572 KB Output is correct
21 Correct 86 ms 4572 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 69 ms 2528 KB Output is correct
24 Correct 113 ms 4572 KB Output is correct
25 Correct 14 ms 1000 KB Output is correct
26 Correct 71 ms 2528 KB Output is correct
27 Correct 97 ms 4552 KB Output is correct
28 Correct 123 ms 4572 KB Output is correct
29 Correct 36 ms 2528 KB Output is correct
30 Correct 4 ms 492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 84 ms 2528 KB Output is correct
2 Correct 22 ms 1508 KB Output is correct
3 Correct 53 ms 2528 KB Output is correct
4 Correct 54 ms 2528 KB Output is correct
5 Correct 71 ms 2528 KB Output is correct
6 Correct 48 ms 2528 KB Output is correct
7 Correct 10 ms 748 KB Output is correct
8 Correct 46 ms 2528 KB Output is correct
9 Correct 116 ms 4592 KB Output is correct
10 Correct 66 ms 2528 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 2 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 2 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 2 ms 364 KB Output is correct
24 Correct 2 ms 364 KB Output is correct
25 Correct 2 ms 364 KB Output is correct
26 Correct 34 ms 1508 KB Output is correct
27 Correct 91 ms 4508 KB Output is correct
28 Correct 126 ms 4572 KB Output is correct
29 Correct 317 ms 4572 KB Output is correct
30 Correct 349 ms 4572 KB Output is correct
31 Correct 86 ms 4572 KB Output is correct
32 Correct 2 ms 384 KB Output is correct
33 Correct 69 ms 2528 KB Output is correct
34 Correct 113 ms 4572 KB Output is correct
35 Correct 14 ms 1000 KB Output is correct
36 Correct 71 ms 2528 KB Output is correct
37 Correct 97 ms 4552 KB Output is correct
38 Correct 123 ms 4572 KB Output is correct
39 Correct 36 ms 2528 KB Output is correct
40 Correct 4 ms 492 KB Output is correct
41 Correct 302 ms 4572 KB Output is correct
42 Correct 147 ms 4596 KB Output is correct
43 Correct 198 ms 4572 KB Output is correct
44 Correct 139 ms 4572 KB Output is correct
45 Correct 131 ms 4572 KB Output is correct
46 Correct 341 ms 4572 KB Output is correct
47 Correct 113 ms 4604 KB Output is correct
48 Correct 171 ms 4572 KB Output is correct
49 Correct 131 ms 4572 KB Output is correct
50 Correct 1342 ms 4572 KB Output is correct
51 Correct 1431 ms 4572 KB Output is correct
52 Correct 1335 ms 4572 KB Output is correct
53 Correct 1417 ms 4572 KB Output is correct
54 Correct 1308 ms 4572 KB Output is correct
55 Correct 1472 ms 4572 KB Output is correct
56 Correct 1274 ms 4572 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 0 ms 364 KB Output is correct
9 Correct 84 ms 2528 KB Output is correct
10 Correct 22 ms 1508 KB Output is correct
11 Correct 53 ms 2528 KB Output is correct
12 Correct 54 ms 2528 KB Output is correct
13 Correct 71 ms 2528 KB Output is correct
14 Correct 48 ms 2528 KB Output is correct
15 Correct 10 ms 748 KB Output is correct
16 Correct 46 ms 2528 KB Output is correct
17 Correct 116 ms 4592 KB Output is correct
18 Correct 66 ms 2528 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 2 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 2 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 2 ms 364 KB Output is correct
32 Correct 2 ms 364 KB Output is correct
33 Correct 2 ms 364 KB Output is correct
34 Correct 34 ms 1508 KB Output is correct
35 Correct 91 ms 4508 KB Output is correct
36 Correct 126 ms 4572 KB Output is correct
37 Correct 317 ms 4572 KB Output is correct
38 Correct 349 ms 4572 KB Output is correct
39 Correct 86 ms 4572 KB Output is correct
40 Correct 2 ms 384 KB Output is correct
41 Correct 69 ms 2528 KB Output is correct
42 Correct 113 ms 4572 KB Output is correct
43 Correct 14 ms 1000 KB Output is correct
44 Correct 71 ms 2528 KB Output is correct
45 Correct 97 ms 4552 KB Output is correct
46 Correct 123 ms 4572 KB Output is correct
47 Correct 36 ms 2528 KB Output is correct
48 Correct 4 ms 492 KB Output is correct
49 Correct 302 ms 4572 KB Output is correct
50 Correct 147 ms 4596 KB Output is correct
51 Correct 198 ms 4572 KB Output is correct
52 Correct 139 ms 4572 KB Output is correct
53 Correct 131 ms 4572 KB Output is correct
54 Correct 341 ms 4572 KB Output is correct
55 Correct 113 ms 4604 KB Output is correct
56 Correct 171 ms 4572 KB Output is correct
57 Correct 131 ms 4572 KB Output is correct
58 Correct 1342 ms 4572 KB Output is correct
59 Correct 1431 ms 4572 KB Output is correct
60 Correct 1335 ms 4572 KB Output is correct
61 Correct 1417 ms 4572 KB Output is correct
62 Correct 1308 ms 4572 KB Output is correct
63 Correct 1472 ms 4572 KB Output is correct
64 Correct 1274 ms 4572 KB Output is correct
65 Correct 1 ms 364 KB Output is correct
66 Correct 1 ms 364 KB Output is correct
67 Correct 1377 ms 4544 KB Output is correct
68 Correct 1387 ms 4572 KB Output is correct
69 Correct 1220 ms 4572 KB Output is correct
70 Correct 1388 ms 4572 KB Output is correct
71 Correct 1337 ms 4728 KB Output is correct
72 Correct 1324 ms 4572 KB Output is correct
73 Correct 1265 ms 4572 KB Output is correct
74 Correct 1351 ms 4720 KB Output is correct
75 Correct 1349 ms 4700 KB Output is correct
76 Correct 1413 ms 4572 KB Output is correct
77 Correct 281 ms 4572 KB Output is correct
78 Correct 138 ms 4572 KB Output is correct
79 Correct 202 ms 4572 KB Output is correct
80 Correct 217 ms 4572 KB Output is correct
81 Correct 201 ms 4572 KB Output is correct
82 Correct 186 ms 4572 KB Output is correct
83 Correct 153 ms 4508 KB Output is correct
84 Correct 851 ms 4720 KB Output is correct
85 Correct 1421 ms 4700 KB Output is correct
86 Correct 2 ms 364 KB Output is correct
87 Correct 1 ms 364 KB Output is correct
88 Correct 1515 ms 4620 KB Output is correct
89 Correct 285 ms 1540 KB Output is correct
90 Correct 1 ms 364 KB Output is correct