This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |