HDU4497:GCD and LCM(数论)

来源:互联网 发布:淘宝拍卖商业房产税费 编辑:程序博客网 时间:2024/05/16 14:21

GCD and LCM

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2648    Accepted Submission(s): 1162


Problem Description
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
 

Source

2013 ACM-ICPC吉林通化全国邀请赛——题目重现


思路:若3个数的lcm为L,gcd为G,一定有G%L==0,否则直接输出结果0;根据lcm和gcd的性质,假设3个数为a,b,c,则他们一定可以整除G,为了简化问题,将3个数和L都除以G,原题变成求gcd为1,lcm为L/G的3个数的组合数,将k=L/G分解质因数,假设分解结果为p1^x1 * p2^x2 * p3^x3......* pn^xn。分析其中一个质因子p1,其数目为x1个,即这三个数各自分解质因数后必定有一个数的p1因子为0个,必定有一个数的p1因子有x1个,另外一个数的p1因子可以是[0, x1]个,根据排列组合,当另外一个数p1因子数为0时,有3种组合,当为x1时也有3种组合,为(0,x1)时,有(x1-1)*6种组合,3+3+(x1-1)*6 = 6*x1种,对于其他质因子依此类推采用乘法原理算出结果。

# include <stdio.h># define MAXN 500000int a[MAXN+1]={0}, b[MAXN+1];int main(){    int t, g, l, num = 0;    for(int i=2; i<MAXN; ++i)        if(!a[i])        {            b[num++] = i;            for(int j=i; j<MAXN; j+=i)                a[j] = 1;        }    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&g,&l);        int icount, ans = 1;        if(l%g)            puts("0");        else        {            int k = l/g;            for(int i=0; i<num&&(b[i]*b[i]<=k); ++i)            {                if(k%b[i]==0)                {                    icount = 0;                    for(; k%b[i]==0; k/=b[i],++icount);                    ans *= icount*6;                }            }            if(k != 1)                ans *= 6;            printf("%d\n",ans);        }    }    return 0;}


0 0
原创粉丝点击