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.

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

 很多题都是难在理解上,这一题明显不是,它难在思考上。题意很简单,就是(a,b,c)的最大公约数和最小公倍数都给出来,

求出有多少个(a,b,c)的组合。(1,1,2)和(1,2,1),不是同一个。也就是a,b,c的位置是固定的。

这一题真让人束手无策。

这就牵涉到另一个定理了。唯一定理;(查了好久,才发现它有另一个名字,算术基本定理)

算术基本定理可表述为:任何一个大于1的自然数 N,如果N不为质数,那么N可以唯一分解成有限个质数的乘积N=P1^a1*P2^a2*P3^a3*......*Pn^an,这里P1<P2<P3......<Pn均为质数,其中指数ai是正整数。

所以把它们3个数(a,b,c)分别用这个式子写出来。

a=p1^a1*p2^a2*p3^a3 ……pn^an;

b=p1^b1*p2^b2*p3^b3……pn^bn;

c=p1^c1*p2^c2*p3^c3……pn^an;

L也可以写成p1^L1*p2^L2*p3^L3……pn^Ln;

可以知道,要保证(a,b,c)的最大公约数=L,那么对于p1来说,a1,b1,c1必定有一个为 L1,其他的要小于等于L1(但不能全等于,因为这样最小公倍数就为1了。很有可能和题意给的不一样。).(其实我也一知半解)

同理p2 p3……pn的指数都一样。

为了更好求,将(a,b,c)的最小公约数变为1,即所有的数a,b,c,G,L,都要除以G,都这时可以列出它们的排列组合。

拿p1为例;(a,b,c)

(L1,0,0)(交换位置有3种情况)

(L1,L1,0)(交换位置有3种情况)

(L1,0,1~L1-1)(交换位置有(L1-1)*6种情况)

总共有6*L1种情况。故所有的情况为6*L1*6*L2……6*Ln;

代码如下:

#include<stdio.h>
#define LL long long
int a[550];
int k;
void Solve(LL n)
{
    int i,c;k=0;
    for(i=2;i*i<=n;i++)
    {
        if(n%i==0)
        {
            c=0;
            while(n%i==0)
            {
                c++;
                n/=i;
            }
            a[k++]=c;
        }
    }
    if(n>1)
        a[k++]=1;
       // for(int i=0;i<k;i++)
	//	printf("\t%d\n",a[i]); 
}
  
int main()  
{  
    int t;
    LL L,G,temp,ans;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld%lld",&G,&L);
        if(L%G)//最大公约数必须能被最小公倍数整除。
        {
            printf("0\n");
            continue;
        }
        temp=L/G;
        Solve(temp);
        ans=1;
        for(int i=0;i<k;i++)
            ans*=(6*a[i]);
        printf("%lld\n",ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值