【题目分析】
唯一分解定理。
【代码】
#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
int tt;
long long ans;
int a0,a1,b0,b1;
int p[100010],a[100010],now[100010],top=0;
int aa[100010],anow[100010];
inline long long gcd(long long a,long long b)
{return (b==0)?a:gcd(b,a%b);}
inline void init()
{
ans=1;top=0;
}
inline long long solve()
{
scanf("%d%d%d%d",&a0,&a1,&b0,&b1);
if (b0>b1) return 0;
if (gcd(b1,b0)!=b0) return 0;
long long x=b1;
for (int i=2;(ll)i*i<x;++i)
{
if (b1%i==0)
{
p[++top]=i;
a[top]=0;
now[top]=0;
while (b1%i==0)
{
a[top]++;
b1/=i;
}
while (b0%i==0)
{
now[top]++;
b0/=i;
}
}
}
if(b1>1)
{
p[++top]=b1;
a[top]=1;
now[top]=0;
while (b0%b1==0)
now[top]++,b0/=b1;
}
for (int i=1;i<=top;++i)
{
aa[i]=0;
anow[i]=0;
while (a1%p[i]==0)
{
a1/=p[i];
aa[i]++;
}
while (a0%p[i]==0)
{
a0/=p[i];
anow[i]++;
}
}
for (int i=1;i<=top;++i)
{
int l1,r1;
if (now[i]>a[i]) l1=inf,r1=-inf;
else if (a[i]>now[i]) l1=a[i],r1=a[i];
else l1=0,r1=a[i];
int l2,r2;
if (aa[i]<anow[i]) l2=aa[i],r2=aa[i];
else if (aa[i]==anow[i]) l2=aa[i],r2=inf;
else r2=-inf,l2=inf;
int tmp=0; int l,r;
if (l2>l1) l=l2; else l=l1;
if (r2>r1) r=r1; else r=r2;
if (r-l+1<=0) return 0;
else ans*=(r-l+1);
}
return ans;
}
int main()
{
scanf("%d",&tt);
while (tt--)
{
init();
printf("%lld\n",solve());
}
}