/* ** *** In the name of God *** ** */
// Only Haider is Amir al-Momenin
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
const ll maxn = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll inf = 1e18 + 1;
// #define endl '\n'
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll n, A, B;
cin >> n >> A >> B;
ll x = 1ll * A / __gcd(A, B + 1);
if (x <= inf / B) x *= B;
else x = inf;
vector<pair<ll, ll>> b;
while (n--) {
ll l, r;
cin >> l >> r;
if (r - l + 1 >= x) return cout << x, 0;
l %= x, r %= x;
if (r >= l) b.push_back({l, r});
else b.push_back({l, x - 1}), b.push_back({0, r});
}
sort(b.begin(), b.end());
ll p = 0, ans = x;
for (auto i : b) {
ll l = i.first, r = i.second;
if (l > p) ans -= (l - p);
p = max(p, r + 1);
}
ans -= (x - p);
cout << ans;
return 0;
}
/*
_ _ _ _ _ _ _ _ _ _ _ _
/ / \ / / | / / |
/``````\ \ |`````| | |`````| |
/ \ \ | | | | | |
/ \ \ | | | | | |
/ /\ \ \ | | | | | |
/ / /\ \ \ | | | | | |
/ / / \ \ \ | | | | | |
/ / /_ _ \ \ \ | | | | | |
/ /_ _ _ _ \ \ \ | | | | | |
/ \ \ | | |_ _ _ _ | | |
/ _ _ _ _ _ _ _ \ \ | | / /| | | |
/ / / \ \ \ | |/_ _ _ _ / | | | |
/ / / \ \ / | | / | | /
/_ _ _/_/ \_ _ _\/ |_ _ _ _ _ _ _ _|/ |_ _ _|/
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
6 ms |
720 KB |
Output is correct |
3 |
Correct |
6 ms |
976 KB |
Output is correct |
4 |
Correct |
0 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 |
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 |
0 ms |
312 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
5 ms |
976 KB |
Output is correct |
17 |
Correct |
51 ms |
5660 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 |
308 KB |
Output is correct |
5 |
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 |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
359 ms |
41232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
512 ms |
16832 KB |
Output is correct |
3 |
Correct |
470 ms |
16848 KB |
Output is correct |
4 |
Correct |
458 ms |
16840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
512 ms |
16832 KB |
Output is correct |
3 |
Correct |
470 ms |
16848 KB |
Output is correct |
4 |
Correct |
458 ms |
16840 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
461 ms |
16852 KB |
Output is correct |
7 |
Correct |
464 ms |
16808 KB |
Output is correct |
8 |
Correct |
536 ms |
16772 KB |
Output is correct |
9 |
Correct |
487 ms |
16832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
512 ms |
16832 KB |
Output is correct |
3 |
Correct |
470 ms |
16848 KB |
Output is correct |
4 |
Correct |
458 ms |
16840 KB |
Output is correct |
5 |
Correct |
1 ms |
308 KB |
Output is correct |
6 |
Correct |
47 ms |
2544 KB |
Output is correct |
7 |
Correct |
50 ms |
2636 KB |
Output is correct |
8 |
Correct |
46 ms |
2508 KB |
Output is correct |
9 |
Correct |
46 ms |
2496 KB |
Output is correct |
10 |
Correct |
47 ms |
2588 KB |
Output is correct |
11 |
Correct |
47 ms |
2556 KB |
Output is correct |
12 |
Correct |
44 ms |
2512 KB |
Output is correct |
13 |
Correct |
48 ms |
2620 KB |
Output is correct |
14 |
Correct |
44 ms |
2544 KB |
Output is correct |
15 |
Correct |
54 ms |
2604 KB |
Output is correct |
16 |
Correct |
55 ms |
5720 KB |
Output is correct |
17 |
Correct |
47 ms |
5640 KB |
Output is correct |
18 |
Correct |
486 ms |
53100 KB |
Output is correct |
19 |
Correct |
535 ms |
53184 KB |
Output is correct |
20 |
Correct |
529 ms |
53260 KB |
Output is correct |
21 |
Correct |
51 ms |
5692 KB |
Output is correct |
22 |
Correct |
45 ms |
5704 KB |
Output is correct |
23 |
Correct |
162 ms |
18456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
48 ms |
2624 KB |
Output is correct |
3 |
Correct |
48 ms |
2592 KB |
Output is correct |
4 |
Correct |
552 ms |
16752 KB |
Output is correct |
5 |
Correct |
50 ms |
2524 KB |
Output is correct |
6 |
Correct |
49 ms |
2624 KB |
Output is correct |
7 |
Correct |
47 ms |
2620 KB |
Output is correct |
8 |
Correct |
52 ms |
2576 KB |
Output is correct |
9 |
Correct |
59 ms |
2620 KB |
Output is correct |
10 |
Correct |
50 ms |
2532 KB |
Output is correct |
11 |
Correct |
51 ms |
2624 KB |
Output is correct |
12 |
Correct |
48 ms |
2612 KB |
Output is correct |
13 |
Correct |
48 ms |
2588 KB |
Output is correct |
14 |
Correct |
529 ms |
16844 KB |
Output is correct |
15 |
Correct |
52 ms |
5704 KB |
Output is correct |
16 |
Correct |
492 ms |
53136 KB |
Output is correct |
17 |
Correct |
475 ms |
53144 KB |
Output is correct |
18 |
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 |
6 ms |
720 KB |
Output is correct |
3 |
Correct |
6 ms |
976 KB |
Output is correct |
4 |
Correct |
0 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 |
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 |
0 ms |
312 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
5 ms |
976 KB |
Output is correct |
17 |
Correct |
51 ms |
5660 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 |
308 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 |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
328 KB |
Output is correct |
28 |
Correct |
359 ms |
41232 KB |
Output is correct |
29 |
Correct |
0 ms |
204 KB |
Output is correct |
30 |
Correct |
512 ms |
16832 KB |
Output is correct |
31 |
Correct |
470 ms |
16848 KB |
Output is correct |
32 |
Correct |
458 ms |
16840 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
461 ms |
16852 KB |
Output is correct |
35 |
Correct |
464 ms |
16808 KB |
Output is correct |
36 |
Correct |
536 ms |
16772 KB |
Output is correct |
37 |
Correct |
487 ms |
16832 KB |
Output is correct |
38 |
Correct |
1 ms |
308 KB |
Output is correct |
39 |
Correct |
47 ms |
2544 KB |
Output is correct |
40 |
Correct |
50 ms |
2636 KB |
Output is correct |
41 |
Correct |
46 ms |
2508 KB |
Output is correct |
42 |
Correct |
46 ms |
2496 KB |
Output is correct |
43 |
Correct |
47 ms |
2588 KB |
Output is correct |
44 |
Correct |
47 ms |
2556 KB |
Output is correct |
45 |
Correct |
44 ms |
2512 KB |
Output is correct |
46 |
Correct |
48 ms |
2620 KB |
Output is correct |
47 |
Correct |
44 ms |
2544 KB |
Output is correct |
48 |
Correct |
54 ms |
2604 KB |
Output is correct |
49 |
Correct |
55 ms |
5720 KB |
Output is correct |
50 |
Correct |
47 ms |
5640 KB |
Output is correct |
51 |
Correct |
486 ms |
53100 KB |
Output is correct |
52 |
Correct |
535 ms |
53184 KB |
Output is correct |
53 |
Correct |
529 ms |
53260 KB |
Output is correct |
54 |
Correct |
51 ms |
5692 KB |
Output is correct |
55 |
Correct |
45 ms |
5704 KB |
Output is correct |
56 |
Correct |
162 ms |
18456 KB |
Output is correct |
57 |
Correct |
1 ms |
204 KB |
Output is correct |
58 |
Correct |
48 ms |
2624 KB |
Output is correct |
59 |
Correct |
48 ms |
2592 KB |
Output is correct |
60 |
Correct |
552 ms |
16752 KB |
Output is correct |
61 |
Correct |
50 ms |
2524 KB |
Output is correct |
62 |
Correct |
49 ms |
2624 KB |
Output is correct |
63 |
Correct |
47 ms |
2620 KB |
Output is correct |
64 |
Correct |
52 ms |
2576 KB |
Output is correct |
65 |
Correct |
59 ms |
2620 KB |
Output is correct |
66 |
Correct |
50 ms |
2532 KB |
Output is correct |
67 |
Correct |
51 ms |
2624 KB |
Output is correct |
68 |
Correct |
48 ms |
2612 KB |
Output is correct |
69 |
Correct |
48 ms |
2588 KB |
Output is correct |
70 |
Correct |
529 ms |
16844 KB |
Output is correct |
71 |
Correct |
52 ms |
5704 KB |
Output is correct |
72 |
Correct |
492 ms |
53136 KB |
Output is correct |
73 |
Correct |
475 ms |
53144 KB |
Output is correct |
74 |
Correct |
1 ms |
204 KB |
Output is correct |
75 |
Correct |
0 ms |
204 KB |
Output is correct |
76 |
Correct |
0 ms |
204 KB |
Output is correct |
77 |
Correct |
1 ms |
204 KB |
Output is correct |
78 |
Correct |
0 ms |
204 KB |
Output is correct |
79 |
Correct |
5 ms |
976 KB |
Output is correct |
80 |
Correct |
598 ms |
53244 KB |
Output is correct |
81 |
Correct |
559 ms |
53132 KB |
Output is correct |
82 |
Correct |
516 ms |
53260 KB |
Output is correct |
83 |
Correct |
529 ms |
53160 KB |
Output is correct |
84 |
Correct |
522 ms |
53200 KB |
Output is correct |
85 |
Correct |
570 ms |
53192 KB |
Output is correct |
86 |
Correct |
152 ms |
18396 KB |
Output is correct |
87 |
Correct |
1 ms |
204 KB |
Output is correct |