#include <bits/stdc++.h>
#define pb push_back
using namespace std;
using ll = long long;
using PII = pair <int, int>;
const ll INF = 1e18;
const int N = 1e5 + 5;
ll n, m, ans = INF;
vector <int> divs;
ll dp[N];
void add(int num) {
if (num == n) return;
divs.pb(num);
ll len = n / num;
dp[num] = (len * len + 1) / 2 * num * num;
}
int getCnt(int pos, int len) {
int full = pos - (pos % len);
int ret = (full / len + 1) / 2 * len;
if ((full / len) % 2 == 0) ret += pos - full;
return ret;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
cin >> n >> m;
for (int i = 1; i * i <= n; i++) {
if (n % i != 0) continue;
add(i);
if (i * i != n) add(n / i);
}
for (int i = 0; i < m; i++) {
int a1, b1, a2, b2; cin >> a1 >> b1 >> a2 >> b2;
for (auto& num : divs) {
int black, white, Black, White;
black = getCnt(a2, num) - getCnt(a1-1, num);
white = a2 - a1 + 1 - black;
Black = getCnt(b2, num) - getCnt(b1-1, num);
White = b2 - b1 + 1 - Black;
ll cur = black - white, cur1 = -cur;
dp[num] -= cur * Black + cur1 * White;
// cout << a1 << ' ' << b1 << ' ' << num << " : " << cur * Black + cur1 * White << '\n';
}
}
for (auto& num : divs) {
// cout << num << ": " << dp[num] << '\n';
ans = min(ans, min(dp[num], n * n - dp[num]));
}
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
308 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
1692 KB |
Output is correct |
2 |
Correct |
7 ms |
656 KB |
Output is correct |
3 |
Correct |
14 ms |
1192 KB |
Output is correct |
4 |
Correct |
15 ms |
1096 KB |
Output is correct |
5 |
Correct |
19 ms |
1604 KB |
Output is correct |
6 |
Correct |
12 ms |
1056 KB |
Output is correct |
7 |
Correct |
3 ms |
464 KB |
Output is correct |
8 |
Correct |
14 ms |
1100 KB |
Output is correct |
9 |
Correct |
37 ms |
2340 KB |
Output is correct |
10 |
Correct |
21 ms |
1476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
10 ms |
696 KB |
Output is correct |
17 |
Correct |
26 ms |
1568 KB |
Output is correct |
18 |
Correct |
38 ms |
1900 KB |
Output is correct |
19 |
Correct |
129 ms |
1736 KB |
Output is correct |
20 |
Correct |
148 ms |
1828 KB |
Output is correct |
21 |
Correct |
24 ms |
1600 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
23 ms |
940 KB |
Output is correct |
24 |
Correct |
33 ms |
1712 KB |
Output is correct |
25 |
Correct |
8 ms |
460 KB |
Output is correct |
26 |
Correct |
24 ms |
1184 KB |
Output is correct |
27 |
Correct |
34 ms |
1328 KB |
Output is correct |
28 |
Correct |
38 ms |
1824 KB |
Output is correct |
29 |
Correct |
10 ms |
844 KB |
Output is correct |
30 |
Correct |
2 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
1692 KB |
Output is correct |
2 |
Correct |
7 ms |
656 KB |
Output is correct |
3 |
Correct |
14 ms |
1192 KB |
Output is correct |
4 |
Correct |
15 ms |
1096 KB |
Output is correct |
5 |
Correct |
19 ms |
1604 KB |
Output is correct |
6 |
Correct |
12 ms |
1056 KB |
Output is correct |
7 |
Correct |
3 ms |
464 KB |
Output is correct |
8 |
Correct |
14 ms |
1100 KB |
Output is correct |
9 |
Correct |
37 ms |
2340 KB |
Output is correct |
10 |
Correct |
21 ms |
1476 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
10 ms |
696 KB |
Output is correct |
27 |
Correct |
26 ms |
1568 KB |
Output is correct |
28 |
Correct |
38 ms |
1900 KB |
Output is correct |
29 |
Correct |
129 ms |
1736 KB |
Output is correct |
30 |
Correct |
148 ms |
1828 KB |
Output is correct |
31 |
Correct |
24 ms |
1600 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
23 ms |
940 KB |
Output is correct |
34 |
Correct |
33 ms |
1712 KB |
Output is correct |
35 |
Correct |
8 ms |
460 KB |
Output is correct |
36 |
Correct |
24 ms |
1184 KB |
Output is correct |
37 |
Correct |
34 ms |
1328 KB |
Output is correct |
38 |
Correct |
38 ms |
1824 KB |
Output is correct |
39 |
Correct |
10 ms |
844 KB |
Output is correct |
40 |
Correct |
2 ms |
460 KB |
Output is correct |
41 |
Correct |
111 ms |
2244 KB |
Output is correct |
42 |
Correct |
44 ms |
2472 KB |
Output is correct |
43 |
Correct |
72 ms |
2224 KB |
Output is correct |
44 |
Correct |
42 ms |
2376 KB |
Output is correct |
45 |
Correct |
33 ms |
2596 KB |
Output is correct |
46 |
Correct |
129 ms |
2496 KB |
Output is correct |
47 |
Correct |
29 ms |
2236 KB |
Output is correct |
48 |
Correct |
67 ms |
2348 KB |
Output is correct |
49 |
Correct |
42 ms |
2212 KB |
Output is correct |
50 |
Correct |
555 ms |
2676 KB |
Output is correct |
51 |
Correct |
617 ms |
2696 KB |
Output is correct |
52 |
Correct |
583 ms |
2424 KB |
Output is correct |
53 |
Correct |
587 ms |
2676 KB |
Output is correct |
54 |
Correct |
552 ms |
2416 KB |
Output is correct |
55 |
Correct |
636 ms |
2676 KB |
Output is correct |
56 |
Correct |
530 ms |
2424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
308 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
23 ms |
1692 KB |
Output is correct |
10 |
Correct |
7 ms |
656 KB |
Output is correct |
11 |
Correct |
14 ms |
1192 KB |
Output is correct |
12 |
Correct |
15 ms |
1096 KB |
Output is correct |
13 |
Correct |
19 ms |
1604 KB |
Output is correct |
14 |
Correct |
12 ms |
1056 KB |
Output is correct |
15 |
Correct |
3 ms |
464 KB |
Output is correct |
16 |
Correct |
14 ms |
1100 KB |
Output is correct |
17 |
Correct |
37 ms |
2340 KB |
Output is correct |
18 |
Correct |
21 ms |
1476 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
324 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
2 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
0 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
0 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
10 ms |
696 KB |
Output is correct |
35 |
Correct |
26 ms |
1568 KB |
Output is correct |
36 |
Correct |
38 ms |
1900 KB |
Output is correct |
37 |
Correct |
129 ms |
1736 KB |
Output is correct |
38 |
Correct |
148 ms |
1828 KB |
Output is correct |
39 |
Correct |
24 ms |
1600 KB |
Output is correct |
40 |
Correct |
1 ms |
204 KB |
Output is correct |
41 |
Correct |
23 ms |
940 KB |
Output is correct |
42 |
Correct |
33 ms |
1712 KB |
Output is correct |
43 |
Correct |
8 ms |
460 KB |
Output is correct |
44 |
Correct |
24 ms |
1184 KB |
Output is correct |
45 |
Correct |
34 ms |
1328 KB |
Output is correct |
46 |
Correct |
38 ms |
1824 KB |
Output is correct |
47 |
Correct |
10 ms |
844 KB |
Output is correct |
48 |
Correct |
2 ms |
460 KB |
Output is correct |
49 |
Correct |
111 ms |
2244 KB |
Output is correct |
50 |
Correct |
44 ms |
2472 KB |
Output is correct |
51 |
Correct |
72 ms |
2224 KB |
Output is correct |
52 |
Correct |
42 ms |
2376 KB |
Output is correct |
53 |
Correct |
33 ms |
2596 KB |
Output is correct |
54 |
Correct |
129 ms |
2496 KB |
Output is correct |
55 |
Correct |
29 ms |
2236 KB |
Output is correct |
56 |
Correct |
67 ms |
2348 KB |
Output is correct |
57 |
Correct |
42 ms |
2212 KB |
Output is correct |
58 |
Correct |
555 ms |
2676 KB |
Output is correct |
59 |
Correct |
617 ms |
2696 KB |
Output is correct |
60 |
Correct |
583 ms |
2424 KB |
Output is correct |
61 |
Correct |
587 ms |
2676 KB |
Output is correct |
62 |
Correct |
552 ms |
2416 KB |
Output is correct |
63 |
Correct |
636 ms |
2676 KB |
Output is correct |
64 |
Correct |
530 ms |
2424 KB |
Output is correct |
65 |
Correct |
1 ms |
204 KB |
Output is correct |
66 |
Correct |
0 ms |
204 KB |
Output is correct |
67 |
Correct |
599 ms |
2552 KB |
Output is correct |
68 |
Correct |
584 ms |
2548 KB |
Output is correct |
69 |
Correct |
513 ms |
2304 KB |
Output is correct |
70 |
Correct |
594 ms |
2644 KB |
Output is correct |
71 |
Correct |
577 ms |
2416 KB |
Output is correct |
72 |
Correct |
554 ms |
2420 KB |
Output is correct |
73 |
Correct |
546 ms |
2416 KB |
Output is correct |
74 |
Correct |
568 ms |
2548 KB |
Output is correct |
75 |
Correct |
549 ms |
2420 KB |
Output is correct |
76 |
Correct |
592 ms |
2544 KB |
Output is correct |
77 |
Correct |
104 ms |
2616 KB |
Output is correct |
78 |
Correct |
41 ms |
2340 KB |
Output is correct |
79 |
Correct |
71 ms |
2228 KB |
Output is correct |
80 |
Correct |
76 ms |
2356 KB |
Output is correct |
81 |
Correct |
80 ms |
2228 KB |
Output is correct |
82 |
Correct |
59 ms |
2480 KB |
Output is correct |
83 |
Correct |
45 ms |
2340 KB |
Output is correct |
84 |
Correct |
364 ms |
2760 KB |
Output is correct |
85 |
Correct |
609 ms |
2800 KB |
Output is correct |
86 |
Correct |
1 ms |
324 KB |
Output is correct |
87 |
Correct |
0 ms |
332 KB |
Output is correct |
88 |
Correct |
622 ms |
2676 KB |
Output is correct |
89 |
Correct |
123 ms |
836 KB |
Output is correct |
90 |
Correct |
1 ms |
448 KB |
Output is correct |