#include <bits/stdc++.h>
using namespace std;
using ll=long long;
const ll MAXT=1e18;
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n; ll a, b; cin>>n>>a>>b;
vector<pair<ll,ll>> p(n);
for (auto &x: p) cin>>x.first>>x.second;
/** the pair repeats after t=a*b/gcd(a, b+1) **/
a/=__gcd(a, b+1);
if (b>MAXT/a) {
ll ans=0;
for (auto &x: p) ans+=x.second-x.first+1;
cout<<ans;
} else {
ll t=a*b;
vector<pair<ll, ll>> p2;
for (auto &x: p) {
ll low=x.first-x.first%t;
ll mid=low+t;
ll high=x.second-x.second%t;
if (low==high) {
p2.emplace_back(x.first%t, x.second%t);
} else if (mid!=high) {
cout<<t;
return 0;
} else {
p2.emplace_back(x.first%t, t-1);
p2.emplace_back(0, x.second%t);
}
}
sort(p2.begin(), p2.end());
ll ans=0;
ll last=-1;
for (auto &x: p2) {
if (last<x.second) {
ans+=x.second-max(x.first, last+1)+1;
last=x.second;
}
}
cout<<ans;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
380 KB |
Output is correct |
2 |
Correct |
10 ms |
1272 KB |
Output is correct |
3 |
Correct |
12 ms |
1272 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
248 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
6 ms |
376 KB |
Output is correct |
16 |
Correct |
11 ms |
1272 KB |
Output is correct |
17 |
Correct |
67 ms |
7788 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
380 KB |
Output is correct |
3 |
Correct |
5 ms |
504 KB |
Output is correct |
4 |
Correct |
6 ms |
376 KB |
Output is correct |
5 |
Correct |
408 ms |
57684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
572 ms |
34260 KB |
Output is correct |
3 |
Correct |
571 ms |
45272 KB |
Output is correct |
4 |
Correct |
538 ms |
45276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
572 ms |
34260 KB |
Output is correct |
3 |
Correct |
571 ms |
45272 KB |
Output is correct |
4 |
Correct |
538 ms |
45276 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
561 ms |
45324 KB |
Output is correct |
7 |
Correct |
567 ms |
45276 KB |
Output is correct |
8 |
Correct |
551 ms |
45272 KB |
Output is correct |
9 |
Correct |
601 ms |
45272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
572 ms |
34260 KB |
Output is correct |
3 |
Correct |
571 ms |
45272 KB |
Output is correct |
4 |
Correct |
538 ms |
45276 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
59 ms |
7920 KB |
Output is correct |
7 |
Correct |
64 ms |
7832 KB |
Output is correct |
8 |
Correct |
60 ms |
7792 KB |
Output is correct |
9 |
Correct |
69 ms |
7968 KB |
Output is correct |
10 |
Correct |
60 ms |
7780 KB |
Output is correct |
11 |
Correct |
64 ms |
7920 KB |
Output is correct |
12 |
Correct |
63 ms |
7792 KB |
Output is correct |
13 |
Correct |
68 ms |
7792 KB |
Output is correct |
14 |
Correct |
62 ms |
7792 KB |
Output is correct |
15 |
Correct |
71 ms |
7912 KB |
Output is correct |
16 |
Correct |
65 ms |
7792 KB |
Output is correct |
17 |
Correct |
61 ms |
7796 KB |
Output is correct |
18 |
Correct |
566 ms |
45188 KB |
Output is correct |
19 |
Correct |
575 ms |
45224 KB |
Output is correct |
20 |
Correct |
632 ms |
45268 KB |
Output is correct |
21 |
Correct |
71 ms |
7792 KB |
Output is correct |
22 |
Correct |
61 ms |
7920 KB |
Output is correct |
23 |
Correct |
183 ms |
26540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
63 ms |
7792 KB |
Output is correct |
3 |
Correct |
67 ms |
7792 KB |
Output is correct |
4 |
Correct |
628 ms |
34252 KB |
Output is correct |
5 |
Correct |
64 ms |
7920 KB |
Output is correct |
6 |
Correct |
66 ms |
7824 KB |
Output is correct |
7 |
Correct |
68 ms |
7792 KB |
Output is correct |
8 |
Correct |
66 ms |
7792 KB |
Output is correct |
9 |
Correct |
65 ms |
7920 KB |
Output is correct |
10 |
Correct |
69 ms |
7880 KB |
Output is correct |
11 |
Correct |
66 ms |
7792 KB |
Output is correct |
12 |
Correct |
63 ms |
7792 KB |
Output is correct |
13 |
Correct |
66 ms |
7920 KB |
Output is correct |
14 |
Correct |
611 ms |
34356 KB |
Output is correct |
15 |
Correct |
61 ms |
7768 KB |
Output is correct |
16 |
Correct |
591 ms |
34264 KB |
Output is correct |
17 |
Correct |
564 ms |
34264 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
380 KB |
Output is correct |
2 |
Correct |
10 ms |
1272 KB |
Output is correct |
3 |
Correct |
12 ms |
1272 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
248 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
6 ms |
376 KB |
Output is correct |
16 |
Correct |
11 ms |
1272 KB |
Output is correct |
17 |
Correct |
67 ms |
7788 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
21 |
Correct |
5 ms |
376 KB |
Output is correct |
22 |
Correct |
5 ms |
376 KB |
Output is correct |
23 |
Correct |
5 ms |
376 KB |
Output is correct |
24 |
Correct |
5 ms |
376 KB |
Output is correct |
25 |
Correct |
5 ms |
380 KB |
Output is correct |
26 |
Correct |
5 ms |
504 KB |
Output is correct |
27 |
Correct |
6 ms |
376 KB |
Output is correct |
28 |
Correct |
408 ms |
57684 KB |
Output is correct |
29 |
Correct |
5 ms |
376 KB |
Output is correct |
30 |
Correct |
572 ms |
34260 KB |
Output is correct |
31 |
Correct |
571 ms |
45272 KB |
Output is correct |
32 |
Correct |
538 ms |
45276 KB |
Output is correct |
33 |
Correct |
5 ms |
376 KB |
Output is correct |
34 |
Correct |
561 ms |
45324 KB |
Output is correct |
35 |
Correct |
567 ms |
45276 KB |
Output is correct |
36 |
Correct |
551 ms |
45272 KB |
Output is correct |
37 |
Correct |
601 ms |
45272 KB |
Output is correct |
38 |
Correct |
5 ms |
376 KB |
Output is correct |
39 |
Correct |
59 ms |
7920 KB |
Output is correct |
40 |
Correct |
64 ms |
7832 KB |
Output is correct |
41 |
Correct |
60 ms |
7792 KB |
Output is correct |
42 |
Correct |
69 ms |
7968 KB |
Output is correct |
43 |
Correct |
60 ms |
7780 KB |
Output is correct |
44 |
Correct |
64 ms |
7920 KB |
Output is correct |
45 |
Correct |
63 ms |
7792 KB |
Output is correct |
46 |
Correct |
68 ms |
7792 KB |
Output is correct |
47 |
Correct |
62 ms |
7792 KB |
Output is correct |
48 |
Correct |
71 ms |
7912 KB |
Output is correct |
49 |
Correct |
65 ms |
7792 KB |
Output is correct |
50 |
Correct |
61 ms |
7796 KB |
Output is correct |
51 |
Correct |
566 ms |
45188 KB |
Output is correct |
52 |
Correct |
575 ms |
45224 KB |
Output is correct |
53 |
Correct |
632 ms |
45268 KB |
Output is correct |
54 |
Correct |
71 ms |
7792 KB |
Output is correct |
55 |
Correct |
61 ms |
7920 KB |
Output is correct |
56 |
Correct |
183 ms |
26540 KB |
Output is correct |
57 |
Correct |
5 ms |
376 KB |
Output is correct |
58 |
Correct |
63 ms |
7792 KB |
Output is correct |
59 |
Correct |
67 ms |
7792 KB |
Output is correct |
60 |
Correct |
628 ms |
34252 KB |
Output is correct |
61 |
Correct |
64 ms |
7920 KB |
Output is correct |
62 |
Correct |
66 ms |
7824 KB |
Output is correct |
63 |
Correct |
68 ms |
7792 KB |
Output is correct |
64 |
Correct |
66 ms |
7792 KB |
Output is correct |
65 |
Correct |
65 ms |
7920 KB |
Output is correct |
66 |
Correct |
69 ms |
7880 KB |
Output is correct |
67 |
Correct |
66 ms |
7792 KB |
Output is correct |
68 |
Correct |
63 ms |
7792 KB |
Output is correct |
69 |
Correct |
66 ms |
7920 KB |
Output is correct |
70 |
Correct |
611 ms |
34356 KB |
Output is correct |
71 |
Correct |
61 ms |
7768 KB |
Output is correct |
72 |
Correct |
591 ms |
34264 KB |
Output is correct |
73 |
Correct |
564 ms |
34264 KB |
Output is correct |
74 |
Correct |
5 ms |
376 KB |
Output is correct |
75 |
Correct |
5 ms |
376 KB |
Output is correct |
76 |
Correct |
5 ms |
376 KB |
Output is correct |
77 |
Correct |
5 ms |
420 KB |
Output is correct |
78 |
Correct |
5 ms |
376 KB |
Output is correct |
79 |
Correct |
11 ms |
1272 KB |
Output is correct |
80 |
Correct |
641 ms |
45108 KB |
Output is correct |
81 |
Correct |
639 ms |
45260 KB |
Output is correct |
82 |
Correct |
621 ms |
45168 KB |
Output is correct |
83 |
Correct |
615 ms |
45228 KB |
Output is correct |
84 |
Correct |
607 ms |
45156 KB |
Output is correct |
85 |
Correct |
609 ms |
45088 KB |
Output is correct |
86 |
Correct |
186 ms |
26596 KB |
Output is correct |
87 |
Correct |
5 ms |
376 KB |
Output is correct |