https://www.luogu.com.cn/problem/P1029
暴力枚举
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
int x,y,ans=0;
cin>>x>>y;
int k=sqrt(x*y);
for(int i=x;i<=k;i+=x)
{
//如果两数之积除以i能整除,且i和另一个数的最小公约数是n,则符合条件
if( (x*y)%i==0 && __gcd(i,x*y/i)==x)
ans++;
}
cout<<ans*2<<"\n";
return 0;
}
数学
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int x,y,ans=0;
//最大公约数与最小公倍数的乘积等于原来两个数的乘积 所以
//x*y=p*q
//gcd(p,q)=x lcm(p,q)=y
//设p=x*t1 , q=x*t2
//因为 gcd(p,q)*lcm(p,q)=p*q
//所以 x*lcm(p,q)=x*t1 * x*t2
// lcm(p,q)=x*t1*t2
// y=x*t1*t2
// y/x=t1*t2
//
//先算出y/x 再循环求出t1,t2 ,则可得 设p=x*t1 , q=x*t2
//最后验证是否满足 gcd(p,q)=x lcm(p,q)=y
int main()
{
cin>>x>>y;
int k=sqrt(y/x);
for(int i=1; i<=k; i++)
{
int j=y/x/i; //i=t1 j=t2
int p=i*x,q=x*j;
if(__gcd(p,q)==x && (p*q==x*y))
ans++;
}
cout<<ans*2<<"\n";
return 0;
}