#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<ll, ll>
const ll inf = 2e18;
ll n, a, b, l, r, p;
vector<pii> v;
ll gcd(ll c1, ll c2) {
if (!c2) return c1;
return gcd(c2, c1%c2);
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> a >> b;
a/=gcd(a, b+1);
p = (inf/b>a ? a*b : inf);
bool done = false;
while (n--) {
cin >> l >> r;
if (done) continue;
if (r-l >= p) {done = true; continue;}
l%=p, r%=p;
if (l>r) v.push_back({0, r}), v.push_back({l, p-1});
else v.push_back({l, r});
}
if (done) {cout << p; return 0;}
sort(v.begin(), v.end());
ll out=0, ma=-1;
for (auto [x,y]:v) out += (ma<x ? y-x+1 : max((ll)0, y-ma)), ma=max(ma, y);
cout << out;
return 0;
}
/*
3 3 3
4 4
7 9
17 18
ans: 4
3 5 10
1 20
50 68
89 98
ans: 31
1 1000000000000000 1000000000000000000
100000 1000000
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
5 ms |
600 KB |
Output is correct |
3 |
Correct |
5 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 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 |
0 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 |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
600 KB |
Output is correct |
17 |
Correct |
41 ms |
2456 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
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 |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
283 ms |
16748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
379 ms |
16868 KB |
Output is correct |
3 |
Correct |
369 ms |
16872 KB |
Output is correct |
4 |
Correct |
364 ms |
16944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
379 ms |
16868 KB |
Output is correct |
3 |
Correct |
369 ms |
16872 KB |
Output is correct |
4 |
Correct |
364 ms |
16944 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
373 ms |
16848 KB |
Output is correct |
7 |
Correct |
371 ms |
16904 KB |
Output is correct |
8 |
Correct |
368 ms |
16812 KB |
Output is correct |
9 |
Correct |
395 ms |
16848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
379 ms |
16868 KB |
Output is correct |
3 |
Correct |
369 ms |
16872 KB |
Output is correct |
4 |
Correct |
364 ms |
16944 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
36 ms |
2524 KB |
Output is correct |
7 |
Correct |
39 ms |
2516 KB |
Output is correct |
8 |
Correct |
37 ms |
2392 KB |
Output is correct |
9 |
Correct |
37 ms |
2484 KB |
Output is correct |
10 |
Correct |
36 ms |
2476 KB |
Output is correct |
11 |
Correct |
37 ms |
2512 KB |
Output is correct |
12 |
Correct |
35 ms |
2500 KB |
Output is correct |
13 |
Correct |
38 ms |
2384 KB |
Output is correct |
14 |
Correct |
37 ms |
2580 KB |
Output is correct |
15 |
Correct |
41 ms |
2520 KB |
Output is correct |
16 |
Correct |
38 ms |
2404 KB |
Output is correct |
17 |
Correct |
36 ms |
2440 KB |
Output is correct |
18 |
Correct |
373 ms |
16828 KB |
Output is correct |
19 |
Correct |
371 ms |
16816 KB |
Output is correct |
20 |
Correct |
398 ms |
16796 KB |
Output is correct |
21 |
Correct |
39 ms |
2512 KB |
Output is correct |
22 |
Correct |
34 ms |
2420 KB |
Output is correct |
23 |
Correct |
117 ms |
8640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
38 ms |
2384 KB |
Output is correct |
3 |
Correct |
38 ms |
2476 KB |
Output is correct |
4 |
Correct |
426 ms |
16944 KB |
Output is correct |
5 |
Correct |
38 ms |
2512 KB |
Output is correct |
6 |
Correct |
40 ms |
2520 KB |
Output is correct |
7 |
Correct |
38 ms |
2464 KB |
Output is correct |
8 |
Correct |
39 ms |
2436 KB |
Output is correct |
9 |
Correct |
38 ms |
2500 KB |
Output is correct |
10 |
Correct |
41 ms |
2436 KB |
Output is correct |
11 |
Correct |
38 ms |
2496 KB |
Output is correct |
12 |
Correct |
35 ms |
2504 KB |
Output is correct |
13 |
Correct |
39 ms |
2524 KB |
Output is correct |
14 |
Correct |
394 ms |
16788 KB |
Output is correct |
15 |
Correct |
39 ms |
2504 KB |
Output is correct |
16 |
Correct |
368 ms |
16812 KB |
Output is correct |
17 |
Correct |
356 ms |
16904 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
5 ms |
600 KB |
Output is correct |
3 |
Correct |
5 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 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 |
0 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 |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
600 KB |
Output is correct |
17 |
Correct |
41 ms |
2456 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
283 ms |
16748 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
379 ms |
16868 KB |
Output is correct |
31 |
Correct |
369 ms |
16872 KB |
Output is correct |
32 |
Correct |
364 ms |
16944 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
373 ms |
16848 KB |
Output is correct |
35 |
Correct |
371 ms |
16904 KB |
Output is correct |
36 |
Correct |
368 ms |
16812 KB |
Output is correct |
37 |
Correct |
395 ms |
16848 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
36 ms |
2524 KB |
Output is correct |
40 |
Correct |
39 ms |
2516 KB |
Output is correct |
41 |
Correct |
37 ms |
2392 KB |
Output is correct |
42 |
Correct |
37 ms |
2484 KB |
Output is correct |
43 |
Correct |
36 ms |
2476 KB |
Output is correct |
44 |
Correct |
37 ms |
2512 KB |
Output is correct |
45 |
Correct |
35 ms |
2500 KB |
Output is correct |
46 |
Correct |
38 ms |
2384 KB |
Output is correct |
47 |
Correct |
37 ms |
2580 KB |
Output is correct |
48 |
Correct |
41 ms |
2520 KB |
Output is correct |
49 |
Correct |
38 ms |
2404 KB |
Output is correct |
50 |
Correct |
36 ms |
2440 KB |
Output is correct |
51 |
Correct |
373 ms |
16828 KB |
Output is correct |
52 |
Correct |
371 ms |
16816 KB |
Output is correct |
53 |
Correct |
398 ms |
16796 KB |
Output is correct |
54 |
Correct |
39 ms |
2512 KB |
Output is correct |
55 |
Correct |
34 ms |
2420 KB |
Output is correct |
56 |
Correct |
117 ms |
8640 KB |
Output is correct |
57 |
Correct |
0 ms |
212 KB |
Output is correct |
58 |
Correct |
38 ms |
2384 KB |
Output is correct |
59 |
Correct |
38 ms |
2476 KB |
Output is correct |
60 |
Correct |
426 ms |
16944 KB |
Output is correct |
61 |
Correct |
38 ms |
2512 KB |
Output is correct |
62 |
Correct |
40 ms |
2520 KB |
Output is correct |
63 |
Correct |
38 ms |
2464 KB |
Output is correct |
64 |
Correct |
39 ms |
2436 KB |
Output is correct |
65 |
Correct |
38 ms |
2500 KB |
Output is correct |
66 |
Correct |
41 ms |
2436 KB |
Output is correct |
67 |
Correct |
38 ms |
2496 KB |
Output is correct |
68 |
Correct |
35 ms |
2504 KB |
Output is correct |
69 |
Correct |
39 ms |
2524 KB |
Output is correct |
70 |
Correct |
394 ms |
16788 KB |
Output is correct |
71 |
Correct |
39 ms |
2504 KB |
Output is correct |
72 |
Correct |
368 ms |
16812 KB |
Output is correct |
73 |
Correct |
356 ms |
16904 KB |
Output is correct |
74 |
Correct |
0 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 |
320 KB |
Output is correct |
78 |
Correct |
0 ms |
212 KB |
Output is correct |
79 |
Correct |
4 ms |
980 KB |
Output is correct |
80 |
Correct |
463 ms |
53416 KB |
Output is correct |
81 |
Correct |
468 ms |
53276 KB |
Output is correct |
82 |
Correct |
408 ms |
53188 KB |
Output is correct |
83 |
Correct |
417 ms |
53384 KB |
Output is correct |
84 |
Correct |
420 ms |
53276 KB |
Output is correct |
85 |
Correct |
411 ms |
53240 KB |
Output is correct |
86 |
Correct |
126 ms |
18488 KB |
Output is correct |
87 |
Correct |
1 ms |
212 KB |
Output is correct |