#include <iostream>
#include <iomanip>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <cassert>
#include <ctime>
#include <chrono>
#include <cstdio>
#include <random>
#include <vector>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <deque>
#include <queue>
#include <bitset>
#include <list>
#include <fstream>
#include <functional>
#include <complex>
using namespace std;
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());
const int INF = 1e9, MOD = 1e9 + 7, MOD2 = 998244353, LOG = 20;
const long double EPS = 1e-9, PI = acos(-1);
void solve()
{
long long n, a, b;
vector<pair<long long, long long>> v;
cin >> n >> a >> b;
v.resize(n);
long long sum = 0;
for (long long i = 0; i < n; i++)
{
cin >> v[i].first >> v[i].second;
sum += v[i].second - v[i].first + 1;
}
long long times = a / gcd(a, b + 1), per = times * b;
if (b >= (long long)1e18 / times)
{
cout << sum << "\n";
return;
}
vector<pair<long long, long long>> sg;
for (long long i = 0; i < n; i++)
{
if (v[i].second - v[i].first >= per)
{
cout << per << "\n";
return;
}
long long l = v[i].first % per, r = v[i].second % per;
if (l > r)
{
sg.push_back({ l, per - 1 });
sg.push_back({ 0, r });
}
else
{
sg.push_back({ l, r });
}
}
sort(sg.begin(), sg.end());
long long ans = 0;
long long mxr = -1;
for (auto& [l, r] : sg)
{
if (l <= mxr)
{
ans += max(0ll, r - mxr);
}
else
{
ans += (r - l + 1);
}
mxr = max(mxr, r);
}
cout << ans << "\n";
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
srand(time(NULL));
int tst = 1;
//cin >> tst;
while (tst--)
{
solve();
//cout << "----------------------------------------\n";
}
return 0;
}
/*
<3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⠤⠖⠚⢉⣩⣭⡭⠛⠓⠲⠦⣄⡀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⢀⡴⠋⠁⠀⠀⠊⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠳⢦⡀⠀⠀⠀⠀
⠀⠀⠀⠀⢀⡴⠃⢀⡴⢳⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⣆⠀⠀⠀
⠀⠀⠀⠀⡾⠁⣠⠋⠀⠈⢧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢧⠀⠀
⠀⠀⠀⣸⠁⢰⠃⠀⠀⠀⠈⢣⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣇⠀
⠀⠀⠀⡇⠀⡾⡀⠀⠀⠀⠀⣀⣹⣆⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⠀
⠀⠀⢸⠃⢀⣇⡈⠀⠀⠀⠀⠀⠀⢀⡑⢄⡀⢀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⡇
⠀⠀⢸⠀⢻⡟⡻⢶⡆⠀⠀⠀⠀⡼⠟⡳⢿⣦⡑⢄⠀⠀⠀⠀⠀⠀⠀⠀⢸⡇
⠀⠀⣸⠀⢸⠃⡇⢀⠇⠀⠀⠀⠀⠀⡼⠀⠀⠈⣿⡗⠂⠀⠀⠀⠀⠀⠀⠀⢸⠁
⠀⠀⡏⠀⣼⠀⢳⠊⠀⠀⠀⠀⠀⠀⠱⣀⣀⠔⣸⠁⠀⠀⠀⠀⠀⠀⠀⢠⡟⠀
⠀⠀⡇⢀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠠⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⢸⠃⠀
⠀⢸⠃⠘⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠁⠀⠀⢀⠀⠀⠀⠀⠀⣾⠀⠀
⠀⣸⠀⠀⠹⡄⠀⠀⠈⠁⠀⠀⠀⠀⠀⠀⠀⡞⠀⠀⠀⠸⠀⠀⠀⠀⠀⡇⠀⠀
⠀⡏⠀⠀⠀⠙⣆⠀⠀⠀⠀⠀⠀⠀⢀⣠⢶⡇⠀⠀⢰⡀⠀⠀⠀⠀⠀⡇⠀⠀
⢰⠇⡄⠀⠀⠀⡿⢣⣀⣀⣀⡤⠴⡞⠉⠀⢸⠀⠀⠀⣿⡇⠀⠀⠀⠀⠀⣧⠀⠀
⣸⠀⡇⠀⠀⠀⠀⠀⠀⠉⠀⠀⠀⢹⠀⠀⢸⠀⠀⢀⣿⠇⠀⠀⠀⠁⠀⢸⠀⠀
⣿⠀⡇⠀⠀⠀⠀⠀⢀⡤⠤⠶⠶⠾⠤⠄⢸⠀⡀⠸⣿⣀⠀⠀⠀⠀⠀⠈⣇⠀
⡇⠀⡇⠀⠀⡀⠀⡴⠋⠀⠀⠀⠀⠀⠀⠀⠸⡌⣵⡀⢳⡇⠀⠀⠀⠀⠀⠀⢹⡀
⡇⠀⠇⠀⠀⡇⡸⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⠮⢧⣀⣻⢂⠀⠀⠀⠀⠀⠀⢧
⣇⠀⢠⠀⠀⢳⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⡎⣆⠀⠀⠀⠀⠀⠘
⢻⠀⠈⠰⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠰⠘⢮⣧⡀⠀⠀⠀⠀
⠸⡆⠀⠀⠇⣾⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠆⠀⠀⠀⠀⠀⠀⠀⠙⠳⣄⡀⢢⡀
<3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
4 ms |
1232 KB |
Output is correct |
3 |
Correct |
4 ms |
1236 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
1256 KB |
Output is correct |
17 |
Correct |
36 ms |
7772 KB |
Output is correct |
18 |
Correct |
1 ms |
324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
220 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
260 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
224 ms |
57808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
318 ms |
69636 KB |
Output is correct |
3 |
Correct |
319 ms |
69576 KB |
Output is correct |
4 |
Correct |
302 ms |
69576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
318 ms |
69636 KB |
Output is correct |
3 |
Correct |
319 ms |
69576 KB |
Output is correct |
4 |
Correct |
302 ms |
69576 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
325 ms |
69584 KB |
Output is correct |
7 |
Correct |
334 ms |
69580 KB |
Output is correct |
8 |
Correct |
316 ms |
69564 KB |
Output is correct |
9 |
Correct |
343 ms |
69580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
318 ms |
69636 KB |
Output is correct |
3 |
Correct |
319 ms |
69576 KB |
Output is correct |
4 |
Correct |
302 ms |
69576 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
30 ms |
7752 KB |
Output is correct |
7 |
Correct |
33 ms |
7752 KB |
Output is correct |
8 |
Correct |
31 ms |
7752 KB |
Output is correct |
9 |
Correct |
31 ms |
7756 KB |
Output is correct |
10 |
Correct |
30 ms |
7760 KB |
Output is correct |
11 |
Correct |
32 ms |
7752 KB |
Output is correct |
12 |
Correct |
30 ms |
7756 KB |
Output is correct |
13 |
Correct |
34 ms |
7764 KB |
Output is correct |
14 |
Correct |
33 ms |
7760 KB |
Output is correct |
15 |
Correct |
34 ms |
7772 KB |
Output is correct |
16 |
Correct |
33 ms |
7760 KB |
Output is correct |
17 |
Correct |
32 ms |
7764 KB |
Output is correct |
18 |
Correct |
329 ms |
69576 KB |
Output is correct |
19 |
Correct |
325 ms |
69572 KB |
Output is correct |
20 |
Correct |
344 ms |
69576 KB |
Output is correct |
21 |
Correct |
35 ms |
7776 KB |
Output is correct |
22 |
Correct |
29 ms |
7752 KB |
Output is correct |
23 |
Correct |
95 ms |
26512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
33 ms |
7768 KB |
Output is correct |
3 |
Correct |
34 ms |
7772 KB |
Output is correct |
4 |
Correct |
368 ms |
69588 KB |
Output is correct |
5 |
Correct |
33 ms |
7752 KB |
Output is correct |
6 |
Correct |
33 ms |
7752 KB |
Output is correct |
7 |
Correct |
39 ms |
7760 KB |
Output is correct |
8 |
Correct |
35 ms |
7752 KB |
Output is correct |
9 |
Correct |
33 ms |
7756 KB |
Output is correct |
10 |
Correct |
33 ms |
7784 KB |
Output is correct |
11 |
Correct |
33 ms |
7764 KB |
Output is correct |
12 |
Correct |
29 ms |
7768 KB |
Output is correct |
13 |
Correct |
37 ms |
7892 KB |
Output is correct |
14 |
Correct |
374 ms |
69588 KB |
Output is correct |
15 |
Correct |
33 ms |
7752 KB |
Output is correct |
16 |
Correct |
328 ms |
69584 KB |
Output is correct |
17 |
Correct |
315 ms |
69584 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
4 ms |
1232 KB |
Output is correct |
3 |
Correct |
4 ms |
1236 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
1256 KB |
Output is correct |
17 |
Correct |
36 ms |
7772 KB |
Output is correct |
18 |
Correct |
1 ms |
324 KB |
Output is correct |
19 |
Correct |
1 ms |
328 KB |
Output is correct |
20 |
Correct |
1 ms |
220 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
328 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
260 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
224 ms |
57808 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
318 ms |
69636 KB |
Output is correct |
31 |
Correct |
319 ms |
69576 KB |
Output is correct |
32 |
Correct |
302 ms |
69576 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
325 ms |
69584 KB |
Output is correct |
35 |
Correct |
334 ms |
69580 KB |
Output is correct |
36 |
Correct |
316 ms |
69564 KB |
Output is correct |
37 |
Correct |
343 ms |
69580 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
30 ms |
7752 KB |
Output is correct |
40 |
Correct |
33 ms |
7752 KB |
Output is correct |
41 |
Correct |
31 ms |
7752 KB |
Output is correct |
42 |
Correct |
31 ms |
7756 KB |
Output is correct |
43 |
Correct |
30 ms |
7760 KB |
Output is correct |
44 |
Correct |
32 ms |
7752 KB |
Output is correct |
45 |
Correct |
30 ms |
7756 KB |
Output is correct |
46 |
Correct |
34 ms |
7764 KB |
Output is correct |
47 |
Correct |
33 ms |
7760 KB |
Output is correct |
48 |
Correct |
34 ms |
7772 KB |
Output is correct |
49 |
Correct |
33 ms |
7760 KB |
Output is correct |
50 |
Correct |
32 ms |
7764 KB |
Output is correct |
51 |
Correct |
329 ms |
69576 KB |
Output is correct |
52 |
Correct |
325 ms |
69572 KB |
Output is correct |
53 |
Correct |
344 ms |
69576 KB |
Output is correct |
54 |
Correct |
35 ms |
7776 KB |
Output is correct |
55 |
Correct |
29 ms |
7752 KB |
Output is correct |
56 |
Correct |
95 ms |
26512 KB |
Output is correct |
57 |
Correct |
1 ms |
212 KB |
Output is correct |
58 |
Correct |
33 ms |
7768 KB |
Output is correct |
59 |
Correct |
34 ms |
7772 KB |
Output is correct |
60 |
Correct |
368 ms |
69588 KB |
Output is correct |
61 |
Correct |
33 ms |
7752 KB |
Output is correct |
62 |
Correct |
33 ms |
7752 KB |
Output is correct |
63 |
Correct |
39 ms |
7760 KB |
Output is correct |
64 |
Correct |
35 ms |
7752 KB |
Output is correct |
65 |
Correct |
33 ms |
7756 KB |
Output is correct |
66 |
Correct |
33 ms |
7784 KB |
Output is correct |
67 |
Correct |
33 ms |
7764 KB |
Output is correct |
68 |
Correct |
29 ms |
7768 KB |
Output is correct |
69 |
Correct |
37 ms |
7892 KB |
Output is correct |
70 |
Correct |
374 ms |
69588 KB |
Output is correct |
71 |
Correct |
33 ms |
7752 KB |
Output is correct |
72 |
Correct |
328 ms |
69584 KB |
Output is correct |
73 |
Correct |
315 ms |
69584 KB |
Output is correct |
74 |
Correct |
1 ms |
212 KB |
Output is correct |
75 |
Correct |
0 ms |
212 KB |
Output is correct |
76 |
Correct |
1 ms |
212 KB |
Output is correct |
77 |
Correct |
1 ms |
212 KB |
Output is correct |
78 |
Correct |
1 ms |
324 KB |
Output is correct |
79 |
Correct |
4 ms |
1236 KB |
Output is correct |
80 |
Correct |
361 ms |
69592 KB |
Output is correct |
81 |
Correct |
381 ms |
69572 KB |
Output is correct |
82 |
Correct |
339 ms |
69612 KB |
Output is correct |
83 |
Correct |
336 ms |
69584 KB |
Output is correct |
84 |
Correct |
374 ms |
69576 KB |
Output is correct |
85 |
Correct |
346 ms |
69576 KB |
Output is correct |
86 |
Correct |
107 ms |
26500 KB |
Output is correct |
87 |
Correct |
1 ms |
212 KB |
Output is correct |