码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4497 GCD and LCM

时间:2018-06-30 12:49:14      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:分解   oid   scanf   esc   you   limit   str   nbsp   style   

GCD and LCM

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


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
 
 
//Pro: hdu 4497 GCD and LCM

//先写点东西: 
//如果有非负整数a,b, 将他们分解质因数,得到:
//a=p1^k1 * p2^k2 * p3^k3 *...* pn^kn
//b=p1^q1 * p2^q2 * p3^q3 *...* pn^qn (p>0, q>=0, k>=0, k,q不同时为0)
//显然地, 
//gcd(a,b)=p1^(min(k1,p1)) * p2(min(k2,p2)) *...* pn^(min(kn,qn))
//lcm(a,b)=p1^(max(k1,p1)) * p2(max(k2,p2)) *...* pn^(max(kn,qn))
//=> a*b=p1^(k1+q1) * p2^(k2+q2) * p3^(k3+q3) *...* pn^(kn+qn)=gcd(a,b) * lcm(a,b)

//题意:
//三个未知数x,y,z,它们的gcd为G,lcm为L,G和L已知,求(x,y,z)三元组的个数 
//由上边那个东东可以知道,将x,y,z,G,L质因数分解之后
//他们的相同的因子Pi的次数 ki_G<=ki_x,ki_y,ki_z<=ki_L,
//且min(ki_x,ki_y,ki_z)==ki_G && max(x,y,z)==ki_L
//所以x,y,z里有两个数的某一质因子的个数是固定的,分别等于ki_L或ki_G,
//剩下的第三个数的该质因子的个数可以在[ki_G,ki_L]中任选 
//(ki_g<ki_a<ki_l)的排列一共有6种,如果ki_a==ki_g或者ki_a==ki_l,各有三种排列,共2*3=6种
//那么(x,y,z)的个数一共有6*(l-g-1)+2*3=6*(l-g)种 


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std; 

const int N=1e7+5;

int T;
int l,g,a;

bool nprime[N];
int prime[N],cnt;
void init()
{
    nprime[1]=1;
    for(int i=2;i<N;++i)
    {
        if(!nprime[i])
            prime[++cnt]=i;
        for(int j=1,d;j<=cnt&&(d=i*prime[j])<N;++j)
        {
            nprime[d]=1;
            if(i%prime[j]==0)
                break;
        }
    }
}

int ans;
int main()
{
    init();
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&g,&l);
        if(l%g)
        {
            puts("0");
            continue;
        }
        l/=g;    //把相同的因子约一下
        ans=1;
        for(int i=1,k;i<=cnt;++i)
        {
            if(l%prime[i])
                continue;
            k=0;
            while(l%prime[i]==0)
            {
                ++k;
                l/=prime[i];
            }
            ans*=6*k;
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

HDU 4497 GCD and LCM

标签:分解   oid   scanf   esc   you   limit   str   nbsp   style   

原文地址:https://www.cnblogs.com/lovewhy/p/9246244.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!