已知整数x,y满足如下面的条件:
ax+by+c = 0
p<=x<=q
r<=y<=s
求满足这些条件的x,y的个数。
#include<iostream>
#define ll long long
using namespace std;
ll n,a,b,c,p,q,r,s;
ll exgcd(ll a,ll b,ll &x,ll &y){
if (b==0){
x=1;
y=0;
return a;
}
ll r=exgcd(b,a%b,x,y);
ll t=x;
x=y;
y=t-a/b*y;
return r;
}
int pd(ll x,ll l,ll r){
if (x>=l&&x<=r) return 1;
return 0;
}
int main(){
cin>>n;
for (int i=1;i<=n;i++){
ll ans=0;
cin>>a>>b>>c>>p>>q>>r>>s;
c=-c;
ll x=0,y=0;
if (a==0&&b==0&&c!=0) {
cout<<"0"<<"\n";
continue;
}
if (p>q||r>s){
cout<<"0"<<"\n";
continue;
}
if (a==0&&b==0&&c==0){
cout<<(q-p+1)*(s-r+1)<<"\n";
continue;
}
int d=exgcd(a,b,x,y);
if (c%d!=0){
cout<<"0\n";
continue;
}
x=x*c/d;
y=y*c/d;
int t1,t2;
if (b==0){
if (x>=p&&x<=q) cout<<"1\n";
else cout<<"0\n";
continue;
}
else if ((float)d/b>0){
t1=(p-x)*d/b;
t2=(q-x)*d/b;
}
else{
t1=(q-x)*d/b;
t2=(p-x)*d/b;
}
for (ll k=t1;k<=t2;k++)
if (pd(x+k*b/d,p,q)&&pd(y-k*a/d,r,s)) ans++;
cout<<ans<<endl;
}
}