#include "bits/stdc++.h"
#define int long long
using namespace std;
const int MAXN = 1e5 + 10;
void solve(int tc) {
int n,k;
cin >> n>>k;
int x1[k],y1[k],x2[k],y2[k];
for(int i=0; i<k; i++) {
cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];
x1[i]--; y1[i]--;
x2[i]--; y2[i]--;
}
vector<int> prof;
for(int i=1; i*i<=n; i++) {
if(n%i == 0) {
prof.push_back(i);
if(i*i!=n && i!=1) prof.push_back(n/i);
}
}
sort(prof.begin(), prof.end());
int ans = 1e18;
for(int s: prof) {
int c[2] = {0, 0}; // number of black cells for each (i/S + j/S) mod 2
for(int i=0; i<k; i++) {
int type0 = 0;
int type1 = 0;
int l = y1[i], r = y2[i];
if(l / (2*s) == r / (2*s)) {
if(r % (2*s) < s) type0 += r-l+1;
else if(l % (2*s) >= s) type0 += 0;
else type0 += s - (l % (2*s));
}
else {
if(l % (2*s) >= s) {
int md = l % (2*s);
l += 2*s - md;
}
else {
int md = l % (2*s);
type0 += s - md;
l += 2*s - md;
}
if(r % (2*s) >= s) {
int md = r % (2*s);
r -= md+1;
type0 += s;
}
else {
int md = r % (2*s);
r -= md+1;
type0 += md+1;
}
type0 += (r-l+1) / 2;
}
type1 = y2[i]-y1[i]+1 - type0;
l = x1[i], r = x2[i];
int type2 = 0;
int type3 = 0;
if(l / (2*s) == r / (2*s)) {
if(r % (2*s) < s) type2 += r-l+1;
else if(l % (2*s) >= s) type2 += 0;
else type2 += s - (l % (2*s));
}
else {
if(l % (2*s) >= s) {
int md = l % (2*s);
l += 2*s - md;
}
else {
int md = l % (2*s);
type2 += s - md;
l += 2*s - md;
}
if(r % (2*s) >= s) {
int md = r % (2*s);
r -= md+1;
type2 += s;
}
else {
int md = r % (2*s);
r -= md+1;
type2 += md+1;
}
type2 += (r-l+1) / 2;
}
type3 = x2[i]-x1[i]+1 - type2;
//cout<<s<<" "<<type0<<" "<<type1<<" "<<type2<<" "<<type3<<"\n";
int d = type0 * type2 + type1 * type3;
c[0] += d;
c[1] += (x2[i] - x1[i] + 1) * (y2[i] - y1[i] + 1) - d;
}
int t[2] = {0, 0};
int nums = (n/s) * (n/s);
t[0] = s*s * ((nums+1) / 2);
t[1] = s*s * (nums / 2);
ans = min(ans, abs(t[0] - c[0]) + c[1]);
ans = min(ans, abs(t[1] - c[1]) + c[0]);
// cout<<ans<<"\n";
}
cout << ans << "\n";
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t=1; //cin>>t;
for(int i=1; i<=t; i++)solve(i);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
0 ms |
316 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
3652 KB |
Output is correct |
2 |
Correct |
7 ms |
1096 KB |
Output is correct |
3 |
Correct |
15 ms |
2424 KB |
Output is correct |
4 |
Correct |
17 ms |
2600 KB |
Output is correct |
5 |
Correct |
20 ms |
3284 KB |
Output is correct |
6 |
Correct |
18 ms |
2132 KB |
Output is correct |
7 |
Correct |
3 ms |
656 KB |
Output is correct |
8 |
Correct |
15 ms |
2164 KB |
Output is correct |
9 |
Correct |
34 ms |
5168 KB |
Output is correct |
10 |
Correct |
18 ms |
3032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
228 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
228 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
14 ms |
1544 KB |
Output is correct |
17 |
Correct |
32 ms |
4172 KB |
Output is correct |
18 |
Correct |
39 ms |
4940 KB |
Output is correct |
19 |
Correct |
91 ms |
4468 KB |
Output is correct |
20 |
Correct |
111 ms |
4984 KB |
Output is correct |
21 |
Correct |
28 ms |
4100 KB |
Output is correct |
22 |
Correct |
1 ms |
320 KB |
Output is correct |
23 |
Correct |
23 ms |
2268 KB |
Output is correct |
24 |
Correct |
33 ms |
4532 KB |
Output is correct |
25 |
Correct |
5 ms |
724 KB |
Output is correct |
26 |
Correct |
21 ms |
2896 KB |
Output is correct |
27 |
Correct |
30 ms |
3456 KB |
Output is correct |
28 |
Correct |
36 ms |
4812 KB |
Output is correct |
29 |
Correct |
11 ms |
1852 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
3652 KB |
Output is correct |
2 |
Correct |
7 ms |
1096 KB |
Output is correct |
3 |
Correct |
15 ms |
2424 KB |
Output is correct |
4 |
Correct |
17 ms |
2600 KB |
Output is correct |
5 |
Correct |
20 ms |
3284 KB |
Output is correct |
6 |
Correct |
18 ms |
2132 KB |
Output is correct |
7 |
Correct |
3 ms |
656 KB |
Output is correct |
8 |
Correct |
15 ms |
2164 KB |
Output is correct |
9 |
Correct |
34 ms |
5168 KB |
Output is correct |
10 |
Correct |
18 ms |
3032 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
228 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
14 ms |
1544 KB |
Output is correct |
27 |
Correct |
32 ms |
4172 KB |
Output is correct |
28 |
Correct |
39 ms |
4940 KB |
Output is correct |
29 |
Correct |
91 ms |
4468 KB |
Output is correct |
30 |
Correct |
111 ms |
4984 KB |
Output is correct |
31 |
Correct |
28 ms |
4100 KB |
Output is correct |
32 |
Correct |
1 ms |
320 KB |
Output is correct |
33 |
Correct |
23 ms |
2268 KB |
Output is correct |
34 |
Correct |
33 ms |
4532 KB |
Output is correct |
35 |
Correct |
5 ms |
724 KB |
Output is correct |
36 |
Correct |
21 ms |
2896 KB |
Output is correct |
37 |
Correct |
30 ms |
3456 KB |
Output is correct |
38 |
Correct |
36 ms |
4812 KB |
Output is correct |
39 |
Correct |
11 ms |
1852 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
110 ms |
4804 KB |
Output is correct |
42 |
Correct |
43 ms |
5412 KB |
Output is correct |
43 |
Correct |
54 ms |
4820 KB |
Output is correct |
44 |
Correct |
50 ms |
5312 KB |
Output is correct |
45 |
Correct |
41 ms |
5660 KB |
Output is correct |
46 |
Correct |
110 ms |
5428 KB |
Output is correct |
47 |
Correct |
31 ms |
4948 KB |
Output is correct |
48 |
Correct |
53 ms |
5120 KB |
Output is correct |
49 |
Correct |
63 ms |
4828 KB |
Output is correct |
50 |
Correct |
457 ms |
5320 KB |
Output is correct |
51 |
Correct |
471 ms |
5644 KB |
Output is correct |
52 |
Correct |
415 ms |
5280 KB |
Output is correct |
53 |
Correct |
463 ms |
5580 KB |
Output is correct |
54 |
Correct |
422 ms |
5216 KB |
Output is correct |
55 |
Correct |
523 ms |
5816 KB |
Output is correct |
56 |
Correct |
401 ms |
5064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
0 ms |
316 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
39 ms |
3652 KB |
Output is correct |
10 |
Correct |
7 ms |
1096 KB |
Output is correct |
11 |
Correct |
15 ms |
2424 KB |
Output is correct |
12 |
Correct |
17 ms |
2600 KB |
Output is correct |
13 |
Correct |
20 ms |
3284 KB |
Output is correct |
14 |
Correct |
18 ms |
2132 KB |
Output is correct |
15 |
Correct |
3 ms |
656 KB |
Output is correct |
16 |
Correct |
15 ms |
2164 KB |
Output is correct |
17 |
Correct |
34 ms |
5168 KB |
Output is correct |
18 |
Correct |
18 ms |
3032 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
228 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
14 ms |
1544 KB |
Output is correct |
35 |
Correct |
32 ms |
4172 KB |
Output is correct |
36 |
Correct |
39 ms |
4940 KB |
Output is correct |
37 |
Correct |
91 ms |
4468 KB |
Output is correct |
38 |
Correct |
111 ms |
4984 KB |
Output is correct |
39 |
Correct |
28 ms |
4100 KB |
Output is correct |
40 |
Correct |
1 ms |
320 KB |
Output is correct |
41 |
Correct |
23 ms |
2268 KB |
Output is correct |
42 |
Correct |
33 ms |
4532 KB |
Output is correct |
43 |
Correct |
5 ms |
724 KB |
Output is correct |
44 |
Correct |
21 ms |
2896 KB |
Output is correct |
45 |
Correct |
30 ms |
3456 KB |
Output is correct |
46 |
Correct |
36 ms |
4812 KB |
Output is correct |
47 |
Correct |
11 ms |
1852 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
110 ms |
4804 KB |
Output is correct |
50 |
Correct |
43 ms |
5412 KB |
Output is correct |
51 |
Correct |
54 ms |
4820 KB |
Output is correct |
52 |
Correct |
50 ms |
5312 KB |
Output is correct |
53 |
Correct |
41 ms |
5660 KB |
Output is correct |
54 |
Correct |
110 ms |
5428 KB |
Output is correct |
55 |
Correct |
31 ms |
4948 KB |
Output is correct |
56 |
Correct |
53 ms |
5120 KB |
Output is correct |
57 |
Correct |
63 ms |
4828 KB |
Output is correct |
58 |
Correct |
457 ms |
5320 KB |
Output is correct |
59 |
Correct |
471 ms |
5644 KB |
Output is correct |
60 |
Correct |
415 ms |
5280 KB |
Output is correct |
61 |
Correct |
463 ms |
5580 KB |
Output is correct |
62 |
Correct |
422 ms |
5216 KB |
Output is correct |
63 |
Correct |
523 ms |
5816 KB |
Output is correct |
64 |
Correct |
401 ms |
5064 KB |
Output is correct |
65 |
Correct |
1 ms |
212 KB |
Output is correct |
66 |
Correct |
1 ms |
212 KB |
Output is correct |
67 |
Correct |
436 ms |
5468 KB |
Output is correct |
68 |
Correct |
441 ms |
5444 KB |
Output is correct |
69 |
Correct |
410 ms |
4792 KB |
Output is correct |
70 |
Correct |
408 ms |
5400 KB |
Output is correct |
71 |
Correct |
406 ms |
5212 KB |
Output is correct |
72 |
Correct |
418 ms |
5116 KB |
Output is correct |
73 |
Correct |
386 ms |
4992 KB |
Output is correct |
74 |
Correct |
449 ms |
5360 KB |
Output is correct |
75 |
Correct |
429 ms |
5164 KB |
Output is correct |
76 |
Correct |
466 ms |
5500 KB |
Output is correct |
77 |
Correct |
110 ms |
5676 KB |
Output is correct |
78 |
Correct |
54 ms |
5188 KB |
Output is correct |
79 |
Correct |
58 ms |
4852 KB |
Output is correct |
80 |
Correct |
60 ms |
5100 KB |
Output is correct |
81 |
Correct |
63 ms |
4828 KB |
Output is correct |
82 |
Correct |
54 ms |
5476 KB |
Output is correct |
83 |
Correct |
59 ms |
5064 KB |
Output is correct |
84 |
Correct |
256 ms |
5632 KB |
Output is correct |
85 |
Correct |
453 ms |
5724 KB |
Output is correct |
86 |
Correct |
1 ms |
320 KB |
Output is correct |
87 |
Correct |
1 ms |
320 KB |
Output is correct |
88 |
Correct |
452 ms |
5844 KB |
Output is correct |
89 |
Correct |
82 ms |
1236 KB |
Output is correct |
90 |
Correct |
1 ms |
212 KB |
Output is correct |