#include <bits/stdc++.h>
using namespace std;
int main(){
long long n, a, b, ans=0;
cin>>n>>a>>b;
if (log(a)+log(b)>log(__gcd(a,b+1))+log(1e18)){
for (int i =0; i<n; ++i){
long long li,ri;cin>>li>>ri;
ans += ri-li+1;
}
cout<<ans<<endl;
return 0;
}
long long s = a/__gcd(a,b+1)*b;
set<pair<long long, long long>> st;
for (int i = 0; i<n;++i){
long long li, ri; cin >> li >> ri;
if (ri-li>=s) st.insert({0,s-1});
else if (ri%s<li%s){
st.insert({0,ri%s});
st.insert({li%s,s-1});
}
else st.insert({li%s,ri%s});
}
ans =s;
long long x = -1;
for (auto el : st) {
ans -= max (el.first-x-1, 0LL);
x = max (x, el.second);
}
ans -= s-1-x;
cout << ans << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
16 ms |
1196 KB |
Output is correct |
3 |
Correct |
21 ms |
1228 KB |
Output is correct |
4 |
Correct |
0 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 |
212 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 |
296 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
17 ms |
1216 KB |
Output is correct |
17 |
Correct |
171 ms |
10164 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 |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 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 |
304 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
316 KB |
Output is correct |
5 |
Correct |
918 ms |
7960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1539 ms |
99964 KB |
Output is correct |
3 |
Correct |
1542 ms |
100132 KB |
Output is correct |
4 |
Correct |
1554 ms |
99976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1539 ms |
99964 KB |
Output is correct |
3 |
Correct |
1542 ms |
100132 KB |
Output is correct |
4 |
Correct |
1554 ms |
99976 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1570 ms |
100016 KB |
Output is correct |
7 |
Correct |
1520 ms |
100044 KB |
Output is correct |
8 |
Correct |
1552 ms |
100036 KB |
Output is correct |
9 |
Correct |
1610 ms |
99960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1539 ms |
99964 KB |
Output is correct |
3 |
Correct |
1542 ms |
100132 KB |
Output is correct |
4 |
Correct |
1554 ms |
99976 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
159 ms |
10244 KB |
Output is correct |
7 |
Correct |
155 ms |
10244 KB |
Output is correct |
8 |
Correct |
147 ms |
10252 KB |
Output is correct |
9 |
Correct |
148 ms |
10224 KB |
Output is correct |
10 |
Correct |
160 ms |
10260 KB |
Output is correct |
11 |
Correct |
159 ms |
10388 KB |
Output is correct |
12 |
Correct |
152 ms |
10224 KB |
Output is correct |
13 |
Correct |
152 ms |
10188 KB |
Output is correct |
14 |
Correct |
170 ms |
10328 KB |
Output is correct |
15 |
Correct |
148 ms |
10244 KB |
Output is correct |
16 |
Correct |
162 ms |
10308 KB |
Output is correct |
17 |
Correct |
198 ms |
10212 KB |
Output is correct |
18 |
Correct |
1574 ms |
100088 KB |
Output is correct |
19 |
Correct |
1556 ms |
99912 KB |
Output is correct |
20 |
Correct |
1637 ms |
99724 KB |
Output is correct |
21 |
Correct |
157 ms |
10164 KB |
Output is correct |
22 |
Correct |
161 ms |
10308 KB |
Output is correct |
23 |
Correct |
445 ms |
12844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
157 ms |
10148 KB |
Output is correct |
3 |
Correct |
168 ms |
10336 KB |
Output is correct |
4 |
Correct |
1680 ms |
99872 KB |
Output is correct |
5 |
Correct |
156 ms |
10316 KB |
Output is correct |
6 |
Correct |
181 ms |
10164 KB |
Output is correct |
7 |
Correct |
160 ms |
10244 KB |
Output is correct |
8 |
Correct |
149 ms |
10208 KB |
Output is correct |
9 |
Correct |
156 ms |
10176 KB |
Output is correct |
10 |
Correct |
164 ms |
10264 KB |
Output is correct |
11 |
Correct |
155 ms |
10140 KB |
Output is correct |
12 |
Correct |
157 ms |
10360 KB |
Output is correct |
13 |
Correct |
173 ms |
10244 KB |
Output is correct |
14 |
Correct |
1628 ms |
100020 KB |
Output is correct |
15 |
Correct |
143 ms |
10216 KB |
Output is correct |
16 |
Correct |
1522 ms |
100144 KB |
Output is correct |
17 |
Correct |
1536 ms |
100060 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 |
16 ms |
1196 KB |
Output is correct |
3 |
Correct |
21 ms |
1228 KB |
Output is correct |
4 |
Correct |
0 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 |
212 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 |
296 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
17 ms |
1216 KB |
Output is correct |
17 |
Correct |
171 ms |
10164 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 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 |
304 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
316 KB |
Output is correct |
28 |
Correct |
918 ms |
7960 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1539 ms |
99964 KB |
Output is correct |
31 |
Correct |
1542 ms |
100132 KB |
Output is correct |
32 |
Correct |
1554 ms |
99976 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1570 ms |
100016 KB |
Output is correct |
35 |
Correct |
1520 ms |
100044 KB |
Output is correct |
36 |
Correct |
1552 ms |
100036 KB |
Output is correct |
37 |
Correct |
1610 ms |
99960 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
159 ms |
10244 KB |
Output is correct |
40 |
Correct |
155 ms |
10244 KB |
Output is correct |
41 |
Correct |
147 ms |
10252 KB |
Output is correct |
42 |
Correct |
148 ms |
10224 KB |
Output is correct |
43 |
Correct |
160 ms |
10260 KB |
Output is correct |
44 |
Correct |
159 ms |
10388 KB |
Output is correct |
45 |
Correct |
152 ms |
10224 KB |
Output is correct |
46 |
Correct |
152 ms |
10188 KB |
Output is correct |
47 |
Correct |
170 ms |
10328 KB |
Output is correct |
48 |
Correct |
148 ms |
10244 KB |
Output is correct |
49 |
Correct |
162 ms |
10308 KB |
Output is correct |
50 |
Correct |
198 ms |
10212 KB |
Output is correct |
51 |
Correct |
1574 ms |
100088 KB |
Output is correct |
52 |
Correct |
1556 ms |
99912 KB |
Output is correct |
53 |
Correct |
1637 ms |
99724 KB |
Output is correct |
54 |
Correct |
157 ms |
10164 KB |
Output is correct |
55 |
Correct |
161 ms |
10308 KB |
Output is correct |
56 |
Correct |
445 ms |
12844 KB |
Output is correct |
57 |
Correct |
1 ms |
212 KB |
Output is correct |
58 |
Correct |
157 ms |
10148 KB |
Output is correct |
59 |
Correct |
168 ms |
10336 KB |
Output is correct |
60 |
Correct |
1680 ms |
99872 KB |
Output is correct |
61 |
Correct |
156 ms |
10316 KB |
Output is correct |
62 |
Correct |
181 ms |
10164 KB |
Output is correct |
63 |
Correct |
160 ms |
10244 KB |
Output is correct |
64 |
Correct |
149 ms |
10208 KB |
Output is correct |
65 |
Correct |
156 ms |
10176 KB |
Output is correct |
66 |
Correct |
164 ms |
10264 KB |
Output is correct |
67 |
Correct |
155 ms |
10140 KB |
Output is correct |
68 |
Correct |
157 ms |
10360 KB |
Output is correct |
69 |
Correct |
173 ms |
10244 KB |
Output is correct |
70 |
Correct |
1628 ms |
100020 KB |
Output is correct |
71 |
Correct |
143 ms |
10216 KB |
Output is correct |
72 |
Correct |
1522 ms |
100144 KB |
Output is correct |
73 |
Correct |
1536 ms |
100060 KB |
Output is correct |
74 |
Correct |
1 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
304 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 |
300 KB |
Output is correct |
79 |
Correct |
15 ms |
1248 KB |
Output is correct |
80 |
Correct |
1574 ms |
100140 KB |
Output is correct |
81 |
Correct |
1583 ms |
99928 KB |
Output is correct |
82 |
Correct |
1563 ms |
99656 KB |
Output is correct |
83 |
Correct |
1572 ms |
98904 KB |
Output is correct |
84 |
Correct |
1641 ms |
99996 KB |
Output is correct |
85 |
Correct |
1570 ms |
99512 KB |
Output is correct |
86 |
Correct |
433 ms |
12816 KB |
Output is correct |
87 |
Correct |
1 ms |
212 KB |
Output is correct |