#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
#define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
#define mod 998244353
#define xx firsts
#define yy second
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define ll long long
#define pii pair<int,int>
#define maxn 200005
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>,rb_tree_tag, tree_order_statistics_node_update> ordered_set;/// find_by_order(x)(x+1th) , order_of_key() (strictly less)
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
int n;
int m;
struct kv{
ll x1;
ll y1;
ll x2;
ll y2;
}niz[maxn];
ll get(ll l, ll r, ll broj){
if(l > r)return 0;
ll prvi = (l/broj)*broj;
if(l != prvi)prvi += broj;
ll last = (r/broj)*broj;
if(prvi > r){
if((l%broj) < broj/2){
return min((broj/2)-1, (r%broj))-(l%broj)+1;
}
return 0;
}
ll klk = last/broj - prvi/broj;
return (klk)*(broj/2) + get(l,prvi-1,broj) + get(last+1,r,broj)+1;
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> m;
ff(i,1,m){
int a,b,c,d;
cin >> a >> b >> c >> d;
a--; b--; c--; d--;
niz[i] = {a,b,c,d};
}
ll ans = 1e9;
ans *= 10;
ff(i,1,n - 1){
if(n%i != 0)continue;
ll b = 0;
ll w = 0;
ll tr = 2*i;
ff(j,1,m){
ll uk = 0;
ll blackrow = get(niz[j].x1,niz[j].x2,tr);
ll blackcol = get(niz[j].y1,niz[j].y2,tr);
uk += (blackrow*blackcol);
blackrow = niz[j].x2 - niz[j].x1 + 1 - blackrow;
blackcol = niz[j].y2 - niz[j].y1 + 1 - blackcol;
uk += (blackrow*blackcol);
b += uk;
w += ((niz[j].x2 - niz[j].x1 + 1) * (niz[j].y2 - niz[j].y1 + 1) - uk);
}
ll str = (n/i);
ll p1 = (str*str + 1) / 2;
ll p2 = (str*str) / 2;
ll pomm = i;
p1 *= (pomm*pomm);
p2 *= (pomm*pomm);
ll tmp = min(p1 - b + w, p2 - w + b);
//cout << i << " " << tmp << "\n";
ans = min(ans, tmp);
}
cout << ans << "\n";
return 0;
}
/*
6 8
3 3 3 3
1 2 1 2
3 4 3 4
5 5 5 5
4 3 4 3
4 4 4 4
2 1 2 1
3 6 3 6
*/
Compilation message
chessboard.cpp: In function 'int main()':
chessboard.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
| ^
chessboard.cpp:55:5: note: in expansion of macro 'ff'
55 | ff(i,1,m){
| ^~
chessboard.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
| ^
chessboard.cpp:63:5: note: in expansion of macro 'ff'
63 | ff(i,1,n - 1){
| ^~
chessboard.cpp:6:27: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
| ^
chessboard.cpp:68:9: note: in expansion of macro 'ff'
68 | ff(j,1,m){
| ^~
# |
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 |
0 ms |
364 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 |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
2284 KB |
Output is correct |
2 |
Correct |
8 ms |
876 KB |
Output is correct |
3 |
Correct |
17 ms |
1644 KB |
Output is correct |
4 |
Correct |
19 ms |
1844 KB |
Output is correct |
5 |
Correct |
23 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 |
1388 KB |
Output is correct |
9 |
Correct |
38 ms |
3180 KB |
Output is correct |
10 |
Correct |
22 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 |
2 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 |
512 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 |
# |
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 |
2 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 |
512 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 |
16 ms |
1260 KB |
Output is correct |
17 |
Correct |
31 ms |
2928 KB |
Output is correct |
18 |
Correct |
42 ms |
3436 KB |
Output is correct |
19 |
Correct |
111 ms |
3180 KB |
Output is correct |
20 |
Correct |
123 ms |
3564 KB |
Output is correct |
21 |
Correct |
33 ms |
2776 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
25 ms |
1644 KB |
Output is correct |
24 |
Correct |
39 ms |
3180 KB |
Output is correct |
25 |
Correct |
5 ms |
620 KB |
Output is correct |
26 |
Correct |
25 ms |
2156 KB |
Output is correct |
27 |
Correct |
35 ms |
2412 KB |
Output is correct |
28 |
Correct |
52 ms |
3308 KB |
Output is correct |
29 |
Correct |
13 ms |
1408 KB |
Output is correct |
30 |
Correct |
2 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
2284 KB |
Output is correct |
2 |
Correct |
8 ms |
876 KB |
Output is correct |
3 |
Correct |
17 ms |
1644 KB |
Output is correct |
4 |
Correct |
19 ms |
1844 KB |
Output is correct |
5 |
Correct |
23 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 |
1388 KB |
Output is correct |
9 |
Correct |
38 ms |
3180 KB |
Output is correct |
10 |
Correct |
22 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 |
2 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 |
512 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 |
16 ms |
1260 KB |
Output is correct |
27 |
Correct |
31 ms |
2928 KB |
Output is correct |
28 |
Correct |
42 ms |
3436 KB |
Output is correct |
29 |
Correct |
111 ms |
3180 KB |
Output is correct |
30 |
Correct |
123 ms |
3564 KB |
Output is correct |
31 |
Correct |
33 ms |
2776 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
25 ms |
1644 KB |
Output is correct |
34 |
Correct |
39 ms |
3180 KB |
Output is correct |
35 |
Correct |
5 ms |
620 KB |
Output is correct |
36 |
Correct |
25 ms |
2156 KB |
Output is correct |
37 |
Correct |
35 ms |
2412 KB |
Output is correct |
38 |
Correct |
52 ms |
3308 KB |
Output is correct |
39 |
Correct |
13 ms |
1408 KB |
Output is correct |
40 |
Correct |
2 ms |
492 KB |
Output is correct |
41 |
Correct |
102 ms |
2924 KB |
Output is correct |
42 |
Correct |
59 ms |
5612 KB |
Output is correct |
43 |
Correct |
75 ms |
5120 KB |
Output is correct |
44 |
Correct |
49 ms |
5484 KB |
Output is correct |
45 |
Correct |
42 ms |
5740 KB |
Output is correct |
46 |
Correct |
115 ms |
5484 KB |
Output is correct |
47 |
Correct |
38 ms |
5228 KB |
Output is correct |
48 |
Correct |
57 ms |
5100 KB |
Output is correct |
49 |
Correct |
43 ms |
4972 KB |
Output is correct |
50 |
Correct |
458 ms |
5484 KB |
Output is correct |
51 |
Correct |
497 ms |
5752 KB |
Output is correct |
52 |
Correct |
449 ms |
5356 KB |
Output is correct |
53 |
Correct |
493 ms |
5716 KB |
Output is correct |
54 |
Correct |
471 ms |
5228 KB |
Output is correct |
55 |
Correct |
503 ms |
5996 KB |
Output is correct |
56 |
Correct |
429 ms |
5100 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 |
0 ms |
364 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 |
364 KB |
Output is correct |
9 |
Correct |
27 ms |
2284 KB |
Output is correct |
10 |
Correct |
8 ms |
876 KB |
Output is correct |
11 |
Correct |
17 ms |
1644 KB |
Output is correct |
12 |
Correct |
19 ms |
1844 KB |
Output is correct |
13 |
Correct |
23 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 |
1388 KB |
Output is correct |
17 |
Correct |
38 ms |
3180 KB |
Output is correct |
18 |
Correct |
22 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 |
2 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 |
512 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 |
364 KB |
Output is correct |
34 |
Correct |
16 ms |
1260 KB |
Output is correct |
35 |
Correct |
31 ms |
2928 KB |
Output is correct |
36 |
Correct |
42 ms |
3436 KB |
Output is correct |
37 |
Correct |
111 ms |
3180 KB |
Output is correct |
38 |
Correct |
123 ms |
3564 KB |
Output is correct |
39 |
Correct |
33 ms |
2776 KB |
Output is correct |
40 |
Correct |
1 ms |
364 KB |
Output is correct |
41 |
Correct |
25 ms |
1644 KB |
Output is correct |
42 |
Correct |
39 ms |
3180 KB |
Output is correct |
43 |
Correct |
5 ms |
620 KB |
Output is correct |
44 |
Correct |
25 ms |
2156 KB |
Output is correct |
45 |
Correct |
35 ms |
2412 KB |
Output is correct |
46 |
Correct |
52 ms |
3308 KB |
Output is correct |
47 |
Correct |
13 ms |
1408 KB |
Output is correct |
48 |
Correct |
2 ms |
492 KB |
Output is correct |
49 |
Correct |
102 ms |
2924 KB |
Output is correct |
50 |
Correct |
59 ms |
5612 KB |
Output is correct |
51 |
Correct |
75 ms |
5120 KB |
Output is correct |
52 |
Correct |
49 ms |
5484 KB |
Output is correct |
53 |
Correct |
42 ms |
5740 KB |
Output is correct |
54 |
Correct |
115 ms |
5484 KB |
Output is correct |
55 |
Correct |
38 ms |
5228 KB |
Output is correct |
56 |
Correct |
57 ms |
5100 KB |
Output is correct |
57 |
Correct |
43 ms |
4972 KB |
Output is correct |
58 |
Correct |
458 ms |
5484 KB |
Output is correct |
59 |
Correct |
497 ms |
5752 KB |
Output is correct |
60 |
Correct |
449 ms |
5356 KB |
Output is correct |
61 |
Correct |
493 ms |
5716 KB |
Output is correct |
62 |
Correct |
471 ms |
5228 KB |
Output is correct |
63 |
Correct |
503 ms |
5996 KB |
Output is correct |
64 |
Correct |
429 ms |
5100 KB |
Output is correct |
65 |
Correct |
1 ms |
364 KB |
Output is correct |
66 |
Correct |
1 ms |
364 KB |
Output is correct |
67 |
Correct |
574 ms |
5572 KB |
Output is correct |
68 |
Correct |
553 ms |
5484 KB |
Output is correct |
69 |
Correct |
456 ms |
4972 KB |
Output is correct |
70 |
Correct |
494 ms |
5484 KB |
Output is correct |
71 |
Correct |
511 ms |
5332 KB |
Output is correct |
72 |
Correct |
483 ms |
5228 KB |
Output is correct |
73 |
Correct |
483 ms |
5100 KB |
Output is correct |
74 |
Correct |
524 ms |
5612 KB |
Output is correct |
75 |
Correct |
499 ms |
5292 KB |
Output is correct |
76 |
Correct |
529 ms |
5612 KB |
Output is correct |
77 |
Correct |
106 ms |
5996 KB |
Output is correct |
78 |
Correct |
49 ms |
5288 KB |
Output is correct |
79 |
Correct |
76 ms |
4972 KB |
Output is correct |
80 |
Correct |
80 ms |
5356 KB |
Output is correct |
81 |
Correct |
76 ms |
4844 KB |
Output is correct |
82 |
Correct |
81 ms |
5484 KB |
Output is correct |
83 |
Correct |
57 ms |
5100 KB |
Output is correct |
84 |
Correct |
325 ms |
5868 KB |
Output is correct |
85 |
Correct |
554 ms |
5868 KB |
Output is correct |
86 |
Correct |
2 ms |
364 KB |
Output is correct |
87 |
Correct |
1 ms |
364 KB |
Output is correct |
88 |
Correct |
581 ms |
5996 KB |
Output is correct |
89 |
Correct |
111 ms |
1388 KB |
Output is correct |
90 |
Correct |
1 ms |
364 KB |
Output is correct |