#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define ff first
#define ss second
#define Maxn 100009
#define ll long long
#define pb push_back
#define Inf 1000000009
#define ppb() pop_back()
#define pii pair <int , int>
#define mid(x, y) (x + y) / 2
#define all(x) x.begin(),x.end()
#define llInf 1000000000000000009
#define tr(i, c) for(__typeof(c).begin() i = (c).begin() ; i != (c).end() ; i++)
using namespace std;
using namespace __gnu_pbds;
typedef tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update> order;
int n, k;
pii x[Maxn];
pii y[Maxn];
ll ans = llInf;
vector <int> A;
bool calc (int x, int y, int z) {
x --, y --;
return ((x / z) % 2) ^ ((y / z) % 2);
}
int main () {
//freopen ("file.in", "r", stdin);
//freopen ("file.out", "w", stdout);
//srand ((unsigned) time ( NULL ));
//int randomNumber = rand() % 10 + 1;
scanf ("%d%d", &n, &k);
for (int i = 1; i <= k; i++)
scanf ("%d%d%d%d", &x[i].ff, &y[i].ff, &x[i].ss, &y[i].ss);
for (int i = 1; i < n; i++)
if (!(n % i))
A.pb (i);
for (auto j: A) {
ll v = j;
ll u = n / j;
ll sm1[2], sm2[2];
sm1[0] = sm2[1] = 0;
sm1[1] = (((u * u) + 1) / 2) * v * v;
sm2[0] = ((u * u) / 2) * v * v;
for (int i = 1; i <= k; i++) {
ll a = min ((j - (x[i].ff - 1) % j) % j, x[i].ss - x[i].ff + 1);
ll b = x[i].ss % j;
ll c = min ((j - (y[i].ff - 1) % j) % j, y[i].ss - y[i].ff + 1);
ll d = y[i].ss % j;
if (x[i].ff + a > x[i].ss)
b = 0;
if (y[i].ff + c > y[i].ss)
d = 0;
bool e = calc (x[i].ff, y[i].ff, j);
sm1[e] += a * c, sm2[e] += a * c;
e = calc (x[i].ff, y[i].ff + c, j);
ll t = (y[i].ss - y[i].ff - c + 1) / j;
ll r = (t + 1) / 2 * a * j;
sm1[e] += r, sm2[e] += r;
r = t / 2 * a * j;
sm1[!e] += r, sm2[!e] += r;
e = calc (x[i].ff, y[i].ss, j);
sm1[e] += a * d, sm2[e] += a * d;
ll xx1 = x[i].ff + a;
if (xx1 > x[i].ss)
continue;
e = calc (x[i].ss, y[i].ff, j);
sm1[e] += b * c, sm2[e] += b * c;
e = calc (x[i].ss, y[i].ff + c, j);
r = (t + 1) / 2 * b * j;
sm1[e] += r, sm2[e] += r;
r = t / 2 * b * j;
sm1[!e] += r, sm2[!e] += r;
e = calc (x[i].ss, y[i].ss, j);
sm1[e] += b * d, sm2[e] += b * d;
ll xx2 = x[i].ss - b;
if (xx1 > xx2)
continue;
e = calc (xx1, y[i].ff, j);
t = (xx2 - xx1 + 1) / j;
r = (t + 1) / 2 * c * j;
sm1[e] += r, sm2[e] += r;
r = t / 2 * c * j;
sm1[!e] += r, sm2[!e] += r;
ll yy1 = y[i].ff + c;
if (yy1 > y[i].ss)
continue;
e = calc (xx1, y[i].ss, j);
r = (t + 1) / 2 * d * j;
sm1[e] += r, sm2[e] += r;
r = t / 2 * d * j;
sm1[!e] += r, sm2[!e] += r;
ll yy2 = y[i].ss - d;
if (yy1 > yy2)
continue;
e = calc (xx1, yy1, j);
t = (yy2 - yy1 + 1) / j;
ll t1 = (xx2 - xx1 + 1) / j;
r = ((t + 1) / 2 * (t1 + 1) / 2 + t / 2 * t1 / 2) * j * j;
sm1[e] += r, sm2[e] += r;
r = (t / 2 * (t1 + 1) / 2 + (t + 1) / 2 * t1 / 2) * j * j;
sm1[!e] += r, sm2[!e] += r;
}
ans = min (ans, min (sm1[1] - sm1[0], sm2[0] - sm2[1]));
}
printf ("%lld\n", ans);
return 0;
}
Compilation message
chessboard.cpp: In function 'int main()':
chessboard.cpp:40:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf ("%d%d", &n, &k);
~~~~~~^~~~~~~~~~~~~~~~
chessboard.cpp:43:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf ("%d%d%d%d", &x[i].ff, &y[i].ff, &x[i].ss, &y[i].ss);
~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
484 KB |
Output is correct |
3 |
Correct |
2 ms |
484 KB |
Output is correct |
4 |
Correct |
2 ms |
484 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
676 KB |
Output is correct |
7 |
Correct |
2 ms |
684 KB |
Output is correct |
8 |
Correct |
2 ms |
684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
2276 KB |
Output is correct |
2 |
Correct |
19 ms |
2276 KB |
Output is correct |
3 |
Correct |
27 ms |
2276 KB |
Output is correct |
4 |
Correct |
28 ms |
2276 KB |
Output is correct |
5 |
Correct |
34 ms |
2276 KB |
Output is correct |
6 |
Correct |
24 ms |
2276 KB |
Output is correct |
7 |
Correct |
6 ms |
2276 KB |
Output is correct |
8 |
Correct |
29 ms |
2276 KB |
Output is correct |
9 |
Correct |
60 ms |
2652 KB |
Output is correct |
10 |
Correct |
33 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
3 ms |
2652 KB |
Output is correct |
4 |
Correct |
3 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2652 KB |
Output is correct |
6 |
Correct |
2 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2652 KB |
Output is correct |
10 |
Correct |
2 ms |
2652 KB |
Output is correct |
11 |
Correct |
2 ms |
2652 KB |
Output is correct |
12 |
Correct |
2 ms |
2652 KB |
Output is correct |
13 |
Correct |
3 ms |
2652 KB |
Output is correct |
14 |
Correct |
3 ms |
2652 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
3 ms |
2652 KB |
Output is correct |
4 |
Correct |
3 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2652 KB |
Output is correct |
6 |
Correct |
2 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2652 KB |
Output is correct |
10 |
Correct |
2 ms |
2652 KB |
Output is correct |
11 |
Correct |
2 ms |
2652 KB |
Output is correct |
12 |
Correct |
2 ms |
2652 KB |
Output is correct |
13 |
Correct |
3 ms |
2652 KB |
Output is correct |
14 |
Correct |
3 ms |
2652 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
16 |
Correct |
21 ms |
2652 KB |
Output is correct |
17 |
Correct |
49 ms |
2676 KB |
Output is correct |
18 |
Correct |
83 ms |
2900 KB |
Output is correct |
19 |
Correct |
189 ms |
2900 KB |
Output is correct |
20 |
Correct |
204 ms |
2932 KB |
Output is correct |
21 |
Correct |
47 ms |
2932 KB |
Output is correct |
22 |
Correct |
2 ms |
2932 KB |
Output is correct |
23 |
Correct |
41 ms |
2932 KB |
Output is correct |
24 |
Correct |
61 ms |
2932 KB |
Output is correct |
25 |
Correct |
9 ms |
2932 KB |
Output is correct |
26 |
Correct |
38 ms |
2932 KB |
Output is correct |
27 |
Correct |
58 ms |
2932 KB |
Output is correct |
28 |
Correct |
69 ms |
2932 KB |
Output is correct |
29 |
Correct |
21 ms |
2932 KB |
Output is correct |
30 |
Correct |
4 ms |
2932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
2276 KB |
Output is correct |
2 |
Correct |
19 ms |
2276 KB |
Output is correct |
3 |
Correct |
27 ms |
2276 KB |
Output is correct |
4 |
Correct |
28 ms |
2276 KB |
Output is correct |
5 |
Correct |
34 ms |
2276 KB |
Output is correct |
6 |
Correct |
24 ms |
2276 KB |
Output is correct |
7 |
Correct |
6 ms |
2276 KB |
Output is correct |
8 |
Correct |
29 ms |
2276 KB |
Output is correct |
9 |
Correct |
60 ms |
2652 KB |
Output is correct |
10 |
Correct |
33 ms |
2652 KB |
Output is correct |
11 |
Correct |
2 ms |
2652 KB |
Output is correct |
12 |
Correct |
2 ms |
2652 KB |
Output is correct |
13 |
Correct |
3 ms |
2652 KB |
Output is correct |
14 |
Correct |
3 ms |
2652 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
16 |
Correct |
2 ms |
2652 KB |
Output is correct |
17 |
Correct |
2 ms |
2652 KB |
Output is correct |
18 |
Correct |
2 ms |
2652 KB |
Output is correct |
19 |
Correct |
2 ms |
2652 KB |
Output is correct |
20 |
Correct |
2 ms |
2652 KB |
Output is correct |
21 |
Correct |
2 ms |
2652 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
3 ms |
2652 KB |
Output is correct |
24 |
Correct |
3 ms |
2652 KB |
Output is correct |
25 |
Correct |
2 ms |
2652 KB |
Output is correct |
26 |
Correct |
21 ms |
2652 KB |
Output is correct |
27 |
Correct |
49 ms |
2676 KB |
Output is correct |
28 |
Correct |
83 ms |
2900 KB |
Output is correct |
29 |
Correct |
189 ms |
2900 KB |
Output is correct |
30 |
Correct |
204 ms |
2932 KB |
Output is correct |
31 |
Correct |
47 ms |
2932 KB |
Output is correct |
32 |
Correct |
2 ms |
2932 KB |
Output is correct |
33 |
Correct |
41 ms |
2932 KB |
Output is correct |
34 |
Correct |
61 ms |
2932 KB |
Output is correct |
35 |
Correct |
9 ms |
2932 KB |
Output is correct |
36 |
Correct |
38 ms |
2932 KB |
Output is correct |
37 |
Correct |
58 ms |
2932 KB |
Output is correct |
38 |
Correct |
69 ms |
2932 KB |
Output is correct |
39 |
Correct |
21 ms |
2932 KB |
Output is correct |
40 |
Correct |
4 ms |
2932 KB |
Output is correct |
41 |
Correct |
161 ms |
2932 KB |
Output is correct |
42 |
Correct |
73 ms |
2932 KB |
Output is correct |
43 |
Correct |
108 ms |
2932 KB |
Output is correct |
44 |
Correct |
73 ms |
2932 KB |
Output is correct |
45 |
Correct |
67 ms |
3060 KB |
Output is correct |
46 |
Correct |
182 ms |
3060 KB |
Output is correct |
47 |
Correct |
56 ms |
3060 KB |
Output is correct |
48 |
Correct |
86 ms |
3060 KB |
Output is correct |
49 |
Correct |
64 ms |
3060 KB |
Output is correct |
50 |
Correct |
699 ms |
3060 KB |
Output is correct |
51 |
Correct |
726 ms |
3120 KB |
Output is correct |
52 |
Correct |
686 ms |
3120 KB |
Output is correct |
53 |
Correct |
737 ms |
3120 KB |
Output is correct |
54 |
Correct |
679 ms |
3120 KB |
Output is correct |
55 |
Correct |
757 ms |
3248 KB |
Output is correct |
56 |
Correct |
641 ms |
3248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
484 KB |
Output is correct |
3 |
Correct |
2 ms |
484 KB |
Output is correct |
4 |
Correct |
2 ms |
484 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
676 KB |
Output is correct |
7 |
Correct |
2 ms |
684 KB |
Output is correct |
8 |
Correct |
2 ms |
684 KB |
Output is correct |
9 |
Correct |
42 ms |
2276 KB |
Output is correct |
10 |
Correct |
19 ms |
2276 KB |
Output is correct |
11 |
Correct |
27 ms |
2276 KB |
Output is correct |
12 |
Correct |
28 ms |
2276 KB |
Output is correct |
13 |
Correct |
34 ms |
2276 KB |
Output is correct |
14 |
Correct |
24 ms |
2276 KB |
Output is correct |
15 |
Correct |
6 ms |
2276 KB |
Output is correct |
16 |
Correct |
29 ms |
2276 KB |
Output is correct |
17 |
Correct |
60 ms |
2652 KB |
Output is correct |
18 |
Correct |
33 ms |
2652 KB |
Output is correct |
19 |
Correct |
2 ms |
2652 KB |
Output is correct |
20 |
Correct |
2 ms |
2652 KB |
Output is correct |
21 |
Correct |
3 ms |
2652 KB |
Output is correct |
22 |
Correct |
3 ms |
2652 KB |
Output is correct |
23 |
Correct |
2 ms |
2652 KB |
Output is correct |
24 |
Correct |
2 ms |
2652 KB |
Output is correct |
25 |
Correct |
2 ms |
2652 KB |
Output is correct |
26 |
Correct |
2 ms |
2652 KB |
Output is correct |
27 |
Correct |
2 ms |
2652 KB |
Output is correct |
28 |
Correct |
2 ms |
2652 KB |
Output is correct |
29 |
Correct |
2 ms |
2652 KB |
Output is correct |
30 |
Correct |
2 ms |
2652 KB |
Output is correct |
31 |
Correct |
3 ms |
2652 KB |
Output is correct |
32 |
Correct |
3 ms |
2652 KB |
Output is correct |
33 |
Correct |
2 ms |
2652 KB |
Output is correct |
34 |
Correct |
21 ms |
2652 KB |
Output is correct |
35 |
Correct |
49 ms |
2676 KB |
Output is correct |
36 |
Correct |
83 ms |
2900 KB |
Output is correct |
37 |
Correct |
189 ms |
2900 KB |
Output is correct |
38 |
Correct |
204 ms |
2932 KB |
Output is correct |
39 |
Correct |
47 ms |
2932 KB |
Output is correct |
40 |
Correct |
2 ms |
2932 KB |
Output is correct |
41 |
Correct |
41 ms |
2932 KB |
Output is correct |
42 |
Correct |
61 ms |
2932 KB |
Output is correct |
43 |
Correct |
9 ms |
2932 KB |
Output is correct |
44 |
Correct |
38 ms |
2932 KB |
Output is correct |
45 |
Correct |
58 ms |
2932 KB |
Output is correct |
46 |
Correct |
69 ms |
2932 KB |
Output is correct |
47 |
Correct |
21 ms |
2932 KB |
Output is correct |
48 |
Correct |
4 ms |
2932 KB |
Output is correct |
49 |
Correct |
161 ms |
2932 KB |
Output is correct |
50 |
Correct |
73 ms |
2932 KB |
Output is correct |
51 |
Correct |
108 ms |
2932 KB |
Output is correct |
52 |
Correct |
73 ms |
2932 KB |
Output is correct |
53 |
Correct |
67 ms |
3060 KB |
Output is correct |
54 |
Correct |
182 ms |
3060 KB |
Output is correct |
55 |
Correct |
56 ms |
3060 KB |
Output is correct |
56 |
Correct |
86 ms |
3060 KB |
Output is correct |
57 |
Correct |
64 ms |
3060 KB |
Output is correct |
58 |
Correct |
699 ms |
3060 KB |
Output is correct |
59 |
Correct |
726 ms |
3120 KB |
Output is correct |
60 |
Correct |
686 ms |
3120 KB |
Output is correct |
61 |
Correct |
737 ms |
3120 KB |
Output is correct |
62 |
Correct |
679 ms |
3120 KB |
Output is correct |
63 |
Correct |
757 ms |
3248 KB |
Output is correct |
64 |
Correct |
641 ms |
3248 KB |
Output is correct |
65 |
Correct |
2 ms |
3248 KB |
Output is correct |
66 |
Correct |
2 ms |
3248 KB |
Output is correct |
67 |
Correct |
798 ms |
3248 KB |
Output is correct |
68 |
Correct |
795 ms |
3248 KB |
Output is correct |
69 |
Correct |
708 ms |
3248 KB |
Output is correct |
70 |
Correct |
742 ms |
3248 KB |
Output is correct |
71 |
Correct |
750 ms |
3248 KB |
Output is correct |
72 |
Correct |
744 ms |
3248 KB |
Output is correct |
73 |
Correct |
749 ms |
3248 KB |
Output is correct |
74 |
Correct |
795 ms |
3248 KB |
Output is correct |
75 |
Correct |
730 ms |
3248 KB |
Output is correct |
76 |
Correct |
783 ms |
3248 KB |
Output is correct |
77 |
Correct |
152 ms |
3248 KB |
Output is correct |
78 |
Correct |
71 ms |
3248 KB |
Output is correct |
79 |
Correct |
110 ms |
5036 KB |
Output is correct |
80 |
Correct |
112 ms |
7072 KB |
Output is correct |
81 |
Correct |
109 ms |
8984 KB |
Output is correct |
82 |
Correct |
94 ms |
11348 KB |
Output is correct |
83 |
Correct |
83 ms |
13348 KB |
Output is correct |
84 |
Correct |
475 ms |
15912 KB |
Output is correct |
85 |
Correct |
841 ms |
18292 KB |
Output is correct |
86 |
Correct |
3 ms |
18292 KB |
Output is correct |
87 |
Incorrect |
3 ms |
18292 KB |
Output isn't correct |
88 |
Halted |
0 ms |
0 KB |
- |