#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 |
484 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
712 KB |
Output is correct |
8 |
Correct |
2 ms |
712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
2868 KB |
Output is correct |
2 |
Correct |
16 ms |
2868 KB |
Output is correct |
3 |
Correct |
27 ms |
2868 KB |
Output is correct |
4 |
Correct |
41 ms |
2868 KB |
Output is correct |
5 |
Correct |
34 ms |
2908 KB |
Output is correct |
6 |
Correct |
27 ms |
2908 KB |
Output is correct |
7 |
Correct |
6 ms |
2908 KB |
Output is correct |
8 |
Correct |
29 ms |
2908 KB |
Output is correct |
9 |
Correct |
67 ms |
3436 KB |
Output is correct |
10 |
Correct |
32 ms |
3436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3436 KB |
Output is correct |
2 |
Correct |
2 ms |
3436 KB |
Output is correct |
3 |
Correct |
2 ms |
3436 KB |
Output is correct |
4 |
Correct |
4 ms |
3436 KB |
Output is correct |
5 |
Correct |
3 ms |
3436 KB |
Output is correct |
6 |
Correct |
2 ms |
3436 KB |
Output is correct |
7 |
Correct |
3 ms |
3436 KB |
Output is correct |
8 |
Correct |
3 ms |
3436 KB |
Output is correct |
9 |
Correct |
2 ms |
3436 KB |
Output is correct |
10 |
Correct |
2 ms |
3436 KB |
Output is correct |
11 |
Correct |
2 ms |
3436 KB |
Output is correct |
12 |
Correct |
3 ms |
3436 KB |
Output is correct |
13 |
Correct |
3 ms |
3436 KB |
Output is correct |
14 |
Correct |
3 ms |
3436 KB |
Output is correct |
15 |
Correct |
3 ms |
3436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3436 KB |
Output is correct |
2 |
Correct |
2 ms |
3436 KB |
Output is correct |
3 |
Correct |
2 ms |
3436 KB |
Output is correct |
4 |
Correct |
4 ms |
3436 KB |
Output is correct |
5 |
Correct |
3 ms |
3436 KB |
Output is correct |
6 |
Correct |
2 ms |
3436 KB |
Output is correct |
7 |
Correct |
3 ms |
3436 KB |
Output is correct |
8 |
Correct |
3 ms |
3436 KB |
Output is correct |
9 |
Correct |
2 ms |
3436 KB |
Output is correct |
10 |
Correct |
2 ms |
3436 KB |
Output is correct |
11 |
Correct |
2 ms |
3436 KB |
Output is correct |
12 |
Correct |
3 ms |
3436 KB |
Output is correct |
13 |
Correct |
3 ms |
3436 KB |
Output is correct |
14 |
Correct |
3 ms |
3436 KB |
Output is correct |
15 |
Correct |
3 ms |
3436 KB |
Output is correct |
16 |
Correct |
20 ms |
3436 KB |
Output is correct |
17 |
Correct |
46 ms |
3436 KB |
Output is correct |
18 |
Correct |
67 ms |
3688 KB |
Output is correct |
19 |
Correct |
183 ms |
3688 KB |
Output is correct |
20 |
Correct |
201 ms |
3688 KB |
Output is correct |
21 |
Correct |
44 ms |
3688 KB |
Output is correct |
22 |
Correct |
3 ms |
3688 KB |
Output is correct |
23 |
Correct |
38 ms |
3688 KB |
Output is correct |
24 |
Correct |
59 ms |
3688 KB |
Output is correct |
25 |
Correct |
9 ms |
3688 KB |
Output is correct |
26 |
Correct |
37 ms |
3688 KB |
Output is correct |
27 |
Correct |
52 ms |
3688 KB |
Output is correct |
28 |
Correct |
63 ms |
3692 KB |
Output is correct |
29 |
Correct |
21 ms |
3692 KB |
Output is correct |
30 |
Correct |
4 ms |
3692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
2868 KB |
Output is correct |
2 |
Correct |
16 ms |
2868 KB |
Output is correct |
3 |
Correct |
27 ms |
2868 KB |
Output is correct |
4 |
Correct |
41 ms |
2868 KB |
Output is correct |
5 |
Correct |
34 ms |
2908 KB |
Output is correct |
6 |
Correct |
27 ms |
2908 KB |
Output is correct |
7 |
Correct |
6 ms |
2908 KB |
Output is correct |
8 |
Correct |
29 ms |
2908 KB |
Output is correct |
9 |
Correct |
67 ms |
3436 KB |
Output is correct |
10 |
Correct |
32 ms |
3436 KB |
Output is correct |
11 |
Correct |
2 ms |
3436 KB |
Output is correct |
12 |
Correct |
2 ms |
3436 KB |
Output is correct |
13 |
Correct |
2 ms |
3436 KB |
Output is correct |
14 |
Correct |
4 ms |
3436 KB |
Output is correct |
15 |
Correct |
3 ms |
3436 KB |
Output is correct |
16 |
Correct |
2 ms |
3436 KB |
Output is correct |
17 |
Correct |
3 ms |
3436 KB |
Output is correct |
18 |
Correct |
3 ms |
3436 KB |
Output is correct |
19 |
Correct |
2 ms |
3436 KB |
Output is correct |
20 |
Correct |
2 ms |
3436 KB |
Output is correct |
21 |
Correct |
2 ms |
3436 KB |
Output is correct |
22 |
Correct |
3 ms |
3436 KB |
Output is correct |
23 |
Correct |
3 ms |
3436 KB |
Output is correct |
24 |
Correct |
3 ms |
3436 KB |
Output is correct |
25 |
Correct |
3 ms |
3436 KB |
Output is correct |
26 |
Correct |
20 ms |
3436 KB |
Output is correct |
27 |
Correct |
46 ms |
3436 KB |
Output is correct |
28 |
Correct |
67 ms |
3688 KB |
Output is correct |
29 |
Correct |
183 ms |
3688 KB |
Output is correct |
30 |
Correct |
201 ms |
3688 KB |
Output is correct |
31 |
Correct |
44 ms |
3688 KB |
Output is correct |
32 |
Correct |
3 ms |
3688 KB |
Output is correct |
33 |
Correct |
38 ms |
3688 KB |
Output is correct |
34 |
Correct |
59 ms |
3688 KB |
Output is correct |
35 |
Correct |
9 ms |
3688 KB |
Output is correct |
36 |
Correct |
37 ms |
3688 KB |
Output is correct |
37 |
Correct |
52 ms |
3688 KB |
Output is correct |
38 |
Correct |
63 ms |
3692 KB |
Output is correct |
39 |
Correct |
21 ms |
3692 KB |
Output is correct |
40 |
Correct |
4 ms |
3692 KB |
Output is correct |
41 |
Correct |
159 ms |
3692 KB |
Output is correct |
42 |
Correct |
75 ms |
3692 KB |
Output is correct |
43 |
Correct |
105 ms |
3692 KB |
Output is correct |
44 |
Correct |
69 ms |
3692 KB |
Output is correct |
45 |
Correct |
111 ms |
3692 KB |
Output is correct |
46 |
Correct |
177 ms |
3948 KB |
Output is correct |
47 |
Correct |
78 ms |
3948 KB |
Output is correct |
48 |
Correct |
83 ms |
3948 KB |
Output is correct |
49 |
Correct |
64 ms |
3948 KB |
Output is correct |
50 |
Correct |
670 ms |
3948 KB |
Output is correct |
51 |
Correct |
728 ms |
3948 KB |
Output is correct |
52 |
Correct |
671 ms |
3948 KB |
Output is correct |
53 |
Correct |
711 ms |
3948 KB |
Output is correct |
54 |
Correct |
671 ms |
3948 KB |
Output is correct |
55 |
Correct |
731 ms |
3964 KB |
Output is correct |
56 |
Correct |
651 ms |
3964 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 |
484 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
712 KB |
Output is correct |
8 |
Correct |
2 ms |
712 KB |
Output is correct |
9 |
Correct |
40 ms |
2868 KB |
Output is correct |
10 |
Correct |
16 ms |
2868 KB |
Output is correct |
11 |
Correct |
27 ms |
2868 KB |
Output is correct |
12 |
Correct |
41 ms |
2868 KB |
Output is correct |
13 |
Correct |
34 ms |
2908 KB |
Output is correct |
14 |
Correct |
27 ms |
2908 KB |
Output is correct |
15 |
Correct |
6 ms |
2908 KB |
Output is correct |
16 |
Correct |
29 ms |
2908 KB |
Output is correct |
17 |
Correct |
67 ms |
3436 KB |
Output is correct |
18 |
Correct |
32 ms |
3436 KB |
Output is correct |
19 |
Correct |
2 ms |
3436 KB |
Output is correct |
20 |
Correct |
2 ms |
3436 KB |
Output is correct |
21 |
Correct |
2 ms |
3436 KB |
Output is correct |
22 |
Correct |
4 ms |
3436 KB |
Output is correct |
23 |
Correct |
3 ms |
3436 KB |
Output is correct |
24 |
Correct |
2 ms |
3436 KB |
Output is correct |
25 |
Correct |
3 ms |
3436 KB |
Output is correct |
26 |
Correct |
3 ms |
3436 KB |
Output is correct |
27 |
Correct |
2 ms |
3436 KB |
Output is correct |
28 |
Correct |
2 ms |
3436 KB |
Output is correct |
29 |
Correct |
2 ms |
3436 KB |
Output is correct |
30 |
Correct |
3 ms |
3436 KB |
Output is correct |
31 |
Correct |
3 ms |
3436 KB |
Output is correct |
32 |
Correct |
3 ms |
3436 KB |
Output is correct |
33 |
Correct |
3 ms |
3436 KB |
Output is correct |
34 |
Correct |
20 ms |
3436 KB |
Output is correct |
35 |
Correct |
46 ms |
3436 KB |
Output is correct |
36 |
Correct |
67 ms |
3688 KB |
Output is correct |
37 |
Correct |
183 ms |
3688 KB |
Output is correct |
38 |
Correct |
201 ms |
3688 KB |
Output is correct |
39 |
Correct |
44 ms |
3688 KB |
Output is correct |
40 |
Correct |
3 ms |
3688 KB |
Output is correct |
41 |
Correct |
38 ms |
3688 KB |
Output is correct |
42 |
Correct |
59 ms |
3688 KB |
Output is correct |
43 |
Correct |
9 ms |
3688 KB |
Output is correct |
44 |
Correct |
37 ms |
3688 KB |
Output is correct |
45 |
Correct |
52 ms |
3688 KB |
Output is correct |
46 |
Correct |
63 ms |
3692 KB |
Output is correct |
47 |
Correct |
21 ms |
3692 KB |
Output is correct |
48 |
Correct |
4 ms |
3692 KB |
Output is correct |
49 |
Correct |
159 ms |
3692 KB |
Output is correct |
50 |
Correct |
75 ms |
3692 KB |
Output is correct |
51 |
Correct |
105 ms |
3692 KB |
Output is correct |
52 |
Correct |
69 ms |
3692 KB |
Output is correct |
53 |
Correct |
111 ms |
3692 KB |
Output is correct |
54 |
Correct |
177 ms |
3948 KB |
Output is correct |
55 |
Correct |
78 ms |
3948 KB |
Output is correct |
56 |
Correct |
83 ms |
3948 KB |
Output is correct |
57 |
Correct |
64 ms |
3948 KB |
Output is correct |
58 |
Correct |
670 ms |
3948 KB |
Output is correct |
59 |
Correct |
728 ms |
3948 KB |
Output is correct |
60 |
Correct |
671 ms |
3948 KB |
Output is correct |
61 |
Correct |
711 ms |
3948 KB |
Output is correct |
62 |
Correct |
671 ms |
3948 KB |
Output is correct |
63 |
Correct |
731 ms |
3964 KB |
Output is correct |
64 |
Correct |
651 ms |
3964 KB |
Output is correct |
65 |
Correct |
2 ms |
3964 KB |
Output is correct |
66 |
Correct |
2 ms |
3964 KB |
Output is correct |
67 |
Correct |
786 ms |
3964 KB |
Output is correct |
68 |
Correct |
771 ms |
3964 KB |
Output is correct |
69 |
Correct |
669 ms |
3964 KB |
Output is correct |
70 |
Correct |
733 ms |
3964 KB |
Output is correct |
71 |
Correct |
717 ms |
3964 KB |
Output is correct |
72 |
Correct |
698 ms |
3964 KB |
Output is correct |
73 |
Correct |
708 ms |
3964 KB |
Output is correct |
74 |
Correct |
779 ms |
3964 KB |
Output is correct |
75 |
Correct |
720 ms |
3964 KB |
Output is correct |
76 |
Correct |
806 ms |
3964 KB |
Output is correct |
77 |
Correct |
146 ms |
3964 KB |
Output is correct |
78 |
Correct |
73 ms |
3964 KB |
Output is correct |
79 |
Correct |
107 ms |
3964 KB |
Output is correct |
80 |
Correct |
112 ms |
3964 KB |
Output is correct |
81 |
Correct |
108 ms |
3964 KB |
Output is correct |
82 |
Correct |
94 ms |
3964 KB |
Output is correct |
83 |
Correct |
84 ms |
3964 KB |
Output is correct |
84 |
Correct |
467 ms |
3964 KB |
Output is correct |
85 |
Correct |
805 ms |
4048 KB |
Output is correct |
86 |
Correct |
3 ms |
4048 KB |
Output is correct |
87 |
Correct |
2 ms |
4048 KB |
Output is correct |
88 |
Correct |
812 ms |
6404 KB |
Output is correct |
89 |
Correct |
173 ms |
6404 KB |
Output is correct |
90 |
Correct |
3 ms |
6404 KB |
Output is correct |