codevs 1213 解的个数(我去年打了个表 - -)

时间:2021-11-02 15:33:27
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int T,x,y,ans,g,l1,r1,l2,r2;
int init()
{
int x=;char s=getchar();bool f=;
while(s<''||s>''){if(s=='-')f=;s=getchar();}
while(s>=''&&s<=''){x=x*+s-'';s=getchar();}
if(f)return -x;return x;
}
void E_gcd(int a,int b)
{
if(b==)
{
x=;y=;g=a;
}
else
{
E_gcd(b,a%b);
int tmp=x;
x=y;
y=tmp-a/b*y;
}
}
int main()
{
int a,b,c,i,j;
T=init();
while(T--)
{
ans=;
a=init();b=init();c=init();
c=-c;
l1=init();r1=init();l2=init();r2=init();
if(a==&&b==)
{
if(c!=||l1>r1||l2>r1)
{
printf("0\n");
continue;
}
long long an,li,ri;
li=r1-l1+;ri=r2-l2+;
an=li*ri;
cout<<an<<endl;
continue;
}
if(a==)
{
y=c/b;
if(y<l2||y>r2||c%b!=)
printf("0\n");
else
printf("1\n");
continue;
}
if(b==)
{
x=c/a;
if(x<l1||x>r1||c%a!=)
printf("0\n");
else
printf("1\n");
continue;
}
E_gcd(a,b);
if(c%g!=)
{
printf("0\n");
continue;
}
int s=c/g;
x*=s;y*=s;
int ai=a/g;
int bi=b/g;
if(x<l1)
{
while(x<l1)
{
x+=bi;y-=ai;
}
for(i=x;i<=r1;i+=bi,y-=ai)
if(y>=l2&&y<=r2)
ans++;
}
else if(x>r1)
{
while(x>r1)
{
x-=bi;y+=ai;
}
for(i=x;i>=l1;i-=bi,y+=ai)
if(y>=l2&&y<=r2)
ans++;
}
else
{
int yi=y;
for(i=x;i<=r1;i+=bi,yi-=ai)
if(yi>=l2&&yi<=r2)
ans++;
yi=y+ai;
for(i=x-bi;i>=l1;i-=bi,yi+=ai)
if(yi>=l2&&yi<=r2)
ans++;
}
printf("%d\n",ans);
}
return ;
}