#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxK = 1e5;
struct rectangle {
int l1, c1, l2, c2;
};
int l;
rectangle black[maxK];
int nxt( int x ) {
return (x / l + 1) * l;
}
int prv( int x ) {
return x / l * l - 1;
}
long long wbSq( int lin, int col, bool color ) {
if ( lin == 0 || col == 0 )
return 0;
return ((lin / 2) * ((col + 1) / 2) + ((lin + 1) / 2) * (col / 2) - ((lin + 1) / 2) * ((col + 1) / 2) - ((lin / 2) * (col / 2))) * (color == 0 ? 1 : -1);
}
long long getCost( rectangle r ) {
int l1 = r.l1, c1 = r.c1, l2 = r.l2, c2 = r.c2;
long long cost = 0;
if ( l1 / l == l2 / l && c1 / l == c2 / l )
cost += (l2 - l1 + 1) * (c2 - c1 + 1) * wbSq( 1, 1, (l1 / l + c1 / l) % 2 );
else if ( l1 / l == l2 / l ) {
cost += (l2 - l1 + 1) * (nxt( c1 ) - c1) * wbSq( 1, 1, (l1 / l + c1 / l) % 2 );
cost += (l2 - l1 + 1) * (c2 - prv( c2 )) * wbSq( 1, 1, (l2 / l + c2 / l) % 2 );
cost += l * (l2 - l1 + 1) * wbSq( 1, c2 / l - c1 / l - 1, (l1 / l + nxt( c1 ) / l) % 2 );
} else if ( c1 / l == c2 / l ) {
cost += (nxt( l1 ) - l1) * (c2 - c1 + 1) * wbSq( 1, 1, (l1 / l + c2 / l) % 2 );
cost += (l2 - prv( l2 )) * (c2 - c1 + 1) * wbSq( 1, 1, (l2 / l + c2 / l) % 2 );
cost += l * (c2 - c1 + 1) * wbSq( l2 / l - l1 / l - 1, 1, (nxt( l1 ) / l + c1 / l) % 2 );
} else {
cost += (nxt( l1 ) - l1) * (nxt( c1 ) - c1) * wbSq( 1, 1, (l1 / l + c1 / l) % 2 );
cost += (nxt( l1 ) - l1) * (c2 - prv( c2 )) * wbSq( 1, 1, (l1 / l + c2 / l) % 2 );
cost += (l2 - prv( l2 )) * (nxt( c1 ) - c1) * wbSq( 1, 1, (l2 / l + c1 / l) % 2 );
cost += (l2 - prv( l2 )) * (c2 - prv( c2 )) * wbSq( 1, 1, (l2 / l + c2 / l) % 2 );
cost += l * (nxt( l1 ) - l1) * wbSq( 1, c2 / l - c1 / l - 1, (l1 / l + nxt( c1 ) / l) % 2 );
cost += l * (nxt( c1 ) - c1) * wbSq( l2 / l - l1 / l - 1, 1, (nxt( l1 ) / l + c1 / l) % 2 );
cost += l * (l2 - prv( l2 )) * wbSq( 1, c2 / l - c1 / l - 1, (l2 / l + nxt( c1 ) / l) % 2 );
cost += l * (c2 - prv( c2 )) * wbSq( l2 / l - l1 / l - 1, 1, (nxt( l1 ) / l + c2 / l) % 2 );
cost += l * l * wbSq( l2 / l - l1 / l - 1, c2 / l - c1 / l - 1, (nxt( l1 ) / l + nxt( c1 ) / l) % 2 );
}
return cost;
}
signed main() {
int n, k;
long long cost, minCost;
cin >> n >> k;
for ( int i = 0; i < k; i++ ) {
cin >> black[i].l1 >> black[i].c1 >> black[i].l2 >> black[i].c2;
black[i].l1--;
black[i].c1--;
black[i].l2--;
black[i].c2--;
}
minCost = n * n;
for ( l = 1; l < n; l++ ) {
if ( n % l == 0 ) {
int x = (n / l + 1) / 2, y = n / l / 2;
cost = (l * l) * (x * x + y * y);
for ( int i = 0; i < k; i++ )
cost += getCost( black[i] );
minCost = min( minCost, cost );
cost = 2 * x * y * l * l;
for ( int i = 0; i < k; i++ )
cost -= getCost( black[i] );
minCost = min( minCost, cost );
}
}
cout << minCost;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
2296 KB |
Output is correct |
2 |
Correct |
21 ms |
724 KB |
Output is correct |
3 |
Correct |
41 ms |
1404 KB |
Output is correct |
4 |
Correct |
42 ms |
1604 KB |
Output is correct |
5 |
Correct |
58 ms |
1992 KB |
Output is correct |
6 |
Correct |
36 ms |
1356 KB |
Output is correct |
7 |
Correct |
8 ms |
468 KB |
Output is correct |
8 |
Correct |
36 ms |
1372 KB |
Output is correct |
9 |
Correct |
95 ms |
3088 KB |
Output is correct |
10 |
Correct |
51 ms |
1840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
26 ms |
1164 KB |
Output is correct |
17 |
Correct |
76 ms |
2900 KB |
Output is correct |
18 |
Correct |
91 ms |
3352 KB |
Output is correct |
19 |
Correct |
167 ms |
2940 KB |
Output is correct |
20 |
Correct |
187 ms |
3368 KB |
Output is correct |
21 |
Correct |
64 ms |
2744 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
44 ms |
1608 KB |
Output is correct |
24 |
Correct |
83 ms |
2980 KB |
Output is correct |
25 |
Correct |
9 ms |
468 KB |
Output is correct |
26 |
Correct |
50 ms |
2052 KB |
Output is correct |
27 |
Correct |
68 ms |
2408 KB |
Output is correct |
28 |
Correct |
83 ms |
3184 KB |
Output is correct |
29 |
Correct |
28 ms |
1292 KB |
Output is correct |
30 |
Correct |
3 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
2296 KB |
Output is correct |
2 |
Correct |
21 ms |
724 KB |
Output is correct |
3 |
Correct |
41 ms |
1404 KB |
Output is correct |
4 |
Correct |
42 ms |
1604 KB |
Output is correct |
5 |
Correct |
58 ms |
1992 KB |
Output is correct |
6 |
Correct |
36 ms |
1356 KB |
Output is correct |
7 |
Correct |
8 ms |
468 KB |
Output is correct |
8 |
Correct |
36 ms |
1372 KB |
Output is correct |
9 |
Correct |
95 ms |
3088 KB |
Output is correct |
10 |
Correct |
51 ms |
1840 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
26 ms |
1164 KB |
Output is correct |
27 |
Correct |
76 ms |
2900 KB |
Output is correct |
28 |
Correct |
91 ms |
3352 KB |
Output is correct |
29 |
Correct |
167 ms |
2940 KB |
Output is correct |
30 |
Correct |
187 ms |
3368 KB |
Output is correct |
31 |
Correct |
64 ms |
2744 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
44 ms |
1608 KB |
Output is correct |
34 |
Correct |
83 ms |
2980 KB |
Output is correct |
35 |
Correct |
9 ms |
468 KB |
Output is correct |
36 |
Correct |
50 ms |
2052 KB |
Output is correct |
37 |
Correct |
68 ms |
2408 KB |
Output is correct |
38 |
Correct |
83 ms |
3184 KB |
Output is correct |
39 |
Correct |
28 ms |
1292 KB |
Output is correct |
40 |
Correct |
3 ms |
340 KB |
Output is correct |
41 |
Correct |
170 ms |
2960 KB |
Output is correct |
42 |
Correct |
115 ms |
3212 KB |
Output is correct |
43 |
Correct |
127 ms |
2784 KB |
Output is correct |
44 |
Correct |
103 ms |
3036 KB |
Output is correct |
45 |
Correct |
101 ms |
3348 KB |
Output is correct |
46 |
Correct |
194 ms |
3252 KB |
Output is correct |
47 |
Correct |
92 ms |
2924 KB |
Output is correct |
48 |
Correct |
117 ms |
3000 KB |
Output is correct |
49 |
Correct |
103 ms |
2864 KB |
Output is correct |
50 |
Correct |
612 ms |
3156 KB |
Output is correct |
51 |
Correct |
647 ms |
3308 KB |
Output is correct |
52 |
Correct |
612 ms |
3100 KB |
Output is correct |
53 |
Correct |
637 ms |
3288 KB |
Output is correct |
54 |
Correct |
598 ms |
3064 KB |
Output is correct |
55 |
Correct |
666 ms |
3404 KB |
Output is correct |
56 |
Correct |
592 ms |
2912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
76 ms |
2296 KB |
Output is correct |
10 |
Correct |
21 ms |
724 KB |
Output is correct |
11 |
Correct |
41 ms |
1404 KB |
Output is correct |
12 |
Correct |
42 ms |
1604 KB |
Output is correct |
13 |
Correct |
58 ms |
1992 KB |
Output is correct |
14 |
Correct |
36 ms |
1356 KB |
Output is correct |
15 |
Correct |
8 ms |
468 KB |
Output is correct |
16 |
Correct |
36 ms |
1372 KB |
Output is correct |
17 |
Correct |
95 ms |
3088 KB |
Output is correct |
18 |
Correct |
51 ms |
1840 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
2 ms |
340 KB |
Output is correct |
34 |
Correct |
26 ms |
1164 KB |
Output is correct |
35 |
Correct |
76 ms |
2900 KB |
Output is correct |
36 |
Correct |
91 ms |
3352 KB |
Output is correct |
37 |
Correct |
167 ms |
2940 KB |
Output is correct |
38 |
Correct |
187 ms |
3368 KB |
Output is correct |
39 |
Correct |
64 ms |
2744 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
44 ms |
1608 KB |
Output is correct |
42 |
Correct |
83 ms |
2980 KB |
Output is correct |
43 |
Correct |
9 ms |
468 KB |
Output is correct |
44 |
Correct |
50 ms |
2052 KB |
Output is correct |
45 |
Correct |
68 ms |
2408 KB |
Output is correct |
46 |
Correct |
83 ms |
3184 KB |
Output is correct |
47 |
Correct |
28 ms |
1292 KB |
Output is correct |
48 |
Correct |
3 ms |
340 KB |
Output is correct |
49 |
Correct |
170 ms |
2960 KB |
Output is correct |
50 |
Correct |
115 ms |
3212 KB |
Output is correct |
51 |
Correct |
127 ms |
2784 KB |
Output is correct |
52 |
Correct |
103 ms |
3036 KB |
Output is correct |
53 |
Correct |
101 ms |
3348 KB |
Output is correct |
54 |
Correct |
194 ms |
3252 KB |
Output is correct |
55 |
Correct |
92 ms |
2924 KB |
Output is correct |
56 |
Correct |
117 ms |
3000 KB |
Output is correct |
57 |
Correct |
103 ms |
2864 KB |
Output is correct |
58 |
Correct |
612 ms |
3156 KB |
Output is correct |
59 |
Correct |
647 ms |
3308 KB |
Output is correct |
60 |
Correct |
612 ms |
3100 KB |
Output is correct |
61 |
Correct |
637 ms |
3288 KB |
Output is correct |
62 |
Correct |
598 ms |
3064 KB |
Output is correct |
63 |
Correct |
666 ms |
3404 KB |
Output is correct |
64 |
Correct |
592 ms |
2912 KB |
Output is correct |
65 |
Correct |
0 ms |
212 KB |
Output is correct |
66 |
Correct |
0 ms |
212 KB |
Output is correct |
67 |
Correct |
725 ms |
3200 KB |
Output is correct |
68 |
Correct |
701 ms |
5332 KB |
Output is correct |
69 |
Correct |
617 ms |
4772 KB |
Output is correct |
70 |
Correct |
687 ms |
5256 KB |
Output is correct |
71 |
Correct |
659 ms |
5168 KB |
Output is correct |
72 |
Correct |
677 ms |
5108 KB |
Output is correct |
73 |
Correct |
650 ms |
5080 KB |
Output is correct |
74 |
Correct |
702 ms |
5368 KB |
Output is correct |
75 |
Correct |
686 ms |
5152 KB |
Output is correct |
76 |
Correct |
723 ms |
5484 KB |
Output is correct |
77 |
Correct |
186 ms |
5836 KB |
Output is correct |
78 |
Correct |
106 ms |
5120 KB |
Output is correct |
79 |
Correct |
137 ms |
4848 KB |
Output is correct |
80 |
Correct |
153 ms |
5164 KB |
Output is correct |
81 |
Correct |
133 ms |
4708 KB |
Output is correct |
82 |
Correct |
130 ms |
5452 KB |
Output is correct |
83 |
Correct |
129 ms |
4940 KB |
Output is correct |
84 |
Correct |
467 ms |
5556 KB |
Output is correct |
85 |
Correct |
735 ms |
5804 KB |
Output is correct |
86 |
Correct |
2 ms |
212 KB |
Output is correct |
87 |
Correct |
1 ms |
212 KB |
Output is correct |
88 |
Correct |
755 ms |
5792 KB |
Output is correct |
89 |
Correct |
153 ms |
1284 KB |
Output is correct |
90 |
Correct |
1 ms |
212 KB |
Output is correct |