#include <bits/stdc++.h>
#define eb emplace_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define svec(x) sort(x.begin(), x.end())
#define press(x) x.erase(unique(x.begin(), x.end()), x.end())
#define lb(x, v) lower_bound(x.begin(), x.end(), v)
#define ub(x, v) upper_bound(x.begin(), x.end(), v)
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef pair<int, LL> pil;
typedef pair<LL, int> pli;
const LL llinf=2000000000000000000;
const LL mod1=1000000007;
const LL mod2=998244353;
const int inf=2000000000;
int n, k;
pair<pii, pii> rect[100010];
LL ans=llinf;
LL query2(int x, int y, int s){
LL ret=0;
int tx=x/s, ty=y/s;
ret+=(LL)tx*ty/2*s*s;
int rx=x%s, ry=y%s;
if(tx%2)ret+=(LL)rx*((ty+1)/2)*s;
else ret+=(LL)rx*(ty/2)*s;
if(ty%2)ret+=(LL)ry*((tx+1)/2)*s;
else ret+=(LL)ry*(tx/2)*s;
if((tx+ty)%2)ret+=(LL)rx*ry;
return ret;
}
pll query(int stx, int sty, int finx, int finy, int s){
LL b=query2(finx, finy, s)-query2(stx-1, finy, s)-query2(finx, sty-1, s)+query2(stx-1, sty-1, s);
LL w=(LL)(finx-stx+1)*(finy-sty+1)-b;
//if(((stx-1)/s+(sty-1)/s)%2)return mp(1, 0);
return mp(b, w);
}
int main(){
scanf("%d %d", &n, &k);
for(int i=1; i<=k; i++){
scanf("%d %d %d %d", &rect[i].F.F, &rect[i].F.S, &rect[i].S.F, &rect[i].S.S);
}
for(int i=1; i<n; i++){
if(n%i)continue;
LL w=0, b=0;
for(int j=1; j<=k; j++){
pll tmp=query(rect[j].F.F, rect[j].F.S, rect[j].S.F, rect[j].S.S, i);
b+=tmp.F;
w+=tmp.S;
}
LL temp1=w+(LL)(n/i)*(n/i)/2*i*i-b;
LL temp2=b+((LL)(n/i)*(n/i)+1)/2*i*i-w;
ans=min(ans, min(temp1, temp2));
}
printf("%lld", ans);
}
/*
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:43:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &k);
~~~~~^~~~~~~~~~~~~~~~~
chessboard.cpp:45:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d %d", &rect[i].F.F, &rect[i].F.S, &rect[i].S.F, &rect[i].S.S);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
256 KB |
Output is correct |
2 |
Correct |
4 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
256 KB |
Output is correct |
5 |
Correct |
5 ms |
256 KB |
Output is correct |
6 |
Correct |
4 ms |
256 KB |
Output is correct |
7 |
Correct |
4 ms |
128 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
1256 KB |
Output is correct |
2 |
Correct |
13 ms |
640 KB |
Output is correct |
3 |
Correct |
26 ms |
896 KB |
Output is correct |
4 |
Correct |
26 ms |
1016 KB |
Output is correct |
5 |
Correct |
36 ms |
1152 KB |
Output is correct |
6 |
Correct |
22 ms |
896 KB |
Output is correct |
7 |
Correct |
9 ms |
384 KB |
Output is correct |
8 |
Correct |
23 ms |
872 KB |
Output is correct |
9 |
Correct |
51 ms |
1656 KB |
Output is correct |
10 |
Correct |
32 ms |
1152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
4 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
256 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
6 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
256 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
256 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
4 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
256 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
6 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
256 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
256 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
16 |
Correct |
20 ms |
768 KB |
Output is correct |
17 |
Correct |
42 ms |
1920 KB |
Output is correct |
18 |
Correct |
59 ms |
1912 KB |
Output is correct |
19 |
Correct |
171 ms |
1748 KB |
Output is correct |
20 |
Correct |
192 ms |
1912 KB |
Output is correct |
21 |
Correct |
39 ms |
1528 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
37 ms |
1024 KB |
Output is correct |
24 |
Correct |
54 ms |
1656 KB |
Output is correct |
25 |
Correct |
11 ms |
512 KB |
Output is correct |
26 |
Correct |
35 ms |
1152 KB |
Output is correct |
27 |
Correct |
49 ms |
1312 KB |
Output is correct |
28 |
Correct |
57 ms |
1792 KB |
Output is correct |
29 |
Correct |
20 ms |
896 KB |
Output is correct |
30 |
Correct |
6 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
1256 KB |
Output is correct |
2 |
Correct |
13 ms |
640 KB |
Output is correct |
3 |
Correct |
26 ms |
896 KB |
Output is correct |
4 |
Correct |
26 ms |
1016 KB |
Output is correct |
5 |
Correct |
36 ms |
1152 KB |
Output is correct |
6 |
Correct |
22 ms |
896 KB |
Output is correct |
7 |
Correct |
9 ms |
384 KB |
Output is correct |
8 |
Correct |
23 ms |
872 KB |
Output is correct |
9 |
Correct |
51 ms |
1656 KB |
Output is correct |
10 |
Correct |
32 ms |
1152 KB |
Output is correct |
11 |
Correct |
5 ms |
256 KB |
Output is correct |
12 |
Correct |
4 ms |
256 KB |
Output is correct |
13 |
Correct |
5 ms |
256 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
6 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
256 KB |
Output is correct |
20 |
Correct |
4 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
256 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
512 KB |
Output is correct |
26 |
Correct |
20 ms |
768 KB |
Output is correct |
27 |
Correct |
42 ms |
1920 KB |
Output is correct |
28 |
Correct |
59 ms |
1912 KB |
Output is correct |
29 |
Correct |
171 ms |
1748 KB |
Output is correct |
30 |
Correct |
192 ms |
1912 KB |
Output is correct |
31 |
Correct |
39 ms |
1528 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
37 ms |
1024 KB |
Output is correct |
34 |
Correct |
54 ms |
1656 KB |
Output is correct |
35 |
Correct |
11 ms |
512 KB |
Output is correct |
36 |
Correct |
35 ms |
1152 KB |
Output is correct |
37 |
Correct |
49 ms |
1312 KB |
Output is correct |
38 |
Correct |
57 ms |
1792 KB |
Output is correct |
39 |
Correct |
20 ms |
896 KB |
Output is correct |
40 |
Correct |
6 ms |
384 KB |
Output is correct |
41 |
Correct |
157 ms |
1656 KB |
Output is correct |
42 |
Correct |
68 ms |
1756 KB |
Output is correct |
43 |
Correct |
105 ms |
1660 KB |
Output is correct |
44 |
Correct |
66 ms |
1784 KB |
Output is correct |
45 |
Correct |
58 ms |
1784 KB |
Output is correct |
46 |
Correct |
178 ms |
1784 KB |
Output is correct |
47 |
Correct |
50 ms |
1656 KB |
Output is correct |
48 |
Correct |
83 ms |
1656 KB |
Output is correct |
49 |
Correct |
59 ms |
1656 KB |
Output is correct |
50 |
Correct |
730 ms |
1784 KB |
Output is correct |
51 |
Correct |
777 ms |
1912 KB |
Output is correct |
52 |
Correct |
724 ms |
1912 KB |
Output is correct |
53 |
Correct |
776 ms |
1864 KB |
Output is correct |
54 |
Correct |
721 ms |
1784 KB |
Output is correct |
55 |
Correct |
806 ms |
2040 KB |
Output is correct |
56 |
Correct |
696 ms |
1784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
256 KB |
Output is correct |
2 |
Correct |
4 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
256 KB |
Output is correct |
5 |
Correct |
5 ms |
256 KB |
Output is correct |
6 |
Correct |
4 ms |
256 KB |
Output is correct |
7 |
Correct |
4 ms |
128 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
37 ms |
1256 KB |
Output is correct |
10 |
Correct |
13 ms |
640 KB |
Output is correct |
11 |
Correct |
26 ms |
896 KB |
Output is correct |
12 |
Correct |
26 ms |
1016 KB |
Output is correct |
13 |
Correct |
36 ms |
1152 KB |
Output is correct |
14 |
Correct |
22 ms |
896 KB |
Output is correct |
15 |
Correct |
9 ms |
384 KB |
Output is correct |
16 |
Correct |
23 ms |
872 KB |
Output is correct |
17 |
Correct |
51 ms |
1656 KB |
Output is correct |
18 |
Correct |
32 ms |
1152 KB |
Output is correct |
19 |
Correct |
5 ms |
256 KB |
Output is correct |
20 |
Correct |
4 ms |
256 KB |
Output is correct |
21 |
Correct |
5 ms |
256 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
6 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
256 KB |
Output is correct |
28 |
Correct |
4 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
256 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
5 ms |
512 KB |
Output is correct |
34 |
Correct |
20 ms |
768 KB |
Output is correct |
35 |
Correct |
42 ms |
1920 KB |
Output is correct |
36 |
Correct |
59 ms |
1912 KB |
Output is correct |
37 |
Correct |
171 ms |
1748 KB |
Output is correct |
38 |
Correct |
192 ms |
1912 KB |
Output is correct |
39 |
Correct |
39 ms |
1528 KB |
Output is correct |
40 |
Correct |
5 ms |
384 KB |
Output is correct |
41 |
Correct |
37 ms |
1024 KB |
Output is correct |
42 |
Correct |
54 ms |
1656 KB |
Output is correct |
43 |
Correct |
11 ms |
512 KB |
Output is correct |
44 |
Correct |
35 ms |
1152 KB |
Output is correct |
45 |
Correct |
49 ms |
1312 KB |
Output is correct |
46 |
Correct |
57 ms |
1792 KB |
Output is correct |
47 |
Correct |
20 ms |
896 KB |
Output is correct |
48 |
Correct |
6 ms |
384 KB |
Output is correct |
49 |
Correct |
157 ms |
1656 KB |
Output is correct |
50 |
Correct |
68 ms |
1756 KB |
Output is correct |
51 |
Correct |
105 ms |
1660 KB |
Output is correct |
52 |
Correct |
66 ms |
1784 KB |
Output is correct |
53 |
Correct |
58 ms |
1784 KB |
Output is correct |
54 |
Correct |
178 ms |
1784 KB |
Output is correct |
55 |
Correct |
50 ms |
1656 KB |
Output is correct |
56 |
Correct |
83 ms |
1656 KB |
Output is correct |
57 |
Correct |
59 ms |
1656 KB |
Output is correct |
58 |
Correct |
730 ms |
1784 KB |
Output is correct |
59 |
Correct |
777 ms |
1912 KB |
Output is correct |
60 |
Correct |
724 ms |
1912 KB |
Output is correct |
61 |
Correct |
776 ms |
1864 KB |
Output is correct |
62 |
Correct |
721 ms |
1784 KB |
Output is correct |
63 |
Correct |
806 ms |
2040 KB |
Output is correct |
64 |
Correct |
696 ms |
1784 KB |
Output is correct |
65 |
Correct |
4 ms |
256 KB |
Output is correct |
66 |
Correct |
5 ms |
256 KB |
Output is correct |
67 |
Correct |
763 ms |
3960 KB |
Output is correct |
68 |
Correct |
760 ms |
4052 KB |
Output is correct |
69 |
Correct |
668 ms |
3604 KB |
Output is correct |
70 |
Correct |
741 ms |
4088 KB |
Output is correct |
71 |
Correct |
740 ms |
3896 KB |
Output is correct |
72 |
Correct |
717 ms |
3832 KB |
Output is correct |
73 |
Correct |
705 ms |
3832 KB |
Output is correct |
74 |
Correct |
754 ms |
3960 KB |
Output is correct |
75 |
Correct |
729 ms |
3960 KB |
Output is correct |
76 |
Correct |
770 ms |
4220 KB |
Output is correct |
77 |
Correct |
143 ms |
4344 KB |
Output is correct |
78 |
Correct |
67 ms |
3832 KB |
Output is correct |
79 |
Correct |
104 ms |
3704 KB |
Output is correct |
80 |
Correct |
108 ms |
3832 KB |
Output is correct |
81 |
Correct |
100 ms |
3576 KB |
Output is correct |
82 |
Correct |
91 ms |
3960 KB |
Output is correct |
83 |
Correct |
75 ms |
3704 KB |
Output is correct |
84 |
Correct |
465 ms |
4216 KB |
Output is correct |
85 |
Correct |
809 ms |
4344 KB |
Output is correct |
86 |
Correct |
6 ms |
384 KB |
Output is correct |
87 |
Correct |
6 ms |
256 KB |
Output is correct |
88 |
Correct |
823 ms |
4344 KB |
Output is correct |
89 |
Correct |
160 ms |
1024 KB |
Output is correct |
90 |
Correct |
5 ms |
256 KB |
Output is correct |