习题:hankson的趣味题(数论)
题目
思路
很简单的一个推导
设r=gcd(b0,x)
则b1=b0∗x/r
b1/b0=x/r
b1/x=b0/rb
最终gcd(b1/b0,b1/x)==1
有了这个等式之后,枚举b1/x就行了,时间复杂度$\sqrt{b1}$
代码
#include<iostream>
#include<algorithm>
using namespace std;
int n;
int tot=0;
int a0,a1;
int b0,b1;
int main()
{
ios::sync_with_stdio(false);
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a0>>a1>>b0>>b1;
int r=b1/b0;
tot=0;
for(int i=1;i*i<=b1;i++)
{
if(b1%i==0)
{
int t1=i;
int t2=b1/i;
if(__gcd(r,t1)==1&&__gcd(b1/t1,a0)==a1)
tot++;
if(__gcd(r,t2)==1&&__gcd(b1/t2,a0)==a1&&t1!=t2)
tot++;
}
}
cout<<tot<<'\n';
}
return 0;
}