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.
Input
First line comes an integer T (T <= 12), telling the number of test cases.
The next T lines, each contains two positive 32-bit signed integers, G and L.
It’s guaranteed that each answer will fit in a 32-bit signed integer.
Output
For each test case, print one line with the number of solutions satisfying the conditions above.
Sample Input
2 6 72 7 33
Sample Output
72 0
题目大意: 已知gcd(x,y,z)=g;lcm(x,y,z)=L;问这样的组合(x,y,z)有多少种情况。
思路:
GCD = p1 ^ min(a1,b1,c1) * p2 ^min(a2,b2,c2) * ... * pn ^min(an,bn,cn)
LCM = p1 ^ max(a1,b1,c1) * p2 ^max(a2,b2,c2) * ... * pn ^max(an,bn,cn)
gcd(x,y,z) == G, lcm(x,y,z) == L,则gcd( x', y',z') == 1,lcm(x',y',z') == L/G ,其中x' = x /G,y' = y /G ,z' = z / G;
这样的话对t = L/G 这个数进行素因子分解,
t = p1^t1 * p2^t2 * p3^t3 ..... * pn ^tn;
x,y,z如下
x' = p1^i1 * p2^i2 *```* pn^in.
y' = p1^j1 * p2^j2 * ```*pn^jn.
z' = p1^k1 * p2^k2 * ```*pn^kn.
为了满足上面的条件,对于p1,一定有max(i1,j1,k1) = t1.min(i1,j1,k1) =0;因为gcd(p1^i1,p1^j1,p1^k1)== 1 == p^0, => min(i1,j1,k1) == 0;
同理:lcm(p1^i1,p1^j1,p1^k1) == p1^t1 => max(i1,j1,k1) == t1;那么的话三个数中至少有一个0,和一个t1,第三个数则是在[0, t1]这个区间里的一个数;
0 0 t1 有3种
t1 t1 0 有3种
t1 0 1~t1-1 有(t1-1)*6种
一共是6*t1种。
根据乘法原理,总的种数是:6*t1 * 6*t2 * ……
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
typedef long long ll;
bool book[10000110];
ll su[1000010];
int r;
void prime()
{
r=0;
for(ll i=2; i<10000100; i++)
{
if(!book[i])
{
su[r++]=i;
for(ll j=i*2; j<10000100; j+=i)
book[j]=1;
}
}
}
int ans;
void solve(int n)
{
for(int i=0; i<r; i++)
{
int num=0;
int x=su[i];
if(x*x>n)
break;
if(n%x==0)
{
while(n%x==0)
{
num++;
n/=x;
}
ans*=(6*num);
}
}
if(n>1)
ans*=6;
}
int main()
{
int t;
prime();
scanf("%d",&t);
while(t--)
{
int g,l;
scanf("%d%d",&l,&g);
if(g%l!=0)
{
printf("0\n");
continue;
}
ans=1;
solve(g/l);
printf("%d\n",ans);
}
return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int g,l;
scanf("%d%d",&g,&l);
if(l%g)
{
printf("0\n");
continue;
}
int k=l/g;
int ans=1;
for(int i=2;; i++)
{
int num=0;
if(k==1)
break;
if(k%i==0)
{
while(k%i==0)
{
num++;
k/=i;
}
ans*=(num*6);
}
}
printf("%d\n",ans);
}
return 0;
}