#include <bits/stdc++.h>
#define ios ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
#define all(a) a.begin() , a.end()
#define F first
#define S second
using namespace std;
using ll = long long;
#define y1 dsfsdf
#define int ll
const int N = 2e5+5 , inf = 2e9 + 7;
const ll INF = 1e18 , mod = 1e9+7 , P = 6547;
int x1[N] , y1[N] , x2[N] , y2[N];
void solve() {
ll n, k;
cin >> n >> k;
if(k == 0) {
ll ans = n*n / 2;
for(int i = 2; i < n; i ++) {
if(n%i == 0) {
int x = i;
if((n/x) % 2) {
ans = min(ans , (n/x) * (n/x) / 2 * x*x);
}
}
}
cout << ans;
return;
}
ll blacks = 0;
for(int i = 1; i <= k; i ++) {
cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];
blacks += (x2[i]-x1[i]+1) * (y2[i]-y1[i]+1);
x1[i] --;
x2[i] --;
y1[i] --;
y2[i] --;
}
ll ans = n*n;
for(int i = 1; i < n; i ++) {
if(n%i == 0) {
int RightBlacks = 0;
for(int j = 1; j <= k; j ++) {
int cnt[2] , cnt1[2];
fill(cnt , cnt+2 , 0);
fill(cnt1 , cnt1+2 , 0);
{
int l = x1[j], r = x2[j];
int l1 = l/i*i+i , r1 = r/i*i-1;
if(l1 <= r1) {
assert(0);
assert((r1-l1+1) % i == 0);
int col = (r1-l1+1) / i;
cnt[0] = col/2 * i;
cnt[1] = col/2 * i;
if(col%2) {
cnt[l1/i % 2] += i;
}
cnt[l/i % 2] += l1-l;
if(r != l)
cnt[r/i % 2] += (r-r1);
}
else {
int l1 = l , r1 = r , res;
while(l1 <= r1) {
int md = l1+r1 >> 1;
if(l/i%2 == md/i%2) {
res = md;
l1 = md+1;
}
else {
r1 = md-1;
}
}
cnt[l/i%2] += res-l+1;
cnt[r/i%2] += r-res;
}
}
{
int l = y1[j], r = y2[j];
int l1 = l/i*i+i , r1 = r/i*i-1;
if(l1 <= r1) {
assert(0);
assert((r1-l1+1) % i == 0);
int col = (r1-l1+1) / i;
cnt1[0] = col/2 * i;
cnt1[1] = col/2 * i;
if(col%2) {
cnt[l1/i % 2] += i;
}
cnt1[l/i % 2] += l1-l;
if(r != l)
cnt1[r/i % 2] += (r-r1);
}
else {
int l1 = l , r1 = r , res;
while(l1 <= r1) {
int md = l1+r1 >> 1;
if(l/i%2 == md/i%2) {
res = md;
l1 = md+1;
}
else {
r1 = md-1;
}
}
cnt1[l/i%2] += res-l+1;
cnt1[r/i%2] += r-res;
}
}
RightBlacks += cnt[0] * cnt1[0];
RightBlacks += cnt[1] * cnt1[1];
}
int a = (n/i)*(n/i) / 2 * i*i;
int b = a;
if((n/i) % 2) {
a += i*i;
}
ans = min(ans , a-RightBlacks + (blacks - RightBlacks));
ans = min(ans , b-(blacks - RightBlacks) + RightBlacks);
}
}
cout << ans;
}
/*
*/
main() {
ios;
int tt = 1;
//cin >> tt;
while(tt --) {
solve();
}
return 0;
}
Compilation message
chessboard.cpp: In function 'void solve()':
chessboard.cpp:74:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
74 | int md = l1+r1 >> 1;
| ~~^~~
chessboard.cpp:107:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
107 | int md = l1+r1 >> 1;
| ~~^~~
chessboard.cpp: At global scope:
chessboard.cpp:137:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
137 | main() {
| ^
chessboard.cpp: In function 'void solve()':
chessboard.cpp:84:22: warning: 'res' may be used uninitialized in this function [-Wmaybe-uninitialized]
84 | cnt[r/i%2] += r-res;
| ~^~~~
chessboard.cpp:117:23: warning: 'res' may be used uninitialized in this function [-Wmaybe-uninitialized]
117 | cnt1[r/i%2] += r-res;
| ~^~~~
# |
Verdict |
Execution time |
Memory |
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 |
1 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
2284 KB |
Output is correct |
2 |
Correct |
7 ms |
876 KB |
Output is correct |
3 |
Correct |
17 ms |
1644 KB |
Output is correct |
4 |
Correct |
21 ms |
1772 KB |
Output is correct |
5 |
Correct |
24 ms |
2028 KB |
Output is correct |
6 |
Correct |
15 ms |
1388 KB |
Output is correct |
7 |
Correct |
4 ms |
620 KB |
Output is correct |
8 |
Correct |
15 ms |
1516 KB |
Output is correct |
9 |
Correct |
37 ms |
3180 KB |
Output is correct |
10 |
Correct |
21 ms |
1900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
1 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 |
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 |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
1 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 |
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 |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
12 ms |
1260 KB |
Output is correct |
17 |
Correct |
30 ms |
3052 KB |
Output is correct |
18 |
Correct |
41 ms |
3436 KB |
Output is correct |
19 |
Correct |
112 ms |
3180 KB |
Output is correct |
20 |
Correct |
129 ms |
3564 KB |
Output is correct |
21 |
Correct |
28 ms |
2924 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
24 ms |
1644 KB |
Output is correct |
24 |
Correct |
38 ms |
3180 KB |
Output is correct |
25 |
Correct |
5 ms |
620 KB |
Output is correct |
26 |
Correct |
24 ms |
2156 KB |
Output is correct |
27 |
Correct |
33 ms |
2540 KB |
Output is correct |
28 |
Correct |
60 ms |
3308 KB |
Output is correct |
29 |
Correct |
12 ms |
1388 KB |
Output is correct |
30 |
Correct |
2 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
2284 KB |
Output is correct |
2 |
Correct |
7 ms |
876 KB |
Output is correct |
3 |
Correct |
17 ms |
1644 KB |
Output is correct |
4 |
Correct |
21 ms |
1772 KB |
Output is correct |
5 |
Correct |
24 ms |
2028 KB |
Output is correct |
6 |
Correct |
15 ms |
1388 KB |
Output is correct |
7 |
Correct |
4 ms |
620 KB |
Output is correct |
8 |
Correct |
15 ms |
1516 KB |
Output is correct |
9 |
Correct |
37 ms |
3180 KB |
Output is correct |
10 |
Correct |
21 ms |
1900 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 |
1 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 |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 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 |
492 KB |
Output is correct |
26 |
Correct |
12 ms |
1260 KB |
Output is correct |
27 |
Correct |
30 ms |
3052 KB |
Output is correct |
28 |
Correct |
41 ms |
3436 KB |
Output is correct |
29 |
Correct |
112 ms |
3180 KB |
Output is correct |
30 |
Correct |
129 ms |
3564 KB |
Output is correct |
31 |
Correct |
28 ms |
2924 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
24 ms |
1644 KB |
Output is correct |
34 |
Correct |
38 ms |
3180 KB |
Output is correct |
35 |
Correct |
5 ms |
620 KB |
Output is correct |
36 |
Correct |
24 ms |
2156 KB |
Output is correct |
37 |
Correct |
33 ms |
2540 KB |
Output is correct |
38 |
Correct |
60 ms |
3308 KB |
Output is correct |
39 |
Correct |
12 ms |
1388 KB |
Output is correct |
40 |
Correct |
2 ms |
492 KB |
Output is correct |
41 |
Correct |
105 ms |
2924 KB |
Output is correct |
42 |
Correct |
47 ms |
3308 KB |
Output is correct |
43 |
Correct |
67 ms |
2924 KB |
Output is correct |
44 |
Correct |
56 ms |
3180 KB |
Output is correct |
45 |
Correct |
41 ms |
3436 KB |
Output is correct |
46 |
Correct |
132 ms |
3308 KB |
Output is correct |
47 |
Correct |
45 ms |
3052 KB |
Output is correct |
48 |
Correct |
68 ms |
3052 KB |
Output is correct |
49 |
Correct |
42 ms |
3200 KB |
Output is correct |
50 |
Correct |
499 ms |
3260 KB |
Output is correct |
51 |
Correct |
522 ms |
3564 KB |
Output is correct |
52 |
Correct |
482 ms |
3308 KB |
Output is correct |
53 |
Correct |
533 ms |
3428 KB |
Output is correct |
54 |
Correct |
484 ms |
3200 KB |
Output is correct |
55 |
Correct |
552 ms |
3436 KB |
Output is correct |
56 |
Correct |
471 ms |
3116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
1 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 |
26 ms |
2284 KB |
Output is correct |
10 |
Correct |
7 ms |
876 KB |
Output is correct |
11 |
Correct |
17 ms |
1644 KB |
Output is correct |
12 |
Correct |
21 ms |
1772 KB |
Output is correct |
13 |
Correct |
24 ms |
2028 KB |
Output is correct |
14 |
Correct |
15 ms |
1388 KB |
Output is correct |
15 |
Correct |
4 ms |
620 KB |
Output is correct |
16 |
Correct |
15 ms |
1516 KB |
Output is correct |
17 |
Correct |
37 ms |
3180 KB |
Output is correct |
18 |
Correct |
21 ms |
1900 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 |
1 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 |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
492 KB |
Output is correct |
34 |
Correct |
12 ms |
1260 KB |
Output is correct |
35 |
Correct |
30 ms |
3052 KB |
Output is correct |
36 |
Correct |
41 ms |
3436 KB |
Output is correct |
37 |
Correct |
112 ms |
3180 KB |
Output is correct |
38 |
Correct |
129 ms |
3564 KB |
Output is correct |
39 |
Correct |
28 ms |
2924 KB |
Output is correct |
40 |
Correct |
1 ms |
364 KB |
Output is correct |
41 |
Correct |
24 ms |
1644 KB |
Output is correct |
42 |
Correct |
38 ms |
3180 KB |
Output is correct |
43 |
Correct |
5 ms |
620 KB |
Output is correct |
44 |
Correct |
24 ms |
2156 KB |
Output is correct |
45 |
Correct |
33 ms |
2540 KB |
Output is correct |
46 |
Correct |
60 ms |
3308 KB |
Output is correct |
47 |
Correct |
12 ms |
1388 KB |
Output is correct |
48 |
Correct |
2 ms |
492 KB |
Output is correct |
49 |
Correct |
105 ms |
2924 KB |
Output is correct |
50 |
Correct |
47 ms |
3308 KB |
Output is correct |
51 |
Correct |
67 ms |
2924 KB |
Output is correct |
52 |
Correct |
56 ms |
3180 KB |
Output is correct |
53 |
Correct |
41 ms |
3436 KB |
Output is correct |
54 |
Correct |
132 ms |
3308 KB |
Output is correct |
55 |
Correct |
45 ms |
3052 KB |
Output is correct |
56 |
Correct |
68 ms |
3052 KB |
Output is correct |
57 |
Correct |
42 ms |
3200 KB |
Output is correct |
58 |
Correct |
499 ms |
3260 KB |
Output is correct |
59 |
Correct |
522 ms |
3564 KB |
Output is correct |
60 |
Correct |
482 ms |
3308 KB |
Output is correct |
61 |
Correct |
533 ms |
3428 KB |
Output is correct |
62 |
Correct |
484 ms |
3200 KB |
Output is correct |
63 |
Correct |
552 ms |
3436 KB |
Output is correct |
64 |
Correct |
471 ms |
3116 KB |
Output is correct |
65 |
Correct |
1 ms |
364 KB |
Output is correct |
66 |
Runtime error |
2 ms |
620 KB |
Execution killed with signal 6 |
67 |
Halted |
0 ms |
0 KB |
- |