HDU 4497 GCD and LCM

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
题意:给你两个整数L和G,L为(x,y,z)的最小公倍数,G为(x,y,z)的最大公因数,求有多少种组合的(x,y,z)满足这个条件。
#include<iostream>
using namespace std;
typedef long long ll;
int main()
{
    int T;
    ll G,L,sum,cou;
    cin>>T;
    while(T--)
    {
        cin>>G>>L;
        if(L%G)//如果L不能整除G,不存在这样的组合
        {
            cout<<0<<endl;
            continue;
        }
        L/=G;
        sum=1;
        for(ll i=2;i*i<=L;i+=2)
        {
            if(L%i==0)
            {
                cou=0;
                while(L%i==0)
                {
                    L/=i;
                    cou+=1;//i的指数加1
                }
                sum*=6*cou;//
            }
            if(i==2)i-=1;//先从2开始,后面的从3开始依次加2
        }
        if(L>1)sum=6*sum;
        cout<<sum<<endl;
    }
    return 0;
}

设L/G=(p1^r1)*(p2^r2)*(p3^r3)…(pm^rm)
又设
x=(p1^a1)*(p2^a2)*(p3^a3)…(pm^am)
y=(p1^b1)*(p2^b2)*(p3^b3)…(pm^bm)
z=(p1^c1)*(p2^c2)*(p3^c3)…(pm^cm)

对于r1~rm中的任一个ri,它所对应的ai,bi,ci中一定有一个是ri,并且有一个是0;

ai,bi,ci有以下三种情况
ri 0 0 ,有C(3,1)种
ri 0 ri ,有C(3,1)种
ri 0 1~ri-1 ,有(ri-1)*A(3,3)种

C(3,1)+C(3,1)+(ri-1)*A(3,3)=6*ri;

posted @ 2018-11-02 17:12  蓝猫爱宇宙  阅读(93)  评论(0编辑  收藏  举报