题意:让你去在[l,r]之间有多少个x满足
解:
1.由两个方程知道这个x一定满足 x%a1==(b1%a1) and x%a2==(b2%a2)
2.题目转化为 上面两个模线性方程组的在[l,r]内解的数量
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define en '\n'
#define ll long long
const ll maxm =1e7+10;
const ll maxn =5e5+10;
using namespace std;
void rep(ll &x,ll p){
x%=p;x+=p;x%=p;
}
ll gcd(ll a,ll b){
if(b==0)return a;return gcd(b,a%b);
}
ll lcm(ll a,ll b){
return a*b/gcd(a,b);
}
ll exgcd(ll a,ll b,ll &x,ll &y){
if(!b){
x=1;y=0;return a;
}ll d=exgcd(b,a%b,x,y);ll z=x;x=y;y=z-(a/b)*y;return d;
}
ll run(ll x,ll lim,ll dis){
lim-=x;
return (lim/dis)+(lim>=0);/// 不大于等于0也会被卡 卡的是例子是一个 3 [3,7]的例子
}
const ll inf=3e9;
ll mul(ll a,ll b,ll p){
ll res=0;
while(b){
if(b&1){
res+=a;
res%=p;
}
a=(a+a)%p;
b>>=1;
}return res;
}
signed main() {
#ifdef local
freopen("input2.txt","r", stdin);
#endif // local
ll a1,a2,b1,b2,l,r;
//cout<<(577593%394)%<<en;
cin>>a1>>b1>>a2>>b2>>l>>r;
ll b11=b1%a1;
ll b22=b2%a2;
ll x=b11;
ll m=a1;
ll xx,yy;
ll t=b22-x;
l=max({l,b1,b2});
ll d=exgcd(a1,a2,xx,yy);
if((b11-b22)%d){/// 第60个点卡了 说明两个方程没有公共解
cout<<0<<en;
return 0;
}
xx*=t/d;
m=lcm(a1,a2);
x+=mul(xx,a1,m);/// 第113个点卡了:会爆ll
///88888888888888888888888888888888888888888888888888888888888888888
x+=inf;
l+=inf;
r+=inf;/// 向上调整不用考虑负数了
rep(x,m);
ll ans=run(x,r,m)-run(x,l-1,m);
ans=max(ans,1ll*0);
cout<<ans<<en;
return 0;
}