#include <bits/stdc++.h>
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define pb push_back
#define FIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
const int N = 1e5 + 7;
ll n, m;
pair <pii, pii> re[N];
int main () {
FIO;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int x, y, x2, y2; cin >> x >> y >> x2 >> y2; x--; y--; x2--; y2--;
re[i] = {{x, y}, {x2, y2}};
}
ll mn = 1e18;
for (ll i = 1; i < n; i++) {
if (n % i) continue;
ll cnt[2] = {0};
for (int j = 0; j < m; j++) {
ll a = re[j].F.F, b = re[j].F.S, c = re[j].S.F, d = re[j].S.S;
ll aa = a/i*i+i, bb = b/i*i+i, cc = c/i*i-1, dd = d/i*i-1;
ll ro = (cc-aa+1)/i, co = (dd-bb+1)/i;
int o = (aa/i+bb/i)%2;
if (ro < 0 && co < 0) {
cnt[o] += (c-a+1)*(d-b+1);
continue;
}
if (ro < 0) {
ro = c-a+1;
cnt[o] += ro*(bb-b);
cnt[(o+co+1)&1] += ro*(d-dd);
cnt[0] += ro*(co/2+(co&1&&o==1))*i;
cnt[1] += ro*(co/2+(co&1&&o==0))*i;
continue;
}
if (co < 0) {
co = d-b+1;
cnt[o] += co*(aa-a);
cnt[(o+ro+1)&1] += co*(c-cc);
cnt[0] += co*(ro/2+(ro&1&&o==1))*i;
cnt[1] += co*(ro/2+(ro&1&&o==0))*i;
continue;
}
cnt[0] += ro*co/2*i*i; cnt[1] += ro*co/2*i*i;
if (ro*co&1) cnt[o] += i*i;
cnt[0] += (ro/2+(o&&ro&1))*(bb-b)*i + (ro/2+((o+co)%2==0&&ro&1))*(d-dd)*i;
cnt[1] += (ro/2+(o^1&&ro&1))*(bb-b)*i + (ro/2+((o+co)%2==1&&ro&1))*(d-dd)*i;
cnt[0] += (co/2+(o&&co&1))*(aa-a)*i + (co/2+((o+ro)%2==0&&co&1))*(c-cc)*i;
cnt[1] += (co/2+(o^1&&co&1))*(aa-a)*i + (co/2+((o+ro)%2==1&&co&1))*(c-cc)*i;
cnt[o] += (aa-a)*(bb-b);
cnt[(o+co-1)&1] += (aa-a)*(d-dd);
cnt[(o+ro-1)&1] += (c-cc)*(bb-b);
cnt[(o+ro+co)&1] += (c-cc)*(d-dd);
}
ll dod = (n/i&1)*i*i;
ll cost = min((n*n-dod)/2+dod - cnt[0] + cnt[1], (n*n-dod)/2 - cnt[1] + cnt[0]);
mn = min(mn, cost);
}
cout << mn;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
464 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
2904 KB |
Output is correct |
2 |
Correct |
4 ms |
860 KB |
Output is correct |
3 |
Correct |
9 ms |
1868 KB |
Output is correct |
4 |
Correct |
10 ms |
1872 KB |
Output is correct |
5 |
Correct |
16 ms |
2440 KB |
Output is correct |
6 |
Correct |
8 ms |
1628 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
8 ms |
1608 KB |
Output is correct |
9 |
Correct |
20 ms |
3668 KB |
Output is correct |
10 |
Correct |
11 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
464 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
464 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
7 ms |
1336 KB |
Output is correct |
17 |
Correct |
15 ms |
2908 KB |
Output is correct |
18 |
Correct |
54 ms |
3412 KB |
Output is correct |
19 |
Correct |
47 ms |
3248 KB |
Output is correct |
20 |
Correct |
53 ms |
3572 KB |
Output is correct |
21 |
Correct |
15 ms |
2908 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
11 ms |
1764 KB |
Output is correct |
24 |
Correct |
19 ms |
3268 KB |
Output is correct |
25 |
Correct |
4 ms |
604 KB |
Output is correct |
26 |
Correct |
12 ms |
2040 KB |
Output is correct |
27 |
Correct |
16 ms |
2396 KB |
Output is correct |
28 |
Correct |
25 ms |
3420 KB |
Output is correct |
29 |
Correct |
6 ms |
1368 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
2904 KB |
Output is correct |
2 |
Correct |
4 ms |
860 KB |
Output is correct |
3 |
Correct |
9 ms |
1868 KB |
Output is correct |
4 |
Correct |
10 ms |
1872 KB |
Output is correct |
5 |
Correct |
16 ms |
2440 KB |
Output is correct |
6 |
Correct |
8 ms |
1628 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
8 ms |
1608 KB |
Output is correct |
9 |
Correct |
20 ms |
3668 KB |
Output is correct |
10 |
Correct |
11 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
364 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
7 ms |
1336 KB |
Output is correct |
27 |
Correct |
15 ms |
2908 KB |
Output is correct |
28 |
Correct |
54 ms |
3412 KB |
Output is correct |
29 |
Correct |
47 ms |
3248 KB |
Output is correct |
30 |
Correct |
53 ms |
3572 KB |
Output is correct |
31 |
Correct |
15 ms |
2908 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
11 ms |
1764 KB |
Output is correct |
34 |
Correct |
19 ms |
3268 KB |
Output is correct |
35 |
Correct |
4 ms |
604 KB |
Output is correct |
36 |
Correct |
12 ms |
2040 KB |
Output is correct |
37 |
Correct |
16 ms |
2396 KB |
Output is correct |
38 |
Correct |
25 ms |
3420 KB |
Output is correct |
39 |
Correct |
6 ms |
1368 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
44 ms |
3676 KB |
Output is correct |
42 |
Correct |
32 ms |
3920 KB |
Output is correct |
43 |
Correct |
30 ms |
3736 KB |
Output is correct |
44 |
Correct |
23 ms |
3924 KB |
Output is correct |
45 |
Correct |
21 ms |
4176 KB |
Output is correct |
46 |
Correct |
49 ms |
3932 KB |
Output is correct |
47 |
Correct |
19 ms |
3676 KB |
Output is correct |
48 |
Correct |
26 ms |
3916 KB |
Output is correct |
49 |
Correct |
21 ms |
3672 KB |
Output is correct |
50 |
Correct |
173 ms |
3972 KB |
Output is correct |
51 |
Correct |
179 ms |
4116 KB |
Output is correct |
52 |
Correct |
161 ms |
3932 KB |
Output is correct |
53 |
Correct |
166 ms |
4184 KB |
Output is correct |
54 |
Correct |
152 ms |
3928 KB |
Output is correct |
55 |
Correct |
187 ms |
4160 KB |
Output is correct |
56 |
Correct |
167 ms |
3848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
464 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
14 ms |
2904 KB |
Output is correct |
10 |
Correct |
4 ms |
860 KB |
Output is correct |
11 |
Correct |
9 ms |
1868 KB |
Output is correct |
12 |
Correct |
10 ms |
1872 KB |
Output is correct |
13 |
Correct |
16 ms |
2440 KB |
Output is correct |
14 |
Correct |
8 ms |
1628 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
8 ms |
1608 KB |
Output is correct |
17 |
Correct |
20 ms |
3668 KB |
Output is correct |
18 |
Correct |
11 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
464 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
468 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
364 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
7 ms |
1336 KB |
Output is correct |
35 |
Correct |
15 ms |
2908 KB |
Output is correct |
36 |
Correct |
54 ms |
3412 KB |
Output is correct |
37 |
Correct |
47 ms |
3248 KB |
Output is correct |
38 |
Correct |
53 ms |
3572 KB |
Output is correct |
39 |
Correct |
15 ms |
2908 KB |
Output is correct |
40 |
Correct |
1 ms |
344 KB |
Output is correct |
41 |
Correct |
11 ms |
1764 KB |
Output is correct |
42 |
Correct |
19 ms |
3268 KB |
Output is correct |
43 |
Correct |
4 ms |
604 KB |
Output is correct |
44 |
Correct |
12 ms |
2040 KB |
Output is correct |
45 |
Correct |
16 ms |
2396 KB |
Output is correct |
46 |
Correct |
25 ms |
3420 KB |
Output is correct |
47 |
Correct |
6 ms |
1368 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
44 ms |
3676 KB |
Output is correct |
50 |
Correct |
32 ms |
3920 KB |
Output is correct |
51 |
Correct |
30 ms |
3736 KB |
Output is correct |
52 |
Correct |
23 ms |
3924 KB |
Output is correct |
53 |
Correct |
21 ms |
4176 KB |
Output is correct |
54 |
Correct |
49 ms |
3932 KB |
Output is correct |
55 |
Correct |
19 ms |
3676 KB |
Output is correct |
56 |
Correct |
26 ms |
3916 KB |
Output is correct |
57 |
Correct |
21 ms |
3672 KB |
Output is correct |
58 |
Correct |
173 ms |
3972 KB |
Output is correct |
59 |
Correct |
179 ms |
4116 KB |
Output is correct |
60 |
Correct |
161 ms |
3932 KB |
Output is correct |
61 |
Correct |
166 ms |
4184 KB |
Output is correct |
62 |
Correct |
152 ms |
3928 KB |
Output is correct |
63 |
Correct |
187 ms |
4160 KB |
Output is correct |
64 |
Correct |
167 ms |
3848 KB |
Output is correct |
65 |
Correct |
0 ms |
348 KB |
Output is correct |
66 |
Correct |
0 ms |
348 KB |
Output is correct |
67 |
Correct |
230 ms |
4356 KB |
Output is correct |
68 |
Correct |
227 ms |
4120 KB |
Output is correct |
69 |
Correct |
186 ms |
3672 KB |
Output is correct |
70 |
Correct |
202 ms |
3932 KB |
Output is correct |
71 |
Correct |
202 ms |
3932 KB |
Output is correct |
72 |
Correct |
186 ms |
3672 KB |
Output is correct |
73 |
Correct |
202 ms |
3676 KB |
Output is correct |
74 |
Correct |
220 ms |
3924 KB |
Output is correct |
75 |
Correct |
192 ms |
3932 KB |
Output is correct |
76 |
Correct |
209 ms |
4188 KB |
Output is correct |
77 |
Correct |
46 ms |
4188 KB |
Output is correct |
78 |
Correct |
24 ms |
3932 KB |
Output is correct |
79 |
Correct |
35 ms |
3672 KB |
Output is correct |
80 |
Correct |
44 ms |
3872 KB |
Output is correct |
81 |
Correct |
35 ms |
3664 KB |
Output is correct |
82 |
Correct |
40 ms |
3928 KB |
Output is correct |
83 |
Correct |
27 ms |
3672 KB |
Output is correct |
84 |
Correct |
135 ms |
4256 KB |
Output is correct |
85 |
Correct |
236 ms |
4308 KB |
Output is correct |
86 |
Correct |
1 ms |
348 KB |
Output is correct |
87 |
Correct |
0 ms |
348 KB |
Output is correct |
88 |
Correct |
231 ms |
4392 KB |
Output is correct |
89 |
Correct |
53 ms |
1116 KB |
Output is correct |
90 |
Correct |
0 ms |
348 KB |
Output is correct |