#include <bits/stdc++.h>
using i64 = long long;
#ifdef DEBUG
#include "debug.h"
#else
#define debug(...) void(23)
#endif
constexpr i64 INF = 2E18;
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int N;
i64 A, B;
std::cin >> N >> A >> B;
std::vector<i64> L(N), R(N);
for(int i = 0; i < N; ++i) {
std::cin >> L[i] >> R[i];
}
i64 ans = 0;
for(int i = 0; i < N; ++i) {
ans += R[i] - L[i] + 1;
}
{
i64 b = B + 1;
i64 g = std::gcd(A, b);
A /= g;
}
// B * A >= INF
if(B >= (INF + A - 1) / A) {
std::cout << ans << '\n';
return 0;
}
i64 search = B * A;
debug(search);
std::vector<std::pair<i64, int>> g;
for(int i = 0; i < N; ++i) {
if(R[i] - L[i] + 1 >= search) {
std::cout << search << '\n';
return 0;
}
if(L[i] % search < (R[i] + 1) % search) {
g.emplace_back(L[i] % search, +1);
g.emplace_back((R[i] + 1) % search, -1);
} else {
g.emplace_back(L[i] % search, +1);
g.emplace_back(search, -1);
g.emplace_back(0, +1);
g.emplace_back((R[i] + 1) % search, -1);
}
}
std::sort(g.begin(), g.end());
debug(g);
i64 last = 0, nw = 0;
ans = 0;
for(auto[x, d] : g) {
if(nw > 0) {
debug(last, x);
ans += x - last;
}
nw += d;
last = x;
}
std::cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
3 ms |
1244 KB |
Output is correct |
3 |
Correct |
3 ms |
1252 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
432 KB |
Output is correct |
16 |
Correct |
4 ms |
1240 KB |
Output is correct |
17 |
Correct |
35 ms |
7124 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
180 ms |
50020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
298 ms |
49244 KB |
Output is correct |
3 |
Correct |
301 ms |
69328 KB |
Output is correct |
4 |
Correct |
290 ms |
68540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
298 ms |
49244 KB |
Output is correct |
3 |
Correct |
301 ms |
69328 KB |
Output is correct |
4 |
Correct |
290 ms |
68540 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
299 ms |
69976 KB |
Output is correct |
7 |
Correct |
303 ms |
67912 KB |
Output is correct |
8 |
Correct |
291 ms |
68680 KB |
Output is correct |
9 |
Correct |
339 ms |
68540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
298 ms |
49244 KB |
Output is correct |
3 |
Correct |
301 ms |
69328 KB |
Output is correct |
4 |
Correct |
290 ms |
68540 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
28 ms |
8556 KB |
Output is correct |
7 |
Correct |
30 ms |
9316 KB |
Output is correct |
8 |
Correct |
28 ms |
8660 KB |
Output is correct |
9 |
Correct |
30 ms |
8452 KB |
Output is correct |
10 |
Correct |
26 ms |
9684 KB |
Output is correct |
11 |
Correct |
31 ms |
9160 KB |
Output is correct |
12 |
Correct |
36 ms |
10196 KB |
Output is correct |
13 |
Correct |
33 ms |
9160 KB |
Output is correct |
14 |
Correct |
29 ms |
9080 KB |
Output is correct |
15 |
Correct |
35 ms |
10448 KB |
Output is correct |
16 |
Correct |
33 ms |
10196 KB |
Output is correct |
17 |
Correct |
29 ms |
9672 KB |
Output is correct |
18 |
Correct |
316 ms |
69824 KB |
Output is correct |
19 |
Correct |
270 ms |
69704 KB |
Output is correct |
20 |
Correct |
338 ms |
69076 KB |
Output is correct |
21 |
Correct |
35 ms |
8660 KB |
Output is correct |
22 |
Correct |
26 ms |
8904 KB |
Output is correct |
23 |
Correct |
76 ms |
29572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
31 ms |
7536 KB |
Output is correct |
3 |
Correct |
33 ms |
6612 KB |
Output is correct |
4 |
Correct |
427 ms |
69564 KB |
Output is correct |
5 |
Correct |
33 ms |
10452 KB |
Output is correct |
6 |
Correct |
34 ms |
9172 KB |
Output is correct |
7 |
Correct |
33 ms |
8404 KB |
Output is correct |
8 |
Correct |
34 ms |
9684 KB |
Output is correct |
9 |
Correct |
33 ms |
9160 KB |
Output is correct |
10 |
Correct |
38 ms |
8904 KB |
Output is correct |
11 |
Correct |
33 ms |
10356 KB |
Output is correct |
12 |
Correct |
30 ms |
10208 KB |
Output is correct |
13 |
Correct |
36 ms |
9572 KB |
Output is correct |
14 |
Correct |
339 ms |
67928 KB |
Output is correct |
15 |
Correct |
35 ms |
10180 KB |
Output is correct |
16 |
Correct |
274 ms |
68424 KB |
Output is correct |
17 |
Correct |
294 ms |
69208 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
3 ms |
1244 KB |
Output is correct |
3 |
Correct |
3 ms |
1252 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
432 KB |
Output is correct |
16 |
Correct |
4 ms |
1240 KB |
Output is correct |
17 |
Correct |
35 ms |
7124 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
180 ms |
50020 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
298 ms |
49244 KB |
Output is correct |
31 |
Correct |
301 ms |
69328 KB |
Output is correct |
32 |
Correct |
290 ms |
68540 KB |
Output is correct |
33 |
Correct |
0 ms |
344 KB |
Output is correct |
34 |
Correct |
299 ms |
69976 KB |
Output is correct |
35 |
Correct |
303 ms |
67912 KB |
Output is correct |
36 |
Correct |
291 ms |
68680 KB |
Output is correct |
37 |
Correct |
339 ms |
68540 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
28 ms |
8556 KB |
Output is correct |
40 |
Correct |
30 ms |
9316 KB |
Output is correct |
41 |
Correct |
28 ms |
8660 KB |
Output is correct |
42 |
Correct |
30 ms |
8452 KB |
Output is correct |
43 |
Correct |
26 ms |
9684 KB |
Output is correct |
44 |
Correct |
31 ms |
9160 KB |
Output is correct |
45 |
Correct |
36 ms |
10196 KB |
Output is correct |
46 |
Correct |
33 ms |
9160 KB |
Output is correct |
47 |
Correct |
29 ms |
9080 KB |
Output is correct |
48 |
Correct |
35 ms |
10448 KB |
Output is correct |
49 |
Correct |
33 ms |
10196 KB |
Output is correct |
50 |
Correct |
29 ms |
9672 KB |
Output is correct |
51 |
Correct |
316 ms |
69824 KB |
Output is correct |
52 |
Correct |
270 ms |
69704 KB |
Output is correct |
53 |
Correct |
338 ms |
69076 KB |
Output is correct |
54 |
Correct |
35 ms |
8660 KB |
Output is correct |
55 |
Correct |
26 ms |
8904 KB |
Output is correct |
56 |
Correct |
76 ms |
29572 KB |
Output is correct |
57 |
Correct |
0 ms |
348 KB |
Output is correct |
58 |
Correct |
31 ms |
7536 KB |
Output is correct |
59 |
Correct |
33 ms |
6612 KB |
Output is correct |
60 |
Correct |
427 ms |
69564 KB |
Output is correct |
61 |
Correct |
33 ms |
10452 KB |
Output is correct |
62 |
Correct |
34 ms |
9172 KB |
Output is correct |
63 |
Correct |
33 ms |
8404 KB |
Output is correct |
64 |
Correct |
34 ms |
9684 KB |
Output is correct |
65 |
Correct |
33 ms |
9160 KB |
Output is correct |
66 |
Correct |
38 ms |
8904 KB |
Output is correct |
67 |
Correct |
33 ms |
10356 KB |
Output is correct |
68 |
Correct |
30 ms |
10208 KB |
Output is correct |
69 |
Correct |
36 ms |
9572 KB |
Output is correct |
70 |
Correct |
339 ms |
67928 KB |
Output is correct |
71 |
Correct |
35 ms |
10180 KB |
Output is correct |
72 |
Correct |
274 ms |
68424 KB |
Output is correct |
73 |
Correct |
294 ms |
69208 KB |
Output is correct |
74 |
Correct |
0 ms |
344 KB |
Output is correct |
75 |
Correct |
0 ms |
348 KB |
Output is correct |
76 |
Correct |
0 ms |
348 KB |
Output is correct |
77 |
Correct |
0 ms |
348 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
3 ms |
1496 KB |
Output is correct |
80 |
Correct |
353 ms |
67924 KB |
Output is correct |
81 |
Correct |
357 ms |
69460 KB |
Output is correct |
82 |
Correct |
335 ms |
69056 KB |
Output is correct |
83 |
Correct |
341 ms |
67652 KB |
Output is correct |
84 |
Correct |
349 ms |
68040 KB |
Output is correct |
85 |
Correct |
339 ms |
67772 KB |
Output is correct |
86 |
Correct |
76 ms |
29812 KB |
Output is correct |
87 |
Correct |
0 ms |
344 KB |
Output is correct |