#include <bits/stdc++.h>
#define pb push_back
// #define endl ("\n")
#define all(aa) aa.begin(), aa.end()
typedef long long ll;
using namespace std;
ll ggcd(ll a, ll b){
if(b == 0) return a;
return ggcd(b, a % b);
}
const ll inf = ll(1e18) + 1;
int main(){
ll n, a, b;
cin >> n >> a >> b;
vector<pair<ll, ll>> v(n);
ll mx = 0;
for(int i = 0; i < n; i++){
ll l, r;
cin >> l >> r;
v[i] = {l, r};
mx = max(mx, r);
}
ll g = ggcd(a, b + 1);
ll sade = a / g;
ll mod;
if(double(sade) * double(b) >= double(inf)){
mod = inf;
}
else mod = sade * b;
multiset<pair<ll, int>> s;
for(int i = 0; i < n; i++){
ll tl = v[i].first;
ll tr = v[i].second;
ll sub = tl / mod;
tl -= sub * mod;
tr -= sub * mod;
if(tr - tl + 1 >= mod){
cout << mod << endl;
exit(0);
}
if(tr >= mod){
s.insert({0, 0});
s.insert({tr - mod, 1});
s.insert({mod - 1, 1});
s.insert({tl, 0});
}
else{
s.insert({tl, 0});
s.insert({tr, 1});
}
}
ll ans = 0;
int cnt = 0;
ll beg = -1;
for(auto it = s.begin(); it != s.end(); it++){
if(it->second == 0 && cnt == 0){
beg = it->first;
}
if(it->second == 0){
cnt++;
}
if(it->second == 1 && cnt == 1){
ans += it->first - beg + 1;
}
if(it->second == 1)
cnt--;
}
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
11 ms |
2392 KB |
Output is correct |
3 |
Correct |
10 ms |
2004 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
9 ms |
1628 KB |
Output is correct |
17 |
Correct |
100 ms |
14996 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1036 ms |
144480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1063 ms |
145992 KB |
Output is correct |
3 |
Correct |
1122 ms |
153628 KB |
Output is correct |
4 |
Correct |
1072 ms |
153540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1063 ms |
145992 KB |
Output is correct |
3 |
Correct |
1122 ms |
153628 KB |
Output is correct |
4 |
Correct |
1072 ms |
153540 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1062 ms |
153508 KB |
Output is correct |
7 |
Correct |
1088 ms |
153760 KB |
Output is correct |
8 |
Correct |
1126 ms |
153584 KB |
Output is correct |
9 |
Correct |
1127 ms |
153472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1063 ms |
145992 KB |
Output is correct |
3 |
Correct |
1122 ms |
153628 KB |
Output is correct |
4 |
Correct |
1072 ms |
153540 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
105 ms |
16212 KB |
Output is correct |
7 |
Correct |
94 ms |
16212 KB |
Output is correct |
8 |
Correct |
94 ms |
16212 KB |
Output is correct |
9 |
Correct |
95 ms |
16072 KB |
Output is correct |
10 |
Correct |
90 ms |
16208 KB |
Output is correct |
11 |
Correct |
95 ms |
16200 KB |
Output is correct |
12 |
Correct |
105 ms |
16208 KB |
Output is correct |
13 |
Correct |
108 ms |
16200 KB |
Output is correct |
14 |
Correct |
98 ms |
16132 KB |
Output is correct |
15 |
Correct |
107 ms |
16212 KB |
Output is correct |
16 |
Correct |
104 ms |
16212 KB |
Output is correct |
17 |
Correct |
100 ms |
16212 KB |
Output is correct |
18 |
Correct |
1055 ms |
153680 KB |
Output is correct |
19 |
Correct |
1156 ms |
153384 KB |
Output is correct |
20 |
Correct |
1240 ms |
153884 KB |
Output is correct |
21 |
Correct |
99 ms |
16592 KB |
Output is correct |
22 |
Correct |
108 ms |
16680 KB |
Output is correct |
23 |
Correct |
399 ms |
55560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
118 ms |
16980 KB |
Output is correct |
3 |
Correct |
125 ms |
16752 KB |
Output is correct |
4 |
Correct |
1109 ms |
159876 KB |
Output is correct |
5 |
Correct |
145 ms |
16672 KB |
Output is correct |
6 |
Correct |
108 ms |
16720 KB |
Output is correct |
7 |
Correct |
112 ms |
16724 KB |
Output is correct |
8 |
Correct |
108 ms |
16668 KB |
Output is correct |
9 |
Correct |
151 ms |
16748 KB |
Output is correct |
10 |
Correct |
118 ms |
16736 KB |
Output is correct |
11 |
Correct |
103 ms |
16680 KB |
Output is correct |
12 |
Correct |
128 ms |
16724 KB |
Output is correct |
13 |
Correct |
107 ms |
16632 KB |
Output is correct |
14 |
Correct |
1145 ms |
159596 KB |
Output is correct |
15 |
Correct |
95 ms |
16816 KB |
Output is correct |
16 |
Correct |
1140 ms |
159644 KB |
Output is correct |
17 |
Correct |
1150 ms |
159740 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
11 ms |
2392 KB |
Output is correct |
3 |
Correct |
10 ms |
2004 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
9 ms |
1628 KB |
Output is correct |
17 |
Correct |
100 ms |
14996 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1036 ms |
144480 KB |
Output is correct |
29 |
Correct |
0 ms |
344 KB |
Output is correct |
30 |
Correct |
1063 ms |
145992 KB |
Output is correct |
31 |
Correct |
1122 ms |
153628 KB |
Output is correct |
32 |
Correct |
1072 ms |
153540 KB |
Output is correct |
33 |
Correct |
0 ms |
344 KB |
Output is correct |
34 |
Correct |
1062 ms |
153508 KB |
Output is correct |
35 |
Correct |
1088 ms |
153760 KB |
Output is correct |
36 |
Correct |
1126 ms |
153584 KB |
Output is correct |
37 |
Correct |
1127 ms |
153472 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
105 ms |
16212 KB |
Output is correct |
40 |
Correct |
94 ms |
16212 KB |
Output is correct |
41 |
Correct |
94 ms |
16212 KB |
Output is correct |
42 |
Correct |
95 ms |
16072 KB |
Output is correct |
43 |
Correct |
90 ms |
16208 KB |
Output is correct |
44 |
Correct |
95 ms |
16200 KB |
Output is correct |
45 |
Correct |
105 ms |
16208 KB |
Output is correct |
46 |
Correct |
108 ms |
16200 KB |
Output is correct |
47 |
Correct |
98 ms |
16132 KB |
Output is correct |
48 |
Correct |
107 ms |
16212 KB |
Output is correct |
49 |
Correct |
104 ms |
16212 KB |
Output is correct |
50 |
Correct |
100 ms |
16212 KB |
Output is correct |
51 |
Correct |
1055 ms |
153680 KB |
Output is correct |
52 |
Correct |
1156 ms |
153384 KB |
Output is correct |
53 |
Correct |
1240 ms |
153884 KB |
Output is correct |
54 |
Correct |
99 ms |
16592 KB |
Output is correct |
55 |
Correct |
108 ms |
16680 KB |
Output is correct |
56 |
Correct |
399 ms |
55560 KB |
Output is correct |
57 |
Correct |
0 ms |
348 KB |
Output is correct |
58 |
Correct |
118 ms |
16980 KB |
Output is correct |
59 |
Correct |
125 ms |
16752 KB |
Output is correct |
60 |
Correct |
1109 ms |
159876 KB |
Output is correct |
61 |
Correct |
145 ms |
16672 KB |
Output is correct |
62 |
Correct |
108 ms |
16720 KB |
Output is correct |
63 |
Correct |
112 ms |
16724 KB |
Output is correct |
64 |
Correct |
108 ms |
16668 KB |
Output is correct |
65 |
Correct |
151 ms |
16748 KB |
Output is correct |
66 |
Correct |
118 ms |
16736 KB |
Output is correct |
67 |
Correct |
103 ms |
16680 KB |
Output is correct |
68 |
Correct |
128 ms |
16724 KB |
Output is correct |
69 |
Correct |
107 ms |
16632 KB |
Output is correct |
70 |
Correct |
1145 ms |
159596 KB |
Output is correct |
71 |
Correct |
95 ms |
16816 KB |
Output is correct |
72 |
Correct |
1140 ms |
159644 KB |
Output is correct |
73 |
Correct |
1150 ms |
159740 KB |
Output is correct |
74 |
Correct |
0 ms |
348 KB |
Output is correct |
75 |
Correct |
0 ms |
348 KB |
Output is correct |
76 |
Correct |
0 ms |
348 KB |
Output is correct |
77 |
Correct |
0 ms |
348 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
10 ms |
2000 KB |
Output is correct |
80 |
Correct |
1183 ms |
168108 KB |
Output is correct |
81 |
Correct |
1211 ms |
168020 KB |
Output is correct |
82 |
Correct |
1188 ms |
168092 KB |
Output is correct |
83 |
Correct |
1228 ms |
167844 KB |
Output is correct |
84 |
Correct |
1158 ms |
168108 KB |
Output is correct |
85 |
Correct |
1206 ms |
168156 KB |
Output is correct |
86 |
Correct |
433 ms |
57976 KB |
Output is correct |
87 |
Correct |
0 ms |
348 KB |
Output is correct |