#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll n, k; vector<ll> v;
ll num[100100];
ll mod(ll x, ll vel){
if (x % vel == 0 && x != 0)
return vel;
return (x % vel);
}
bool black(ll x, ll y, ll vel){
x -= mod(x, vel); y -= mod(y, vel);
x /= vel; y /= vel;
if ((x + y) & 1)
return 0;
return 1;
}
ll pref(ll x, ll y, ll vel){
if (x == 0 || y == 0) return 0;
ll ans = 0;
ll x2 = x - mod(x, vel); ll y2 = y - mod(y, vel);
ll val1 = x2 / vel; ll val2 = y2 / vel;
ll p = val1 * val2; ll u = p / 2;
ans += -(p - u) * vel * vel + u * vel * vel;
p = val2; u = p / 2; ll pt = 1;
if (val1 % 2 == 0) pt = -1;
ans += pt * (p - u) * vel * (x - x2) + -pt * u * vel * (x - x2);
p = val1; u = p / 2; pt = 1;
if (val2 % 2 == 0) pt = -1;
ans += pt * (p - u) * vel * (y - y2) + -pt * u * vel * (y - y2);
if (black(x, y, vel))
ans -= (x - x2) * (y - y2);
else ans += (x - x2) * (y - y2);
return ans;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> k;
for (ll i = 1; i < n; ++i){
if (n % i == 0){
v.push_back(i);
ll d = n / i;
num[i] = (d * d + 1) / 2; num[i] *= i * i;
}
}
for (int i = 0; i < k; ++i){
ll x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2;
for (ll vel: v){
num[vel] += (pref(x2, y2, vel) - pref(x2, y1 - 1, vel) - pref(x1 - 1, y2, vel) + pref(x1 - 1, y1 - 1, vel));
}
}
ll sol = 1e18;
for (ll x: v) sol = min(abs(sol), min(num[x], n * n - abs(num[x])));
cout << sol;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
312 KB |
Output is correct |
2 |
Correct |
7 ms |
336 KB |
Output is correct |
3 |
Correct |
23 ms |
308 KB |
Output is correct |
4 |
Correct |
18 ms |
212 KB |
Output is correct |
5 |
Correct |
25 ms |
332 KB |
Output is correct |
6 |
Correct |
16 ms |
212 KB |
Output is correct |
7 |
Correct |
4 ms |
212 KB |
Output is correct |
8 |
Correct |
16 ms |
332 KB |
Output is correct |
9 |
Correct |
40 ms |
212 KB |
Output is correct |
10 |
Correct |
22 ms |
324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
17 ms |
332 KB |
Output is correct |
17 |
Correct |
32 ms |
212 KB |
Output is correct |
18 |
Correct |
63 ms |
308 KB |
Output is correct |
19 |
Correct |
338 ms |
332 KB |
Output is correct |
20 |
Correct |
371 ms |
212 KB |
Output is correct |
21 |
Correct |
32 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
48 ms |
316 KB |
Output is correct |
24 |
Correct |
57 ms |
212 KB |
Output is correct |
25 |
Correct |
10 ms |
332 KB |
Output is correct |
26 |
Correct |
35 ms |
212 KB |
Output is correct |
27 |
Correct |
58 ms |
212 KB |
Output is correct |
28 |
Correct |
61 ms |
312 KB |
Output is correct |
29 |
Correct |
13 ms |
336 KB |
Output is correct |
30 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
312 KB |
Output is correct |
2 |
Correct |
7 ms |
336 KB |
Output is correct |
3 |
Correct |
23 ms |
308 KB |
Output is correct |
4 |
Correct |
18 ms |
212 KB |
Output is correct |
5 |
Correct |
25 ms |
332 KB |
Output is correct |
6 |
Correct |
16 ms |
212 KB |
Output is correct |
7 |
Correct |
4 ms |
212 KB |
Output is correct |
8 |
Correct |
16 ms |
332 KB |
Output is correct |
9 |
Correct |
40 ms |
212 KB |
Output is correct |
10 |
Correct |
22 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 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 |
17 ms |
332 KB |
Output is correct |
27 |
Correct |
32 ms |
212 KB |
Output is correct |
28 |
Correct |
63 ms |
308 KB |
Output is correct |
29 |
Correct |
338 ms |
332 KB |
Output is correct |
30 |
Correct |
371 ms |
212 KB |
Output is correct |
31 |
Correct |
32 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
48 ms |
316 KB |
Output is correct |
34 |
Correct |
57 ms |
212 KB |
Output is correct |
35 |
Correct |
10 ms |
332 KB |
Output is correct |
36 |
Correct |
35 ms |
212 KB |
Output is correct |
37 |
Correct |
58 ms |
212 KB |
Output is correct |
38 |
Correct |
61 ms |
312 KB |
Output is correct |
39 |
Correct |
13 ms |
336 KB |
Output is correct |
40 |
Correct |
2 ms |
340 KB |
Output is correct |
41 |
Correct |
306 ms |
348 KB |
Output is correct |
42 |
Correct |
67 ms |
316 KB |
Output is correct |
43 |
Correct |
159 ms |
340 KB |
Output is correct |
44 |
Correct |
78 ms |
316 KB |
Output is correct |
45 |
Correct |
55 ms |
312 KB |
Output is correct |
46 |
Correct |
333 ms |
348 KB |
Output is correct |
47 |
Correct |
37 ms |
212 KB |
Output is correct |
48 |
Correct |
113 ms |
340 KB |
Output is correct |
49 |
Correct |
64 ms |
308 KB |
Output is correct |
50 |
Correct |
1658 ms |
400 KB |
Output is correct |
51 |
Correct |
1770 ms |
396 KB |
Output is correct |
52 |
Correct |
1648 ms |
404 KB |
Output is correct |
53 |
Correct |
1758 ms |
400 KB |
Output is correct |
54 |
Correct |
1633 ms |
400 KB |
Output is correct |
55 |
Correct |
1820 ms |
404 KB |
Output is correct |
56 |
Correct |
1571 ms |
400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 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 |
32 ms |
312 KB |
Output is correct |
10 |
Correct |
7 ms |
336 KB |
Output is correct |
11 |
Correct |
23 ms |
308 KB |
Output is correct |
12 |
Correct |
18 ms |
212 KB |
Output is correct |
13 |
Correct |
25 ms |
332 KB |
Output is correct |
14 |
Correct |
16 ms |
212 KB |
Output is correct |
15 |
Correct |
4 ms |
212 KB |
Output is correct |
16 |
Correct |
16 ms |
332 KB |
Output is correct |
17 |
Correct |
40 ms |
212 KB |
Output is correct |
18 |
Correct |
22 ms |
324 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 |
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 |
17 ms |
332 KB |
Output is correct |
35 |
Correct |
32 ms |
212 KB |
Output is correct |
36 |
Correct |
63 ms |
308 KB |
Output is correct |
37 |
Correct |
338 ms |
332 KB |
Output is correct |
38 |
Correct |
371 ms |
212 KB |
Output is correct |
39 |
Correct |
32 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
48 ms |
316 KB |
Output is correct |
42 |
Correct |
57 ms |
212 KB |
Output is correct |
43 |
Correct |
10 ms |
332 KB |
Output is correct |
44 |
Correct |
35 ms |
212 KB |
Output is correct |
45 |
Correct |
58 ms |
212 KB |
Output is correct |
46 |
Correct |
61 ms |
312 KB |
Output is correct |
47 |
Correct |
13 ms |
336 KB |
Output is correct |
48 |
Correct |
2 ms |
340 KB |
Output is correct |
49 |
Correct |
306 ms |
348 KB |
Output is correct |
50 |
Correct |
67 ms |
316 KB |
Output is correct |
51 |
Correct |
159 ms |
340 KB |
Output is correct |
52 |
Correct |
78 ms |
316 KB |
Output is correct |
53 |
Correct |
55 ms |
312 KB |
Output is correct |
54 |
Correct |
333 ms |
348 KB |
Output is correct |
55 |
Correct |
37 ms |
212 KB |
Output is correct |
56 |
Correct |
113 ms |
340 KB |
Output is correct |
57 |
Correct |
64 ms |
308 KB |
Output is correct |
58 |
Correct |
1658 ms |
400 KB |
Output is correct |
59 |
Correct |
1770 ms |
396 KB |
Output is correct |
60 |
Correct |
1648 ms |
404 KB |
Output is correct |
61 |
Correct |
1758 ms |
400 KB |
Output is correct |
62 |
Correct |
1633 ms |
400 KB |
Output is correct |
63 |
Correct |
1820 ms |
404 KB |
Output is correct |
64 |
Correct |
1571 ms |
400 KB |
Output is correct |
65 |
Correct |
0 ms |
212 KB |
Output is correct |
66 |
Correct |
1 ms |
212 KB |
Output is correct |
67 |
Correct |
1695 ms |
408 KB |
Output is correct |
68 |
Correct |
1702 ms |
400 KB |
Output is correct |
69 |
Correct |
1485 ms |
396 KB |
Output is correct |
70 |
Correct |
1687 ms |
404 KB |
Output is correct |
71 |
Correct |
1655 ms |
400 KB |
Output is correct |
72 |
Correct |
1612 ms |
400 KB |
Output is correct |
73 |
Correct |
1551 ms |
400 KB |
Output is correct |
74 |
Correct |
1694 ms |
400 KB |
Output is correct |
75 |
Correct |
1643 ms |
400 KB |
Output is correct |
76 |
Correct |
1761 ms |
396 KB |
Output is correct |
77 |
Correct |
241 ms |
340 KB |
Output is correct |
78 |
Correct |
69 ms |
332 KB |
Output is correct |
79 |
Correct |
161 ms |
340 KB |
Output is correct |
80 |
Correct |
172 ms |
212 KB |
Output is correct |
81 |
Correct |
153 ms |
340 KB |
Output is correct |
82 |
Correct |
123 ms |
212 KB |
Output is correct |
83 |
Correct |
87 ms |
212 KB |
Output is correct |
84 |
Correct |
1010 ms |
460 KB |
Output is correct |
85 |
Correct |
1781 ms |
400 KB |
Output is correct |
86 |
Correct |
2 ms |
340 KB |
Output is correct |
87 |
Correct |
1 ms |
340 KB |
Output is correct |
88 |
Correct |
1850 ms |
396 KB |
Output is correct |
89 |
Correct |
358 ms |
404 KB |
Output is correct |
90 |
Correct |
1 ms |
340 KB |
Output is correct |