首页 > 代码库 > hdu 4497 GCD and LCM

hdu 4497 GCD and LCM

GCD and LCM

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


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吉林通化全国邀请赛——题目重现
 

题意:求满足 gcd(x,y,z) = g , lcm(x,y,z) = l  的 组数

首先 l%g != 0 肯定是不行的,

我们对 l 和 g 经行素数分解

g = p1^a1 * p2 ^ a2 * p3 ^ a3 ...

l = p1^b1 * p2 ^ b2 * p3 ^ b3 ...

因为 l %g == 0 ;所以 ai <= bi 
对于p1 素数,
对于 x,y,z ,需要有一个数的素数分解 中 p1的次数为 ai ;
一个数的素数分解 中 p1的次数为 bi 
如果 ai == bi ,那么 没有选择
ai < bi 的选择就是 C(1,3)*C(1,2)*(bi-a1+1) 公式就是 6*(bi-ai+1) 
意思就是,三个里面选一个取 ai,然后两个里面选一个选 bi ,第三个的任意选
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<vector>#include<set>#include<stack>#include<map>#include<ctime>#include<bitset>#define LL long long#define mod 1000000007#define maxn 110#define pi acos(-1.0)#define eps 1e-8#define INF 0x3f3f3f3fusing namespace std;bool check(LL i ){    for(int j = 2 ; j*j <= i ;j++)if(i%j == 0)        return false;    return true;}int main(){    int j,i,l,g;    int T,ans1,u,v;    LL ans;    cin >> T ;    while(T--)    {        scanf("%d%d",&g,&l ) ;        if(l%g !=0)puts("0") ;        else        {            l /= g ;            ans=1;            for( LL i = 2 ; i*i <= l ;i++)if(l%i==0&&check(i))            {                v = 0;                while(l%i==0)                {                    v++;                    l /= i ;                }                ans *= v*6 ;            }            if(l !=1) ans *= 6 ;            printf("%I64d\n",ans);        }    }    return 0 ;}
View Code

 

 
 

hdu 4497 GCD and LCM