//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl "\n"
#define all(c) (c).begin(),(c).end()
// when you ponder, divide and conquer
#define lo __int128
lo _gcd(lo x, lo y) {
if (x == 0) return y;
return _gcd(y % x, x);
}
signed main() {
// freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
std::ios::sync_with_stdio(false);
cin.tie(0);
ll n, a, b;
cin >> n >> a >> b;
lo g = _gcd(b+1, (lo) a*b);
lo m = (lo) a * b / g;
vector<pair<lo, int>> evs;
for (int i = 0; i < n; i++) {
ll xl, xr;
cin >> xl >> xr;
xr++;
if (xr-xl >= m) {
cout << ((ll) m) << endl;
return 0;
}
lo l = xl % m;
lo r = xr % m;
if (r <= l)
evs.emplace_back(0, -1), evs.emplace_back(r, 1), evs.emplace_back(l, -1), evs.emplace_back(m, 1);
else
evs.emplace_back(l, -1), evs.emplace_back(r, 1);
}
sort(all(evs));
ll res = 0;
lo p = 0;
int c = 0;
for (auto [x, t] : evs) {
if (c == 0) p = x;
c -= t;
if (c == 0) res += x-p;
}
cout << res << endl;
}
/* --- PSolving ---
* Simplifying (getting rid of variables, conditions, code logic, etc.)
* Reframing
* Solving a subtask (subgoal, aux. problem, removing a condition or fixing a parameter, etc.)
* Inducing
* Divide and conquer
* Working backwards
* Visual intuition
* !! Reductions don't have to be specializations, they can be generalizations
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
7 ms |
1484 KB |
Output is correct |
3 |
Correct |
7 ms |
1484 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 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 |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 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 |
7 ms |
1484 KB |
Output is correct |
17 |
Correct |
81 ms |
8616 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
482 ms |
66000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
696 ms |
66076 KB |
Output is correct |
3 |
Correct |
745 ms |
66020 KB |
Output is correct |
4 |
Correct |
684 ms |
66080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
696 ms |
66076 KB |
Output is correct |
3 |
Correct |
745 ms |
66020 KB |
Output is correct |
4 |
Correct |
684 ms |
66080 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
696 ms |
66028 KB |
Output is correct |
7 |
Correct |
744 ms |
66084 KB |
Output is correct |
8 |
Correct |
681 ms |
66220 KB |
Output is correct |
9 |
Correct |
896 ms |
66056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
696 ms |
66076 KB |
Output is correct |
3 |
Correct |
745 ms |
66020 KB |
Output is correct |
4 |
Correct |
684 ms |
66080 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
64 ms |
8612 KB |
Output is correct |
7 |
Correct |
72 ms |
8648 KB |
Output is correct |
8 |
Correct |
61 ms |
8640 KB |
Output is correct |
9 |
Correct |
65 ms |
8624 KB |
Output is correct |
10 |
Correct |
59 ms |
8560 KB |
Output is correct |
11 |
Correct |
67 ms |
8660 KB |
Output is correct |
12 |
Correct |
62 ms |
8628 KB |
Output is correct |
13 |
Correct |
72 ms |
8632 KB |
Output is correct |
14 |
Correct |
62 ms |
8544 KB |
Output is correct |
15 |
Correct |
82 ms |
8564 KB |
Output is correct |
16 |
Correct |
74 ms |
8656 KB |
Output is correct |
17 |
Correct |
63 ms |
8572 KB |
Output is correct |
18 |
Correct |
806 ms |
66240 KB |
Output is correct |
19 |
Correct |
687 ms |
66044 KB |
Output is correct |
20 |
Correct |
901 ms |
66028 KB |
Output is correct |
21 |
Correct |
86 ms |
8672 KB |
Output is correct |
22 |
Correct |
60 ms |
8560 KB |
Output is correct |
23 |
Correct |
205 ms |
33164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
75 ms |
8640 KB |
Output is correct |
3 |
Correct |
74 ms |
8576 KB |
Output is correct |
4 |
Correct |
941 ms |
66180 KB |
Output is correct |
5 |
Correct |
75 ms |
8724 KB |
Output is correct |
6 |
Correct |
73 ms |
8584 KB |
Output is correct |
7 |
Correct |
76 ms |
8616 KB |
Output is correct |
8 |
Correct |
94 ms |
8568 KB |
Output is correct |
9 |
Correct |
71 ms |
8652 KB |
Output is correct |
10 |
Correct |
79 ms |
8624 KB |
Output is correct |
11 |
Correct |
73 ms |
8684 KB |
Output is correct |
12 |
Correct |
66 ms |
8600 KB |
Output is correct |
13 |
Correct |
79 ms |
8624 KB |
Output is correct |
14 |
Correct |
881 ms |
66120 KB |
Output is correct |
15 |
Correct |
80 ms |
8548 KB |
Output is correct |
16 |
Correct |
679 ms |
66040 KB |
Output is correct |
17 |
Correct |
696 ms |
66028 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
7 ms |
1484 KB |
Output is correct |
3 |
Correct |
7 ms |
1484 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 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 |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 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 |
7 ms |
1484 KB |
Output is correct |
17 |
Correct |
81 ms |
8616 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 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 |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
2 ms |
332 KB |
Output is correct |
28 |
Correct |
482 ms |
66000 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
696 ms |
66076 KB |
Output is correct |
31 |
Correct |
745 ms |
66020 KB |
Output is correct |
32 |
Correct |
684 ms |
66080 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
696 ms |
66028 KB |
Output is correct |
35 |
Correct |
744 ms |
66084 KB |
Output is correct |
36 |
Correct |
681 ms |
66220 KB |
Output is correct |
37 |
Correct |
896 ms |
66056 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
64 ms |
8612 KB |
Output is correct |
40 |
Correct |
72 ms |
8648 KB |
Output is correct |
41 |
Correct |
61 ms |
8640 KB |
Output is correct |
42 |
Correct |
65 ms |
8624 KB |
Output is correct |
43 |
Correct |
59 ms |
8560 KB |
Output is correct |
44 |
Correct |
67 ms |
8660 KB |
Output is correct |
45 |
Correct |
62 ms |
8628 KB |
Output is correct |
46 |
Correct |
72 ms |
8632 KB |
Output is correct |
47 |
Correct |
62 ms |
8544 KB |
Output is correct |
48 |
Correct |
82 ms |
8564 KB |
Output is correct |
49 |
Correct |
74 ms |
8656 KB |
Output is correct |
50 |
Correct |
63 ms |
8572 KB |
Output is correct |
51 |
Correct |
806 ms |
66240 KB |
Output is correct |
52 |
Correct |
687 ms |
66044 KB |
Output is correct |
53 |
Correct |
901 ms |
66028 KB |
Output is correct |
54 |
Correct |
86 ms |
8672 KB |
Output is correct |
55 |
Correct |
60 ms |
8560 KB |
Output is correct |
56 |
Correct |
205 ms |
33164 KB |
Output is correct |
57 |
Correct |
1 ms |
204 KB |
Output is correct |
58 |
Correct |
75 ms |
8640 KB |
Output is correct |
59 |
Correct |
74 ms |
8576 KB |
Output is correct |
60 |
Correct |
941 ms |
66180 KB |
Output is correct |
61 |
Correct |
75 ms |
8724 KB |
Output is correct |
62 |
Correct |
73 ms |
8584 KB |
Output is correct |
63 |
Correct |
76 ms |
8616 KB |
Output is correct |
64 |
Correct |
94 ms |
8568 KB |
Output is correct |
65 |
Correct |
71 ms |
8652 KB |
Output is correct |
66 |
Correct |
79 ms |
8624 KB |
Output is correct |
67 |
Correct |
73 ms |
8684 KB |
Output is correct |
68 |
Correct |
66 ms |
8600 KB |
Output is correct |
69 |
Correct |
79 ms |
8624 KB |
Output is correct |
70 |
Correct |
881 ms |
66120 KB |
Output is correct |
71 |
Correct |
80 ms |
8548 KB |
Output is correct |
72 |
Correct |
679 ms |
66040 KB |
Output is correct |
73 |
Correct |
696 ms |
66028 KB |
Output is correct |
74 |
Correct |
1 ms |
204 KB |
Output is correct |
75 |
Correct |
1 ms |
204 KB |
Output is correct |
76 |
Correct |
1 ms |
204 KB |
Output is correct |
77 |
Correct |
1 ms |
204 KB |
Output is correct |
78 |
Correct |
1 ms |
204 KB |
Output is correct |
79 |
Correct |
7 ms |
1860 KB |
Output is correct |
80 |
Correct |
944 ms |
95768 KB |
Output is correct |
81 |
Correct |
959 ms |
95756 KB |
Output is correct |
82 |
Correct |
899 ms |
95904 KB |
Output is correct |
83 |
Correct |
893 ms |
95656 KB |
Output is correct |
84 |
Correct |
889 ms |
97704 KB |
Output is correct |
85 |
Correct |
892 ms |
95680 KB |
Output is correct |
86 |
Correct |
200 ms |
43024 KB |
Output is correct |
87 |
Correct |
1 ms |
332 KB |
Output is correct |