#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
9 ms |
2004 KB |
Output is correct |
3 |
Correct |
9 ms |
2124 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 |
344 KB |
Output is correct |
11 |
Correct |
0 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 |
8 ms |
1628 KB |
Output is correct |
17 |
Correct |
100 ms |
15696 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
3 ms |
600 KB |
Output is correct |
5 |
Correct |
976 ms |
155744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1143 ms |
161956 KB |
Output is correct |
3 |
Correct |
1116 ms |
162820 KB |
Output is correct |
4 |
Correct |
1111 ms |
162820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1143 ms |
161956 KB |
Output is correct |
3 |
Correct |
1116 ms |
162820 KB |
Output is correct |
4 |
Correct |
1111 ms |
162820 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1123 ms |
162868 KB |
Output is correct |
7 |
Correct |
1071 ms |
163084 KB |
Output is correct |
8 |
Correct |
1088 ms |
162624 KB |
Output is correct |
9 |
Correct |
1136 ms |
162656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1143 ms |
161956 KB |
Output is correct |
3 |
Correct |
1116 ms |
162820 KB |
Output is correct |
4 |
Correct |
1111 ms |
162820 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
115 ms |
16888 KB |
Output is correct |
7 |
Correct |
97 ms |
16980 KB |
Output is correct |
8 |
Correct |
99 ms |
17060 KB |
Output is correct |
9 |
Correct |
95 ms |
16976 KB |
Output is correct |
10 |
Correct |
101 ms |
17064 KB |
Output is correct |
11 |
Correct |
94 ms |
16976 KB |
Output is correct |
12 |
Correct |
102 ms |
17020 KB |
Output is correct |
13 |
Correct |
97 ms |
16984 KB |
Output is correct |
14 |
Correct |
93 ms |
16976 KB |
Output is correct |
15 |
Correct |
101 ms |
16868 KB |
Output is correct |
16 |
Correct |
115 ms |
16980 KB |
Output is correct |
17 |
Correct |
103 ms |
17100 KB |
Output is correct |
18 |
Correct |
1050 ms |
162896 KB |
Output is correct |
19 |
Correct |
1118 ms |
162760 KB |
Output is correct |
20 |
Correct |
1217 ms |
162648 KB |
Output is correct |
21 |
Correct |
94 ms |
16720 KB |
Output is correct |
22 |
Correct |
134 ms |
16724 KB |
Output is correct |
23 |
Correct |
385 ms |
54920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
113 ms |
16772 KB |
Output is correct |
3 |
Correct |
137 ms |
16896 KB |
Output is correct |
4 |
Correct |
1130 ms |
159868 KB |
Output is correct |
5 |
Correct |
109 ms |
16724 KB |
Output is correct |
6 |
Correct |
106 ms |
16680 KB |
Output is correct |
7 |
Correct |
97 ms |
16720 KB |
Output is correct |
8 |
Correct |
96 ms |
16724 KB |
Output is correct |
9 |
Correct |
98 ms |
16792 KB |
Output is correct |
10 |
Correct |
95 ms |
16776 KB |
Output is correct |
11 |
Correct |
101 ms |
16728 KB |
Output is correct |
12 |
Correct |
105 ms |
16848 KB |
Output is correct |
13 |
Correct |
97 ms |
16804 KB |
Output is correct |
14 |
Correct |
1110 ms |
159508 KB |
Output is correct |
15 |
Correct |
93 ms |
16720 KB |
Output is correct |
16 |
Correct |
1055 ms |
159568 KB |
Output is correct |
17 |
Correct |
1073 ms |
159608 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
9 ms |
2004 KB |
Output is correct |
3 |
Correct |
9 ms |
2124 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 |
344 KB |
Output is correct |
11 |
Correct |
0 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 |
8 ms |
1628 KB |
Output is correct |
17 |
Correct |
100 ms |
15696 KB |
Output is correct |
18 |
Correct |
1 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 |
348 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 |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
3 ms |
600 KB |
Output is correct |
28 |
Correct |
976 ms |
155744 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1143 ms |
161956 KB |
Output is correct |
31 |
Correct |
1116 ms |
162820 KB |
Output is correct |
32 |
Correct |
1111 ms |
162820 KB |
Output is correct |
33 |
Correct |
0 ms |
344 KB |
Output is correct |
34 |
Correct |
1123 ms |
162868 KB |
Output is correct |
35 |
Correct |
1071 ms |
163084 KB |
Output is correct |
36 |
Correct |
1088 ms |
162624 KB |
Output is correct |
37 |
Correct |
1136 ms |
162656 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
115 ms |
16888 KB |
Output is correct |
40 |
Correct |
97 ms |
16980 KB |
Output is correct |
41 |
Correct |
99 ms |
17060 KB |
Output is correct |
42 |
Correct |
95 ms |
16976 KB |
Output is correct |
43 |
Correct |
101 ms |
17064 KB |
Output is correct |
44 |
Correct |
94 ms |
16976 KB |
Output is correct |
45 |
Correct |
102 ms |
17020 KB |
Output is correct |
46 |
Correct |
97 ms |
16984 KB |
Output is correct |
47 |
Correct |
93 ms |
16976 KB |
Output is correct |
48 |
Correct |
101 ms |
16868 KB |
Output is correct |
49 |
Correct |
115 ms |
16980 KB |
Output is correct |
50 |
Correct |
103 ms |
17100 KB |
Output is correct |
51 |
Correct |
1050 ms |
162896 KB |
Output is correct |
52 |
Correct |
1118 ms |
162760 KB |
Output is correct |
53 |
Correct |
1217 ms |
162648 KB |
Output is correct |
54 |
Correct |
94 ms |
16720 KB |
Output is correct |
55 |
Correct |
134 ms |
16724 KB |
Output is correct |
56 |
Correct |
385 ms |
54920 KB |
Output is correct |
57 |
Correct |
0 ms |
348 KB |
Output is correct |
58 |
Correct |
113 ms |
16772 KB |
Output is correct |
59 |
Correct |
137 ms |
16896 KB |
Output is correct |
60 |
Correct |
1130 ms |
159868 KB |
Output is correct |
61 |
Correct |
109 ms |
16724 KB |
Output is correct |
62 |
Correct |
106 ms |
16680 KB |
Output is correct |
63 |
Correct |
97 ms |
16720 KB |
Output is correct |
64 |
Correct |
96 ms |
16724 KB |
Output is correct |
65 |
Correct |
98 ms |
16792 KB |
Output is correct |
66 |
Correct |
95 ms |
16776 KB |
Output is correct |
67 |
Correct |
101 ms |
16728 KB |
Output is correct |
68 |
Correct |
105 ms |
16848 KB |
Output is correct |
69 |
Correct |
97 ms |
16804 KB |
Output is correct |
70 |
Correct |
1110 ms |
159508 KB |
Output is correct |
71 |
Correct |
93 ms |
16720 KB |
Output is correct |
72 |
Correct |
1055 ms |
159568 KB |
Output is correct |
73 |
Correct |
1073 ms |
159608 KB |
Output is correct |
74 |
Correct |
0 ms |
344 KB |
Output is correct |
75 |
Correct |
0 ms |
348 KB |
Output is correct |
76 |
Correct |
0 ms |
348 KB |
Output is correct |
77 |
Correct |
1 ms |
348 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
12 ms |
2000 KB |
Output is correct |
80 |
Correct |
1094 ms |
159140 KB |
Output is correct |
81 |
Correct |
1114 ms |
159060 KB |
Output is correct |
82 |
Correct |
1175 ms |
159216 KB |
Output is correct |
83 |
Correct |
1133 ms |
145908 KB |
Output is correct |
84 |
Correct |
1114 ms |
145588 KB |
Output is correct |
85 |
Correct |
1121 ms |
145488 KB |
Output is correct |
86 |
Correct |
392 ms |
49300 KB |
Output is correct |
87 |
Correct |
0 ms |
344 KB |
Output is correct |