HDU4497 GCD and LCM
题目描述
Given two positive integers G and L, could you tell me how many solutions of (x, y, z) there are, satisfying that gcd(x, y, z) = G and lcm(x, y, z) = L?
Note, gcd(x, y, z) means the greatest common divisor of x, y and z, while lcm(x, y, z) means the least common multiple of x, y and z.
Note 2, (1, 2, 3) and (1, 3, 2) are two different solutions.
题意
给两个整数G,L,问有多少个有序的三元组(x,y,z),使其的gcd为Glcm为M。
解法
L一定是G的倍数
很明显,x,y,z的gcd一定是lcm的因子,令K = L / G, xx = x / G, yy = y / G, zz = z / G.那么xx,yy,zz的lcm为K,gcd为1.
K = p1^n1 * p2^n2 * …对于第i个质因子pi,它在三元组中的出现情况只能是
1.必须出现一次p1^n
2.必须出现一次p1^0
3.有一个的元素的因子为p1^m (0 <= m < n)对于素因子pi共有6*ni种情况
所以结果为p1*6*p2*6*p3*6…
#include<cstdio>
using namespace std;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int G,L;
scanf("%d%d",&G,&L);
if(L%G) printf("0\n");
else
{
int K = L / G;
int res = 1;
for(int i=2;K>1;i++)
{
int cnt = 0;
while(K%i == 0 && K > 1)
{
cnt++;
K /= i;
}
if(cnt) res *= 6 * cnt;
}
printf("%d\n",res);
}
}
return 0;
}