题意:
分析:
先暴力找规律
\[x=1,2,3\dots A-1+\lfloor\frac{A-1}{B}\rfloor,A+\lfloor\frac{A}{B}\rfloor|A+1+\lfloor\frac{A+1}{B}\rfloor,A+2+\lfloor\frac{A+2}{B}\rfloor\dots 2A+\lfloor\frac{2A}{B}\rfloor \\ y=1,2,3,\dots B-1,0|1,2,3\dots B-1,0 \]貌似没有任何规律
所以我们换个方式,找最小循环节 \(t\),由定义可得
\[\begin{cases} x+\lfloor \frac{x}{B}\rfloor\equiv x+t+\lfloor\frac{t+x}{B}\rfloor\ (\bmod A) \\ x\equiv x+t\ (\bmod B) \end{cases} \]令 \(t=kB\) 联立可得
\[x+\lfloor \frac{x}{B}\rfloor\equiv x+kB+\lfloor\frac{kB+x}{B}\rfloor\ (\bmod A) \\ kB+k\equiv0\ (\bmod A) \\ A|k(B+1) \\ \frac{A}{\gcd(A,B+1)}|k \\ \frac{AB}{\gcd(A,B+1)}|t \]所以 \(t\) 的最小周期为 \(\frac{AB}{\gcd(A,B+1)}\),然后我们的题意就转换为在 \([0,t-1]\) 的区间内求线段并集的大小
代码:
#include<bits/stdc++.h>
#define inl inline
#define reg register
#define mk(x,y) make_pair(x,y)
#define fir first
#define sec second
#define pb push_back
using namespace std;
namespace zzc
{
typedef long long ll;
inl ll read()
{
ll x=0,f=1;char ch=getchar();
while(!isdigit(ch)) {if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch)) {x=x*10+ch-48;ch=getchar();}
return x*f;
}
vector<pair<ll,ll> > v;
ll n,t,ans,a,b;
void work()
{
ll l,r;
n=read();a=read();b=read();
t=a/__gcd(a,b+1)*b;
for(reg int i=1;i<=n;i++)
{
l=read();r=read();
if(r-l+1>=t)
{
printf("%lld\n",t);
return ;
}
l%=t;r%=t;
if(l<=r) v.pb(mk(l,r));
else v.pb(mk(0,r)),v.pb(mk(l,t-1));
}
sort(v.begin(),v.end());
l=v[0].fir,r=v[0].sec;
for(auto x:v)
{
if(x.fir<=r) r=max(r,x.sec);
else
{
ans+=r-l+1;
l=x.fir;r=x.sec;
}
}
ans+=r-l+1;
printf("%lld\n",ans);
}
}
int main()
{
zzc::work();
return 0;
}