#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define f first
#define s second
#define pii pair<ll,ll>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;
/*
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
*/
int pos[100001][2];
ll compute_line(ll x1, ll y1, ll x2, ll y2, ll k){
ll u = (x1 / k) + (y1 / k);
ll v = (x2 / k) + (y2 / k);
ll a = (x2 - x1 + 1);
ll b = (y2 - y1 + 1);
ll cnt = 0;
if(u % 2 == v % 2){
if(u & 1){
cnt += k * a;
}
else cnt -= k * a;
}
ll c = k - (y1 % k);
if(u & 1){
cnt -= c * a;
}
else cnt += c * a;
ll d = y2 % k;
d++;
if(v & 1) cnt -= d * a;
else cnt += d * a;
return cnt;
}
ll C(ll x1, ll y1, ll x2, ll y2, ll k){
if(x1 / k == x2 / k){
if(y1 / k == y2 / k){
ll x = x1 / k + y1 / k;
if(x % 2) return -(x2 - x1 + 1) * (y2 - y1 + 1);
return (x2 - x1 + 1) * (y2 - y1 + 1);
}
return compute_line(x1, y1, x2, y2, k);
}
if(y1 / k == y2 / k)
return compute_line(y1, x1, y2, x2, k);
ll cnt = 0;
cnt += compute_line(x1, y1, x1 / k * k + (k - 1), y2, k);
cnt += compute_line(x2 / k * k, y1, x2, y2, k);
ll v = x2 / k;
v -= x1 / k;
v--;
if(v > 0){
ll j = x1 / k * k + k;
ll nw = compute_line(j, y1, j + k - 1, y2, k);
if(v & 1) cnt += nw;
}
return cnt;
}
int main(){
// freopen("school.in", "r", stdin);
// freopen("school.out", "w", stdout);
int n, k;
cin >> n >> k;
int x1[k + 1], x2[k + 1], y1[k + 1], y2[k + 1];
for(int i = 1; i <= k; ++i){
cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];
x1[i]--;
y1[i]--;
x2[i]--;
y2[i]--;
}
ll ans = 1e18 + 1;
for(ll a = 1; a <n; a++){
if(n % a) continue;
ll v = n / a;
ll cnt = 0;
if(v % 2 == 1)
cnt += v * (v / 2) + (v + 1) / 2;
else cnt = v * v / 2;
cnt *= a * a;
for(int i = 1; i <= k; i++){
cnt -= C(x1[i], y1[i], x2[i], y2[i], a);
}
ans = min(ans, cnt);
}
for(ll a = 1; a <n; a++){
if(n % a) continue;
ll v = n / a;
ll cnt = -1;
if(v % 2 == 1)
cnt += v * (v / 2) + (v + 1) / 2;
else cnt = v * v / 2;
cnt *= a * a;
for(int i = 1; i <= k; i++)
cnt += C(x1[i], y1[i], x2[i], y2[i], a);
ans = min(ans, cnt);
}
cout << ans;
return 0;
}
Compilation message
chessboard.cpp: In function 'long long int compute_line(long long int, long long int, long long int, long long int, long long int)':
chessboard.cpp:29:8: warning: unused variable 'b' [-Wunused-variable]
29 | ll b = (y2 - y1 + 1);
| ^
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
1236 KB |
Output is correct |
2 |
Correct |
17 ms |
468 KB |
Output is correct |
3 |
Correct |
43 ms |
900 KB |
Output is correct |
4 |
Correct |
41 ms |
980 KB |
Output is correct |
5 |
Correct |
69 ms |
1108 KB |
Output is correct |
6 |
Correct |
37 ms |
852 KB |
Output is correct |
7 |
Correct |
8 ms |
340 KB |
Output is correct |
8 |
Correct |
37 ms |
724 KB |
Output is correct |
9 |
Correct |
94 ms |
1704 KB |
Output is correct |
10 |
Correct |
52 ms |
1072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
2 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 |
212 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 |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
2 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 |
212 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 |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
26 ms |
724 KB |
Output is correct |
17 |
Correct |
71 ms |
1600 KB |
Output is correct |
18 |
Correct |
91 ms |
1836 KB |
Output is correct |
19 |
Correct |
197 ms |
1668 KB |
Output is correct |
20 |
Correct |
209 ms |
1832 KB |
Output is correct |
21 |
Correct |
66 ms |
1548 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
48 ms |
944 KB |
Output is correct |
24 |
Correct |
81 ms |
1696 KB |
Output is correct |
25 |
Correct |
9 ms |
340 KB |
Output is correct |
26 |
Correct |
51 ms |
1184 KB |
Output is correct |
27 |
Correct |
70 ms |
1344 KB |
Output is correct |
28 |
Correct |
101 ms |
1776 KB |
Output is correct |
29 |
Correct |
27 ms |
724 KB |
Output is correct |
30 |
Correct |
3 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
1236 KB |
Output is correct |
2 |
Correct |
17 ms |
468 KB |
Output is correct |
3 |
Correct |
43 ms |
900 KB |
Output is correct |
4 |
Correct |
41 ms |
980 KB |
Output is correct |
5 |
Correct |
69 ms |
1108 KB |
Output is correct |
6 |
Correct |
37 ms |
852 KB |
Output is correct |
7 |
Correct |
8 ms |
340 KB |
Output is correct |
8 |
Correct |
37 ms |
724 KB |
Output is correct |
9 |
Correct |
94 ms |
1704 KB |
Output is correct |
10 |
Correct |
52 ms |
1072 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 |
2 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 |
212 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 |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
26 ms |
724 KB |
Output is correct |
27 |
Correct |
71 ms |
1600 KB |
Output is correct |
28 |
Correct |
91 ms |
1836 KB |
Output is correct |
29 |
Correct |
197 ms |
1668 KB |
Output is correct |
30 |
Correct |
209 ms |
1832 KB |
Output is correct |
31 |
Correct |
66 ms |
1548 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
48 ms |
944 KB |
Output is correct |
34 |
Correct |
81 ms |
1696 KB |
Output is correct |
35 |
Correct |
9 ms |
340 KB |
Output is correct |
36 |
Correct |
51 ms |
1184 KB |
Output is correct |
37 |
Correct |
70 ms |
1344 KB |
Output is correct |
38 |
Correct |
101 ms |
1776 KB |
Output is correct |
39 |
Correct |
27 ms |
724 KB |
Output is correct |
40 |
Correct |
3 ms |
340 KB |
Output is correct |
41 |
Correct |
191 ms |
1576 KB |
Output is correct |
42 |
Correct |
118 ms |
1756 KB |
Output is correct |
43 |
Correct |
146 ms |
1588 KB |
Output is correct |
44 |
Correct |
106 ms |
1620 KB |
Output is correct |
45 |
Correct |
104 ms |
1816 KB |
Output is correct |
46 |
Correct |
234 ms |
1748 KB |
Output is correct |
47 |
Correct |
96 ms |
1636 KB |
Output is correct |
48 |
Correct |
124 ms |
1620 KB |
Output is correct |
49 |
Correct |
99 ms |
1600 KB |
Output is correct |
50 |
Correct |
719 ms |
1716 KB |
Output is correct |
51 |
Correct |
772 ms |
1792 KB |
Output is correct |
52 |
Correct |
718 ms |
1620 KB |
Output is correct |
53 |
Correct |
769 ms |
1792 KB |
Output is correct |
54 |
Correct |
701 ms |
1740 KB |
Output is correct |
55 |
Correct |
810 ms |
1848 KB |
Output is correct |
56 |
Correct |
713 ms |
1632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
66 ms |
1236 KB |
Output is correct |
10 |
Correct |
17 ms |
468 KB |
Output is correct |
11 |
Correct |
43 ms |
900 KB |
Output is correct |
12 |
Correct |
41 ms |
980 KB |
Output is correct |
13 |
Correct |
69 ms |
1108 KB |
Output is correct |
14 |
Correct |
37 ms |
852 KB |
Output is correct |
15 |
Correct |
8 ms |
340 KB |
Output is correct |
16 |
Correct |
37 ms |
724 KB |
Output is correct |
17 |
Correct |
94 ms |
1704 KB |
Output is correct |
18 |
Correct |
52 ms |
1072 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
2 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 |
212 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 |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
26 ms |
724 KB |
Output is correct |
35 |
Correct |
71 ms |
1600 KB |
Output is correct |
36 |
Correct |
91 ms |
1836 KB |
Output is correct |
37 |
Correct |
197 ms |
1668 KB |
Output is correct |
38 |
Correct |
209 ms |
1832 KB |
Output is correct |
39 |
Correct |
66 ms |
1548 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
48 ms |
944 KB |
Output is correct |
42 |
Correct |
81 ms |
1696 KB |
Output is correct |
43 |
Correct |
9 ms |
340 KB |
Output is correct |
44 |
Correct |
51 ms |
1184 KB |
Output is correct |
45 |
Correct |
70 ms |
1344 KB |
Output is correct |
46 |
Correct |
101 ms |
1776 KB |
Output is correct |
47 |
Correct |
27 ms |
724 KB |
Output is correct |
48 |
Correct |
3 ms |
340 KB |
Output is correct |
49 |
Correct |
191 ms |
1576 KB |
Output is correct |
50 |
Correct |
118 ms |
1756 KB |
Output is correct |
51 |
Correct |
146 ms |
1588 KB |
Output is correct |
52 |
Correct |
106 ms |
1620 KB |
Output is correct |
53 |
Correct |
104 ms |
1816 KB |
Output is correct |
54 |
Correct |
234 ms |
1748 KB |
Output is correct |
55 |
Correct |
96 ms |
1636 KB |
Output is correct |
56 |
Correct |
124 ms |
1620 KB |
Output is correct |
57 |
Correct |
99 ms |
1600 KB |
Output is correct |
58 |
Correct |
719 ms |
1716 KB |
Output is correct |
59 |
Correct |
772 ms |
1792 KB |
Output is correct |
60 |
Correct |
718 ms |
1620 KB |
Output is correct |
61 |
Correct |
769 ms |
1792 KB |
Output is correct |
62 |
Correct |
701 ms |
1740 KB |
Output is correct |
63 |
Correct |
810 ms |
1848 KB |
Output is correct |
64 |
Correct |
713 ms |
1632 KB |
Output is correct |
65 |
Correct |
0 ms |
212 KB |
Output is correct |
66 |
Correct |
1 ms |
212 KB |
Output is correct |
67 |
Correct |
893 ms |
1748 KB |
Output is correct |
68 |
Correct |
901 ms |
4044 KB |
Output is correct |
69 |
Correct |
776 ms |
3504 KB |
Output is correct |
70 |
Correct |
821 ms |
3860 KB |
Output is correct |
71 |
Correct |
828 ms |
3916 KB |
Output is correct |
72 |
Correct |
812 ms |
3796 KB |
Output is correct |
73 |
Correct |
825 ms |
3644 KB |
Output is correct |
74 |
Correct |
883 ms |
4032 KB |
Output is correct |
75 |
Correct |
862 ms |
3772 KB |
Output is correct |
76 |
Correct |
903 ms |
4008 KB |
Output is correct |
77 |
Correct |
208 ms |
4116 KB |
Output is correct |
78 |
Correct |
112 ms |
3784 KB |
Output is correct |
79 |
Correct |
163 ms |
3516 KB |
Output is correct |
80 |
Correct |
164 ms |
3836 KB |
Output is correct |
81 |
Correct |
160 ms |
3516 KB |
Output is correct |
82 |
Correct |
147 ms |
3924 KB |
Output is correct |
83 |
Correct |
128 ms |
3660 KB |
Output is correct |
84 |
Correct |
561 ms |
4112 KB |
Output is correct |
85 |
Correct |
951 ms |
4144 KB |
Output is correct |
86 |
Correct |
2 ms |
212 KB |
Output is correct |
87 |
Correct |
1 ms |
212 KB |
Output is correct |
88 |
Correct |
944 ms |
4252 KB |
Output is correct |
89 |
Correct |
190 ms |
1024 KB |
Output is correct |
90 |
Correct |
1 ms |
212 KB |
Output is correct |