#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
using ll = long long;
struct el {
int a, b, c, d;
};
vector<ll> dv;
vector<el> points;
ll ans;
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;
}
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;
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);
}
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++)
{
int 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;
blackans = n * n - NumWhite(1, 1, n, n, d);
whiteans = NumWhite(1, 1, n, n, d);
//cerr << blackans << ' ' << whiteans << " 1, 1, " << n << ", " << n << " " << NumWhite(1, 1, n, n, d) << '\n';
for (auto p : points)
{
blackans -= (p.d - p.b + 1) * (p.c - p.a + 1) - NumWhite(p.a, p.b, p.c, p.d, d);
whiteans -= NumWhite(p.a, p.b, p.c, p.d, d);
whiteans += (p.d - p.b + 1) * (p.c - p.a + 1) - NumWhite(p.a, p.b, p.c, p.d, d);
blackans += NumWhite(p.a, p.b, p.c, p.d, d);
//blackans += prefans(p.c, p.d, d, 1) - prefans(p.a - 1, p.d, d, 1) - prefans(p.c, p.b - 1, d, 1) + 2 * prefans(p.a - 1, p.b - 1, d, 1);
//whiteans += prefans(p.c, p.d, d, 0) - prefans(p.a - 1, p.d, d, 0) - prefans(p.c, p.b - 1, d, 0) + 2 * prefans(p.a - 1, p.b - 1, d, 0);
}
//cerr << blackans << ' ' << whiteans << '\n';
ans = min(ans, min(blackans, whiteans));
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
1508 KB |
Output is correct |
2 |
Correct |
20 ms |
1256 KB |
Output is correct |
3 |
Correct |
51 ms |
2404 KB |
Output is correct |
4 |
Correct |
51 ms |
2276 KB |
Output is correct |
5 |
Correct |
70 ms |
2560 KB |
Output is correct |
6 |
Correct |
45 ms |
2276 KB |
Output is correct |
7 |
Correct |
10 ms |
748 KB |
Output is correct |
8 |
Correct |
45 ms |
2372 KB |
Output is correct |
9 |
Correct |
115 ms |
4212 KB |
Output is correct |
10 |
Correct |
68 ms |
2532 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 |
492 KB |
Output is correct |
11 |
Correct |
1 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 |
492 KB |
Output is correct |
11 |
Correct |
1 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 |
31 ms |
1384 KB |
Output is correct |
17 |
Correct |
84 ms |
3840 KB |
Output is correct |
18 |
Correct |
114 ms |
3808 KB |
Output is correct |
19 |
Correct |
226 ms |
3808 KB |
Output is correct |
20 |
Correct |
255 ms |
3808 KB |
Output is correct |
21 |
Correct |
81 ms |
3808 KB |
Output is correct |
22 |
Correct |
2 ms |
364 KB |
Output is correct |
23 |
Correct |
58 ms |
2148 KB |
Output is correct |
24 |
Correct |
101 ms |
3808 KB |
Output is correct |
25 |
Correct |
12 ms |
876 KB |
Output is correct |
26 |
Correct |
64 ms |
2296 KB |
Output is correct |
27 |
Correct |
85 ms |
3552 KB |
Output is correct |
28 |
Correct |
107 ms |
3960 KB |
Output is correct |
29 |
Correct |
34 ms |
2148 KB |
Output is correct |
30 |
Correct |
4 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
1508 KB |
Output is correct |
2 |
Correct |
20 ms |
1256 KB |
Output is correct |
3 |
Correct |
51 ms |
2404 KB |
Output is correct |
4 |
Correct |
51 ms |
2276 KB |
Output is correct |
5 |
Correct |
70 ms |
2560 KB |
Output is correct |
6 |
Correct |
45 ms |
2276 KB |
Output is correct |
7 |
Correct |
10 ms |
748 KB |
Output is correct |
8 |
Correct |
45 ms |
2372 KB |
Output is correct |
9 |
Correct |
115 ms |
4212 KB |
Output is correct |
10 |
Correct |
68 ms |
2532 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 |
492 KB |
Output is correct |
21 |
Correct |
1 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 |
31 ms |
1384 KB |
Output is correct |
27 |
Correct |
84 ms |
3840 KB |
Output is correct |
28 |
Correct |
114 ms |
3808 KB |
Output is correct |
29 |
Correct |
226 ms |
3808 KB |
Output is correct |
30 |
Correct |
255 ms |
3808 KB |
Output is correct |
31 |
Correct |
81 ms |
3808 KB |
Output is correct |
32 |
Correct |
2 ms |
364 KB |
Output is correct |
33 |
Correct |
58 ms |
2148 KB |
Output is correct |
34 |
Correct |
101 ms |
3808 KB |
Output is correct |
35 |
Correct |
12 ms |
876 KB |
Output is correct |
36 |
Correct |
64 ms |
2296 KB |
Output is correct |
37 |
Correct |
85 ms |
3552 KB |
Output is correct |
38 |
Correct |
107 ms |
3960 KB |
Output is correct |
39 |
Correct |
34 ms |
2148 KB |
Output is correct |
40 |
Correct |
4 ms |
492 KB |
Output is correct |
41 |
Correct |
224 ms |
4340 KB |
Output is correct |
42 |
Correct |
133 ms |
4476 KB |
Output is correct |
43 |
Correct |
175 ms |
4320 KB |
Output is correct |
44 |
Correct |
128 ms |
4320 KB |
Output is correct |
45 |
Correct |
124 ms |
4320 KB |
Output is correct |
46 |
Correct |
252 ms |
4320 KB |
Output is correct |
47 |
Correct |
114 ms |
4320 KB |
Output is correct |
48 |
Correct |
147 ms |
4320 KB |
Output is correct |
49 |
Correct |
118 ms |
4320 KB |
Output is correct |
50 |
Correct |
854 ms |
4448 KB |
Output is correct |
51 |
Correct |
911 ms |
4320 KB |
Output is correct |
52 |
Correct |
853 ms |
4320 KB |
Output is correct |
53 |
Correct |
905 ms |
4320 KB |
Output is correct |
54 |
Correct |
837 ms |
4448 KB |
Output is correct |
55 |
Correct |
946 ms |
4444 KB |
Output is correct |
56 |
Correct |
820 ms |
4320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
79 ms |
1508 KB |
Output is correct |
10 |
Correct |
20 ms |
1256 KB |
Output is correct |
11 |
Correct |
51 ms |
2404 KB |
Output is correct |
12 |
Correct |
51 ms |
2276 KB |
Output is correct |
13 |
Correct |
70 ms |
2560 KB |
Output is correct |
14 |
Correct |
45 ms |
2276 KB |
Output is correct |
15 |
Correct |
10 ms |
748 KB |
Output is correct |
16 |
Correct |
45 ms |
2372 KB |
Output is correct |
17 |
Correct |
115 ms |
4212 KB |
Output is correct |
18 |
Correct |
68 ms |
2532 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 |
492 KB |
Output is correct |
29 |
Correct |
1 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 |
31 ms |
1384 KB |
Output is correct |
35 |
Correct |
84 ms |
3840 KB |
Output is correct |
36 |
Correct |
114 ms |
3808 KB |
Output is correct |
37 |
Correct |
226 ms |
3808 KB |
Output is correct |
38 |
Correct |
255 ms |
3808 KB |
Output is correct |
39 |
Correct |
81 ms |
3808 KB |
Output is correct |
40 |
Correct |
2 ms |
364 KB |
Output is correct |
41 |
Correct |
58 ms |
2148 KB |
Output is correct |
42 |
Correct |
101 ms |
3808 KB |
Output is correct |
43 |
Correct |
12 ms |
876 KB |
Output is correct |
44 |
Correct |
64 ms |
2296 KB |
Output is correct |
45 |
Correct |
85 ms |
3552 KB |
Output is correct |
46 |
Correct |
107 ms |
3960 KB |
Output is correct |
47 |
Correct |
34 ms |
2148 KB |
Output is correct |
48 |
Correct |
4 ms |
492 KB |
Output is correct |
49 |
Correct |
224 ms |
4340 KB |
Output is correct |
50 |
Correct |
133 ms |
4476 KB |
Output is correct |
51 |
Correct |
175 ms |
4320 KB |
Output is correct |
52 |
Correct |
128 ms |
4320 KB |
Output is correct |
53 |
Correct |
124 ms |
4320 KB |
Output is correct |
54 |
Correct |
252 ms |
4320 KB |
Output is correct |
55 |
Correct |
114 ms |
4320 KB |
Output is correct |
56 |
Correct |
147 ms |
4320 KB |
Output is correct |
57 |
Correct |
118 ms |
4320 KB |
Output is correct |
58 |
Correct |
854 ms |
4448 KB |
Output is correct |
59 |
Correct |
911 ms |
4320 KB |
Output is correct |
60 |
Correct |
853 ms |
4320 KB |
Output is correct |
61 |
Correct |
905 ms |
4320 KB |
Output is correct |
62 |
Correct |
837 ms |
4448 KB |
Output is correct |
63 |
Correct |
946 ms |
4444 KB |
Output is correct |
64 |
Correct |
820 ms |
4320 KB |
Output is correct |
65 |
Correct |
1 ms |
364 KB |
Output is correct |
66 |
Correct |
1 ms |
364 KB |
Output is correct |
67 |
Correct |
886 ms |
4320 KB |
Output is correct |
68 |
Correct |
891 ms |
4320 KB |
Output is correct |
69 |
Correct |
778 ms |
4320 KB |
Output is correct |
70 |
Correct |
863 ms |
4320 KB |
Output is correct |
71 |
Correct |
863 ms |
4320 KB |
Output is correct |
72 |
Correct |
838 ms |
4320 KB |
Output is correct |
73 |
Correct |
808 ms |
4320 KB |
Output is correct |
74 |
Correct |
891 ms |
4320 KB |
Output is correct |
75 |
Correct |
854 ms |
4320 KB |
Output is correct |
76 |
Correct |
908 ms |
4320 KB |
Output is correct |
77 |
Correct |
221 ms |
4320 KB |
Output is correct |
78 |
Correct |
126 ms |
4192 KB |
Output is correct |
79 |
Correct |
167 ms |
4320 KB |
Output is correct |
80 |
Correct |
175 ms |
4320 KB |
Output is correct |
81 |
Correct |
162 ms |
4412 KB |
Output is correct |
82 |
Correct |
157 ms |
4320 KB |
Output is correct |
83 |
Correct |
135 ms |
4372 KB |
Output is correct |
84 |
Correct |
563 ms |
4444 KB |
Output is correct |
85 |
Correct |
915 ms |
4444 KB |
Output is correct |
86 |
Correct |
2 ms |
364 KB |
Output is correct |
87 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
88 |
Halted |
0 ms |
0 KB |
- |