#include <bits/stdc++.h>
using namespace std;
#define int long long
int isprime(int n) {
if (n == 2) return false;
for (int i = 2; i < n; i++) {
if (n % i == 0) return false;
}
return true;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
vector <int> x1(k), x2(k), y1(k), y2(k);
for (int i = 0; i < k; i++) {
cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];
}
// subtest1
if (k == 0) {
int ans = -1;
for (int i = 1; i < n; i++) {
if (n % i == 0) {
int res = ((n * n) / (i * i)) / 2 * (i * i);
if (ans == -1 || ans > res)
ans = res;
}
}
cout << ans << '\n';
return 0;
}
/// subtest2
bool ok = true;
for (int i = 0; i < k; i++) {
if (x1[i] != x2[i] || y1[i] != y2[i])
ok = false;
}
if (ok && isprime(n)) {
int cnt[] = {0, 0};
for (int i = 0, x, y; i < k; i++) {
x = x1[i]; y = y1[i];
cnt[(x + y) % 2]++;
}
if (n == 2) {
cout << min(2 - cnt[0] + cnt[1], 2 - cnt[1] + cnt[0]);
} else {
cout << min(n * n / 2 + 1 - cnt[0] + cnt[1], n * n / 2 - cnt[1] + cnt[0]);
}
return 0;
}
// stupid //subtest3, subtes4
if (n <= 0) {
cerr << "3, 4" << '\n';
vector <vector <int> > sum(1+n,vector <int> (1+n, 0));
for (int i = 0; i < k; i++) {
for (int x = x1[i]; x <= x2[i]; x++)
for (int y = y1[i]; y <= y2[i]; y++)
sum[x][y] = 1;
}
//for (int i = 1; i <= n; i++) {
// for (int j = 1; j <= n; j++) {
// cout << sum[i][j] << ' ';
// }
// cout << '\n';
//}
//cout << '\n';
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
sum[i][j] += sum[i][j-1];
sum[i][j] += sum[i-1][j];
sum[i][j] -= sum[i-1][j-1];
}
}
//for (int i = 1; i <= n; i++) {
// for (int j = 1; j <= n; j++) {
// cout << sum[i][j] << ' ';
// }
// cout << '\n';
//}
//cout << '\n';
vector <int> div;
for (int i = 1; i < n; i++)
if (n % i == 0)
div.push_back(i);
int ans = 9e18;
for (auto D: div) {
int cnt[] = {0, 0};
int pos = 1;
for (int i = 1;; i += D) {
if (i + D - 1 > n) break;
int par = (pos & 1);
for (int j = 1;; j += D) {
if (j + D - 1 > n) break;
int i2 = i + D - 1, j2 = j + D - 1;
int A = sum[i2][j2];
A -= sum[i2][j-1];
A -= sum[i-1][j2];
A += sum[i-1][j-1];
cnt[par] += A;
par ^= 1;
}
pos++;
}
//cout << D << ' ' << cnt[1] << ' ' << cnt[0] << '\n';
int sc = ((n*n)/(D*D))/2*(D*D);
int fr = ((n*n)/(D*D)+1)/2*(D*D);
//cout << min({fr-cnt[1]+cnt[0],sc-cnt[0]+cnt[1]}) << '\n';
ans = min({ans, fr-cnt[1]+cnt[0], sc-cnt[0]+cnt[1]});
}
cout << ans << '\n';
} else if (n <= 100000) {
cerr << "5" << '\n';
vector <int> div;
for (int i = 1; i < n; i++)
if (n % i == 0)
div.push_back(i);
int ans = n * n;
for (auto D: div) {
int cnt[] = {0, 0};
for (int i = 0; i < k; i++) {
int x = (x1[i] + D - 1) / D, y = (y1[i] + D - 1) / D;
if (x % 2 == y % 2) {
cnt[1] ++;
} else {
cnt[0] ++;
}
}
//cout << D << ' ' << cnt[1] << ' ' << cnt[0] << '\n';
int sc = ((n*n)/(D*D))/2*(D*D);
int fr = ((n*n)/(D*D)+1)/2*(D*D);
//cout << min({fr-cnt[1]+cnt[0],sc-cnt[0]+cnt[1]}) << '\n';
ans = min({ans, fr-cnt[1]+cnt[0], sc-cnt[0]+cnt[1]});
}
cout << ans << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
2284 KB |
Output is correct |
2 |
Correct |
7 ms |
876 KB |
Output is correct |
3 |
Correct |
19 ms |
1516 KB |
Output is correct |
4 |
Correct |
16 ms |
1772 KB |
Output is correct |
5 |
Correct |
21 ms |
2028 KB |
Output is correct |
6 |
Correct |
13 ms |
1388 KB |
Output is correct |
7 |
Correct |
3 ms |
620 KB |
Output is correct |
8 |
Correct |
15 ms |
1516 KB |
Output is correct |
9 |
Correct |
35 ms |
3308 KB |
Output is correct |
10 |
Correct |
20 ms |
1900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
384 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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
384 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 |
11 ms |
1132 KB |
Output is correct |
17 |
Correct |
27 ms |
2924 KB |
Output is correct |
18 |
Correct |
36 ms |
3436 KB |
Output is correct |
19 |
Correct |
68 ms |
3052 KB |
Output is correct |
20 |
Correct |
75 ms |
3436 KB |
Output is correct |
21 |
Correct |
26 ms |
2924 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
19 ms |
1644 KB |
Output is correct |
24 |
Correct |
34 ms |
3180 KB |
Output is correct |
25 |
Correct |
4 ms |
620 KB |
Output is correct |
26 |
Correct |
21 ms |
2156 KB |
Output is correct |
27 |
Correct |
27 ms |
2412 KB |
Output is correct |
28 |
Correct |
34 ms |
3308 KB |
Output is correct |
29 |
Correct |
11 ms |
1388 KB |
Output is correct |
30 |
Correct |
2 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
2284 KB |
Output is correct |
2 |
Correct |
7 ms |
876 KB |
Output is correct |
3 |
Correct |
19 ms |
1516 KB |
Output is correct |
4 |
Correct |
16 ms |
1772 KB |
Output is correct |
5 |
Correct |
21 ms |
2028 KB |
Output is correct |
6 |
Correct |
13 ms |
1388 KB |
Output is correct |
7 |
Correct |
3 ms |
620 KB |
Output is correct |
8 |
Correct |
15 ms |
1516 KB |
Output is correct |
9 |
Correct |
35 ms |
3308 KB |
Output is correct |
10 |
Correct |
20 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 |
384 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 |
11 ms |
1132 KB |
Output is correct |
27 |
Correct |
27 ms |
2924 KB |
Output is correct |
28 |
Correct |
36 ms |
3436 KB |
Output is correct |
29 |
Correct |
68 ms |
3052 KB |
Output is correct |
30 |
Correct |
75 ms |
3436 KB |
Output is correct |
31 |
Correct |
26 ms |
2924 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
19 ms |
1644 KB |
Output is correct |
34 |
Correct |
34 ms |
3180 KB |
Output is correct |
35 |
Correct |
4 ms |
620 KB |
Output is correct |
36 |
Correct |
21 ms |
2156 KB |
Output is correct |
37 |
Correct |
27 ms |
2412 KB |
Output is correct |
38 |
Correct |
34 ms |
3308 KB |
Output is correct |
39 |
Correct |
11 ms |
1388 KB |
Output is correct |
40 |
Correct |
2 ms |
492 KB |
Output is correct |
41 |
Correct |
72 ms |
3052 KB |
Output is correct |
42 |
Correct |
41 ms |
3308 KB |
Output is correct |
43 |
Correct |
48 ms |
2924 KB |
Output is correct |
44 |
Correct |
39 ms |
3180 KB |
Output is correct |
45 |
Correct |
39 ms |
3436 KB |
Output is correct |
46 |
Correct |
73 ms |
3328 KB |
Output is correct |
47 |
Correct |
31 ms |
3052 KB |
Output is correct |
48 |
Correct |
46 ms |
3052 KB |
Output is correct |
49 |
Correct |
36 ms |
2924 KB |
Output is correct |
50 |
Correct |
241 ms |
3308 KB |
Output is correct |
51 |
Correct |
259 ms |
3308 KB |
Output is correct |
52 |
Correct |
241 ms |
3180 KB |
Output is correct |
53 |
Correct |
255 ms |
3436 KB |
Output is correct |
54 |
Correct |
236 ms |
3308 KB |
Output is correct |
55 |
Correct |
268 ms |
3436 KB |
Output is correct |
56 |
Correct |
239 ms |
3052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 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 |
23 ms |
2284 KB |
Output is correct |
10 |
Correct |
7 ms |
876 KB |
Output is correct |
11 |
Correct |
19 ms |
1516 KB |
Output is correct |
12 |
Correct |
16 ms |
1772 KB |
Output is correct |
13 |
Correct |
21 ms |
2028 KB |
Output is correct |
14 |
Correct |
13 ms |
1388 KB |
Output is correct |
15 |
Correct |
3 ms |
620 KB |
Output is correct |
16 |
Correct |
15 ms |
1516 KB |
Output is correct |
17 |
Correct |
35 ms |
3308 KB |
Output is correct |
18 |
Correct |
20 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 |
384 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 |
11 ms |
1132 KB |
Output is correct |
35 |
Correct |
27 ms |
2924 KB |
Output is correct |
36 |
Correct |
36 ms |
3436 KB |
Output is correct |
37 |
Correct |
68 ms |
3052 KB |
Output is correct |
38 |
Correct |
75 ms |
3436 KB |
Output is correct |
39 |
Correct |
26 ms |
2924 KB |
Output is correct |
40 |
Correct |
1 ms |
364 KB |
Output is correct |
41 |
Correct |
19 ms |
1644 KB |
Output is correct |
42 |
Correct |
34 ms |
3180 KB |
Output is correct |
43 |
Correct |
4 ms |
620 KB |
Output is correct |
44 |
Correct |
21 ms |
2156 KB |
Output is correct |
45 |
Correct |
27 ms |
2412 KB |
Output is correct |
46 |
Correct |
34 ms |
3308 KB |
Output is correct |
47 |
Correct |
11 ms |
1388 KB |
Output is correct |
48 |
Correct |
2 ms |
492 KB |
Output is correct |
49 |
Correct |
72 ms |
3052 KB |
Output is correct |
50 |
Correct |
41 ms |
3308 KB |
Output is correct |
51 |
Correct |
48 ms |
2924 KB |
Output is correct |
52 |
Correct |
39 ms |
3180 KB |
Output is correct |
53 |
Correct |
39 ms |
3436 KB |
Output is correct |
54 |
Correct |
73 ms |
3328 KB |
Output is correct |
55 |
Correct |
31 ms |
3052 KB |
Output is correct |
56 |
Correct |
46 ms |
3052 KB |
Output is correct |
57 |
Correct |
36 ms |
2924 KB |
Output is correct |
58 |
Correct |
241 ms |
3308 KB |
Output is correct |
59 |
Correct |
259 ms |
3308 KB |
Output is correct |
60 |
Correct |
241 ms |
3180 KB |
Output is correct |
61 |
Correct |
255 ms |
3436 KB |
Output is correct |
62 |
Correct |
236 ms |
3308 KB |
Output is correct |
63 |
Correct |
268 ms |
3436 KB |
Output is correct |
64 |
Correct |
239 ms |
3052 KB |
Output is correct |
65 |
Correct |
0 ms |
364 KB |
Output is correct |
66 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
67 |
Halted |
0 ms |
0 KB |
- |