#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define all(s) s.begin(),s.end()
#define rall(s) s.rbegin(),s.rend()
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
ll n,a,b;
cin>>n>>a>>b;
ll l[n+5],r[n+5];
ll g=a/gcd((b+1)%a,a),MX=1e18+1;
if(MX/b>=g){
g=g*b;
}
else{
g=MX;
}
ll S=0;
for(ll i=1;i<=n;i++){
cin>>l[i]>>r[i];
S+=r[i]-l[i]+1;
}
if(n==1){
cout<<min(r[1]-l[1]+1,g);
return 0;
}
if(S<=1000000){
set<pair<ll,ll>>st;
for(ll i=1;i<=n;i++){
for(ll t=l[i];t<=r[i];t++){
st.insert({(t+t/b)%a,t%b});
}
}
cout<<st.size();
return 0;
}
if(g<=1000000){
vector<ll>add(g,0),sub(g,0);
for(ll i=1;i<=n;i++){
if(r[i]-l[i]+1>=g){
cout<<g;
return 0;
}
ll L=l[i]%g,R=r[i]%g;
if(L<=R){
add[L]++;
sub[R]++;
}
else{
add[L]++;
sub[g-1]--;
add[0]++;
sub[R]--;
}
}
ll cur=0,ans=0;
for(ll i=0;i<g;i++){
cur+=add[i];
if(cur>0) ans++;
cur-=sub[i];
}
cout<<ans;
return 0;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
34 ms |
12460 KB |
Output is correct |
3 |
Correct |
52 ms |
18516 KB |
Output is correct |
4 |
Correct |
2 ms |
856 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 |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
6 ms |
1116 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
31 ms |
6980 KB |
Output is correct |
16 |
Correct |
21 ms |
7248 KB |
Output is correct |
17 |
Correct |
44 ms |
11852 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 |
348 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 |
112 ms |
32428 KB |
Output is correct |
3 |
Correct |
149 ms |
32080 KB |
Output is correct |
4 |
Correct |
97 ms |
30680 KB |
Output is correct |
5 |
Correct |
213 ms |
56904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
233 ms |
15900 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
233 ms |
15900 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
233 ms |
15900 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
24 ms |
1880 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
34 ms |
12460 KB |
Output is correct |
3 |
Correct |
52 ms |
18516 KB |
Output is correct |
4 |
Correct |
2 ms |
856 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 |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
6 ms |
1116 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
31 ms |
6980 KB |
Output is correct |
16 |
Correct |
21 ms |
7248 KB |
Output is correct |
17 |
Correct |
44 ms |
11852 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 |
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 |
112 ms |
32428 KB |
Output is correct |
26 |
Correct |
149 ms |
32080 KB |
Output is correct |
27 |
Correct |
97 ms |
30680 KB |
Output is correct |
28 |
Correct |
213 ms |
56904 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Incorrect |
233 ms |
15900 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |