#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define ll long long
#define ld long double
#define all(_v) _v.begin(), _v.end()
#define sz(_v) (int)_v.size()
#define pii pair <int, int>
#define pll pair <ll, ll>
#define veci vector <int>
#define vecll vector <ll>
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
const double PI = 3.1415926535897932384626433832795;
const double eps = 1e-9;
const int MOD1 = 1e9 + 7;
const int MOD2 = 998244353;
const int MAXN = 2e5 + 10;
ll n;
ll cnt[MAXN];
ll get(ll r, ll k) {
ll nr = r - r % k;
ll bl = nr / k;
ll cnt = (bl + 1) / 2 * k;
if(bl % 2 == 0) cnt += r - nr;
return cnt;
}
void solve() {
int k;
cin >> n >> k;
vecll dv;
dv.pb(1);
for(ll i = 2; i * i <= n; ++i) {
if(n % i == 0) {
dv.pb(i);
if(i * i != n) dv.pb(n / i);
}
}
sort(all(dv));
for(auto i : dv) {
ll cnt1 = get(n, i);
cnt[i] = cnt1 * i * (((n / i) + 1) / 2) + (n - cnt1) * i * (n / i - (((n / i) + 1) / 2));
}
for(int rect = 1; rect <= k; ++rect) {
ll x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
for(auto i : dv) {
ll row_cnt[2];
row_cnt[1] = get(x2, i) - get(x1 - 1, i);
row_cnt[0] = x2 - x1 + 1 - row_cnt[1];
ll col_cnt[2];
col_cnt[1] = get(y2, i) - get(y1 - 1, i);
col_cnt[0] = y2 - y1 + 1 - col_cnt[1];
cnt[i] -= row_cnt[1] * col_cnt[1];
cnt[i] -= row_cnt[0] * col_cnt[0];
cnt[i] += row_cnt[0] * col_cnt[1];
cnt[i] += row_cnt[1] * col_cnt[0];
}
}
ll ans = 4e18;
for(auto i : dv) ans = min(ans, min(cnt[i], n * n - cnt[i]));
cout << ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int CNT_TESTS = 1;
///cin >> CNT_TESTS;
for(int NUMCASE = 1; NUMCASE <= CNT_TESTS; ++NUMCASE) {
solve();
if(NUMCASE != CNT_TESTS) cout << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
320 KB |
Output is correct |
2 |
Correct |
5 ms |
204 KB |
Output is correct |
3 |
Correct |
15 ms |
324 KB |
Output is correct |
4 |
Correct |
18 ms |
324 KB |
Output is correct |
5 |
Correct |
17 ms |
320 KB |
Output is correct |
6 |
Correct |
11 ms |
320 KB |
Output is correct |
7 |
Correct |
3 ms |
204 KB |
Output is correct |
8 |
Correct |
11 ms |
204 KB |
Output is correct |
9 |
Correct |
28 ms |
204 KB |
Output is correct |
10 |
Correct |
16 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
10 ms |
204 KB |
Output is correct |
17 |
Correct |
22 ms |
320 KB |
Output is correct |
18 |
Correct |
35 ms |
204 KB |
Output is correct |
19 |
Correct |
117 ms |
304 KB |
Output is correct |
20 |
Correct |
146 ms |
304 KB |
Output is correct |
21 |
Correct |
23 ms |
312 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
22 ms |
204 KB |
Output is correct |
24 |
Correct |
34 ms |
204 KB |
Output is correct |
25 |
Correct |
5 ms |
204 KB |
Output is correct |
26 |
Correct |
29 ms |
204 KB |
Output is correct |
27 |
Correct |
29 ms |
308 KB |
Output is correct |
28 |
Correct |
34 ms |
324 KB |
Output is correct |
29 |
Correct |
9 ms |
324 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
320 KB |
Output is correct |
2 |
Correct |
5 ms |
204 KB |
Output is correct |
3 |
Correct |
15 ms |
324 KB |
Output is correct |
4 |
Correct |
18 ms |
324 KB |
Output is correct |
5 |
Correct |
17 ms |
320 KB |
Output is correct |
6 |
Correct |
11 ms |
320 KB |
Output is correct |
7 |
Correct |
3 ms |
204 KB |
Output is correct |
8 |
Correct |
11 ms |
204 KB |
Output is correct |
9 |
Correct |
28 ms |
204 KB |
Output is correct |
10 |
Correct |
16 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
10 ms |
204 KB |
Output is correct |
27 |
Correct |
22 ms |
320 KB |
Output is correct |
28 |
Correct |
35 ms |
204 KB |
Output is correct |
29 |
Correct |
117 ms |
304 KB |
Output is correct |
30 |
Correct |
146 ms |
304 KB |
Output is correct |
31 |
Correct |
23 ms |
312 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
22 ms |
204 KB |
Output is correct |
34 |
Correct |
34 ms |
204 KB |
Output is correct |
35 |
Correct |
5 ms |
204 KB |
Output is correct |
36 |
Correct |
29 ms |
204 KB |
Output is correct |
37 |
Correct |
29 ms |
308 KB |
Output is correct |
38 |
Correct |
34 ms |
324 KB |
Output is correct |
39 |
Correct |
9 ms |
324 KB |
Output is correct |
40 |
Correct |
1 ms |
204 KB |
Output is correct |
41 |
Correct |
113 ms |
336 KB |
Output is correct |
42 |
Correct |
38 ms |
204 KB |
Output is correct |
43 |
Correct |
64 ms |
332 KB |
Output is correct |
44 |
Correct |
41 ms |
312 KB |
Output is correct |
45 |
Correct |
31 ms |
204 KB |
Output is correct |
46 |
Correct |
123 ms |
336 KB |
Output is correct |
47 |
Correct |
34 ms |
332 KB |
Output is correct |
48 |
Correct |
65 ms |
312 KB |
Output is correct |
49 |
Correct |
36 ms |
204 KB |
Output is correct |
50 |
Correct |
541 ms |
384 KB |
Output is correct |
51 |
Correct |
584 ms |
388 KB |
Output is correct |
52 |
Correct |
540 ms |
396 KB |
Output is correct |
53 |
Correct |
594 ms |
384 KB |
Output is correct |
54 |
Correct |
540 ms |
388 KB |
Output is correct |
55 |
Correct |
611 ms |
388 KB |
Output is correct |
56 |
Correct |
530 ms |
396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
20 ms |
320 KB |
Output is correct |
10 |
Correct |
5 ms |
204 KB |
Output is correct |
11 |
Correct |
15 ms |
324 KB |
Output is correct |
12 |
Correct |
18 ms |
324 KB |
Output is correct |
13 |
Correct |
17 ms |
320 KB |
Output is correct |
14 |
Correct |
11 ms |
320 KB |
Output is correct |
15 |
Correct |
3 ms |
204 KB |
Output is correct |
16 |
Correct |
11 ms |
204 KB |
Output is correct |
17 |
Correct |
28 ms |
204 KB |
Output is correct |
18 |
Correct |
16 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 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 |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
0 ms |
204 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
204 KB |
Output is correct |
28 |
Correct |
0 ms |
204 KB |
Output is correct |
29 |
Correct |
0 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
10 ms |
204 KB |
Output is correct |
35 |
Correct |
22 ms |
320 KB |
Output is correct |
36 |
Correct |
35 ms |
204 KB |
Output is correct |
37 |
Correct |
117 ms |
304 KB |
Output is correct |
38 |
Correct |
146 ms |
304 KB |
Output is correct |
39 |
Correct |
23 ms |
312 KB |
Output is correct |
40 |
Correct |
1 ms |
204 KB |
Output is correct |
41 |
Correct |
22 ms |
204 KB |
Output is correct |
42 |
Correct |
34 ms |
204 KB |
Output is correct |
43 |
Correct |
5 ms |
204 KB |
Output is correct |
44 |
Correct |
29 ms |
204 KB |
Output is correct |
45 |
Correct |
29 ms |
308 KB |
Output is correct |
46 |
Correct |
34 ms |
324 KB |
Output is correct |
47 |
Correct |
9 ms |
324 KB |
Output is correct |
48 |
Correct |
1 ms |
204 KB |
Output is correct |
49 |
Correct |
113 ms |
336 KB |
Output is correct |
50 |
Correct |
38 ms |
204 KB |
Output is correct |
51 |
Correct |
64 ms |
332 KB |
Output is correct |
52 |
Correct |
41 ms |
312 KB |
Output is correct |
53 |
Correct |
31 ms |
204 KB |
Output is correct |
54 |
Correct |
123 ms |
336 KB |
Output is correct |
55 |
Correct |
34 ms |
332 KB |
Output is correct |
56 |
Correct |
65 ms |
312 KB |
Output is correct |
57 |
Correct |
36 ms |
204 KB |
Output is correct |
58 |
Correct |
541 ms |
384 KB |
Output is correct |
59 |
Correct |
584 ms |
388 KB |
Output is correct |
60 |
Correct |
540 ms |
396 KB |
Output is correct |
61 |
Correct |
594 ms |
384 KB |
Output is correct |
62 |
Correct |
540 ms |
388 KB |
Output is correct |
63 |
Correct |
611 ms |
388 KB |
Output is correct |
64 |
Correct |
530 ms |
396 KB |
Output is correct |
65 |
Correct |
0 ms |
204 KB |
Output is correct |
66 |
Correct |
0 ms |
204 KB |
Output is correct |
67 |
Correct |
616 ms |
2684 KB |
Output is correct |
68 |
Correct |
572 ms |
2616 KB |
Output is correct |
69 |
Correct |
497 ms |
2292 KB |
Output is correct |
70 |
Correct |
564 ms |
2568 KB |
Output is correct |
71 |
Correct |
555 ms |
2428 KB |
Output is correct |
72 |
Correct |
553 ms |
2500 KB |
Output is correct |
73 |
Correct |
533 ms |
2552 KB |
Output is correct |
74 |
Correct |
559 ms |
2552 KB |
Output is correct |
75 |
Correct |
551 ms |
2520 KB |
Output is correct |
76 |
Correct |
597 ms |
2552 KB |
Output is correct |
77 |
Correct |
96 ms |
2612 KB |
Output is correct |
78 |
Correct |
53 ms |
2340 KB |
Output is correct |
79 |
Correct |
78 ms |
2248 KB |
Output is correct |
80 |
Correct |
70 ms |
2364 KB |
Output is correct |
81 |
Correct |
71 ms |
2232 KB |
Output is correct |
82 |
Correct |
62 ms |
2472 KB |
Output is correct |
83 |
Correct |
45 ms |
2348 KB |
Output is correct |
84 |
Correct |
347 ms |
2680 KB |
Output is correct |
85 |
Correct |
605 ms |
2832 KB |
Output is correct |
86 |
Correct |
1 ms |
300 KB |
Output is correct |
87 |
Correct |
1 ms |
332 KB |
Output is correct |
88 |
Correct |
610 ms |
2788 KB |
Output is correct |
89 |
Correct |
115 ms |
776 KB |
Output is correct |
90 |
Correct |
1 ms |
336 KB |
Output is correct |