#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mp make_pair
#define pii pair<int,int>
#define fi first
#define se second
const int INF=1000000000000000000;
int gcd(int a,int b){
if(b==0 || a==0) return a+b;
if(a<b) swap(a,b);
return gcd(b,a%b);
}
void solve(){
int n,a,b;
cin >> n >> a >> b;
int bp=a/gcd(a,b+1LL);
int p;
if(bp>INF/b){
p=INF;
} else p=(bp*b);
vector<pii> ranges;
for(int i=0;i<n;i++){
int l,r;
cin >> l >> r;
ranges.push_back(mp(l,r));
}
set<int> imppts;
imppts.insert(0);
imppts.insert(p);
map<int,int> cnt;
for(auto [l,r]:ranges){
if(r-l+1LL>=p){
cout << p << "\n";
return;
}
r%=p;
l%=p;
imppts.insert(l);
imppts.insert(r+1);
cnt[l]++;
cnt[r+1]--;
if(l>r){
cnt[0]++;
cnt[p]--;
}
}
int ccnt=0;
int prev=0;
int ans=0;
for(int i:imppts){
if(ccnt>0) ans+=(i-prev);
ccnt+=cnt[i];
prev=i;
}
cout << ans;
}
signed main(){
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
t=1;
//cin >> t;
while(t--) solve();
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
7 ms |
1992 KB |
Output is correct |
3 |
Correct |
7 ms |
2012 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 |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
7 ms |
2132 KB |
Output is correct |
17 |
Correct |
76 ms |
16720 KB |
Output is correct |
18 |
Correct |
0 ms |
356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
4 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
281 ms |
41384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
807 ms |
163300 KB |
Output is correct |
3 |
Correct |
843 ms |
162728 KB |
Output is correct |
4 |
Correct |
919 ms |
162732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
807 ms |
163300 KB |
Output is correct |
3 |
Correct |
843 ms |
162728 KB |
Output is correct |
4 |
Correct |
919 ms |
162732 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
832 ms |
163052 KB |
Output is correct |
7 |
Correct |
867 ms |
162708 KB |
Output is correct |
8 |
Correct |
816 ms |
162728 KB |
Output is correct |
9 |
Correct |
993 ms |
162796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
807 ms |
163300 KB |
Output is correct |
3 |
Correct |
843 ms |
162728 KB |
Output is correct |
4 |
Correct |
919 ms |
162732 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
66 ms |
16608 KB |
Output is correct |
7 |
Correct |
69 ms |
16840 KB |
Output is correct |
8 |
Correct |
65 ms |
16584 KB |
Output is correct |
9 |
Correct |
71 ms |
16584 KB |
Output is correct |
10 |
Correct |
65 ms |
16588 KB |
Output is correct |
11 |
Correct |
64 ms |
16708 KB |
Output is correct |
12 |
Correct |
71 ms |
16848 KB |
Output is correct |
13 |
Correct |
74 ms |
16640 KB |
Output is correct |
14 |
Correct |
66 ms |
16480 KB |
Output is correct |
15 |
Correct |
82 ms |
16584 KB |
Output is correct |
16 |
Correct |
87 ms |
16580 KB |
Output is correct |
17 |
Correct |
70 ms |
16588 KB |
Output is correct |
18 |
Correct |
849 ms |
163680 KB |
Output is correct |
19 |
Correct |
905 ms |
162884 KB |
Output is correct |
20 |
Correct |
941 ms |
163016 KB |
Output is correct |
21 |
Correct |
68 ms |
16580 KB |
Output is correct |
22 |
Correct |
72 ms |
16680 KB |
Output is correct |
23 |
Correct |
90 ms |
19892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
81 ms |
16584 KB |
Output is correct |
3 |
Correct |
81 ms |
16592 KB |
Output is correct |
4 |
Correct |
936 ms |
163244 KB |
Output is correct |
5 |
Correct |
83 ms |
16584 KB |
Output is correct |
6 |
Correct |
80 ms |
16584 KB |
Output is correct |
7 |
Correct |
85 ms |
16580 KB |
Output is correct |
8 |
Correct |
80 ms |
16584 KB |
Output is correct |
9 |
Correct |
80 ms |
16580 KB |
Output is correct |
10 |
Correct |
70 ms |
16536 KB |
Output is correct |
11 |
Correct |
73 ms |
16580 KB |
Output is correct |
12 |
Correct |
73 ms |
16664 KB |
Output is correct |
13 |
Correct |
82 ms |
16584 KB |
Output is correct |
14 |
Correct |
990 ms |
162568 KB |
Output is correct |
15 |
Correct |
68 ms |
16584 KB |
Output is correct |
16 |
Correct |
857 ms |
162868 KB |
Output is correct |
17 |
Correct |
809 ms |
162972 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
7 ms |
1992 KB |
Output is correct |
3 |
Correct |
7 ms |
2012 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 |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
7 ms |
2132 KB |
Output is correct |
17 |
Correct |
76 ms |
16720 KB |
Output is correct |
18 |
Correct |
0 ms |
356 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
512 KB |
Output is correct |
26 |
Correct |
4 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
281 ms |
41384 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
807 ms |
163300 KB |
Output is correct |
31 |
Correct |
843 ms |
162728 KB |
Output is correct |
32 |
Correct |
919 ms |
162732 KB |
Output is correct |
33 |
Correct |
0 ms |
344 KB |
Output is correct |
34 |
Correct |
832 ms |
163052 KB |
Output is correct |
35 |
Correct |
867 ms |
162708 KB |
Output is correct |
36 |
Correct |
816 ms |
162728 KB |
Output is correct |
37 |
Correct |
993 ms |
162796 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
66 ms |
16608 KB |
Output is correct |
40 |
Correct |
69 ms |
16840 KB |
Output is correct |
41 |
Correct |
65 ms |
16584 KB |
Output is correct |
42 |
Correct |
71 ms |
16584 KB |
Output is correct |
43 |
Correct |
65 ms |
16588 KB |
Output is correct |
44 |
Correct |
64 ms |
16708 KB |
Output is correct |
45 |
Correct |
71 ms |
16848 KB |
Output is correct |
46 |
Correct |
74 ms |
16640 KB |
Output is correct |
47 |
Correct |
66 ms |
16480 KB |
Output is correct |
48 |
Correct |
82 ms |
16584 KB |
Output is correct |
49 |
Correct |
87 ms |
16580 KB |
Output is correct |
50 |
Correct |
70 ms |
16588 KB |
Output is correct |
51 |
Correct |
849 ms |
163680 KB |
Output is correct |
52 |
Correct |
905 ms |
162884 KB |
Output is correct |
53 |
Correct |
941 ms |
163016 KB |
Output is correct |
54 |
Correct |
68 ms |
16580 KB |
Output is correct |
55 |
Correct |
72 ms |
16680 KB |
Output is correct |
56 |
Correct |
90 ms |
19892 KB |
Output is correct |
57 |
Correct |
0 ms |
344 KB |
Output is correct |
58 |
Correct |
81 ms |
16584 KB |
Output is correct |
59 |
Correct |
81 ms |
16592 KB |
Output is correct |
60 |
Correct |
936 ms |
163244 KB |
Output is correct |
61 |
Correct |
83 ms |
16584 KB |
Output is correct |
62 |
Correct |
80 ms |
16584 KB |
Output is correct |
63 |
Correct |
85 ms |
16580 KB |
Output is correct |
64 |
Correct |
80 ms |
16584 KB |
Output is correct |
65 |
Correct |
80 ms |
16580 KB |
Output is correct |
66 |
Correct |
70 ms |
16536 KB |
Output is correct |
67 |
Correct |
73 ms |
16580 KB |
Output is correct |
68 |
Correct |
73 ms |
16664 KB |
Output is correct |
69 |
Correct |
82 ms |
16584 KB |
Output is correct |
70 |
Correct |
990 ms |
162568 KB |
Output is correct |
71 |
Correct |
68 ms |
16584 KB |
Output is correct |
72 |
Correct |
857 ms |
162868 KB |
Output is correct |
73 |
Correct |
809 ms |
162972 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 |
460 KB |
Output is correct |
77 |
Correct |
0 ms |
348 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
8 ms |
1920 KB |
Output is correct |
80 |
Correct |
927 ms |
162736 KB |
Output is correct |
81 |
Correct |
912 ms |
163508 KB |
Output is correct |
82 |
Correct |
1025 ms |
162736 KB |
Output is correct |
83 |
Correct |
1012 ms |
162568 KB |
Output is correct |
84 |
Correct |
1012 ms |
163560 KB |
Output is correct |
85 |
Correct |
926 ms |
162732 KB |
Output is correct |
86 |
Correct |
90 ms |
20268 KB |
Output is correct |
87 |
Correct |
0 ms |
348 KB |
Output is correct |