HDU4497 GCD and LCM

4 篇文章 0 订阅
4 篇文章 0 订阅

HDU4497 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.

题意

给两个整数G,L,问有多少个有序的三元组(x,y,z),使其的gcd为Glcm为M。

解法

  • L一定是G的倍数

    很明显,x,y,z的gcd一定是lcm的因子,令K = L / G, xx = x / G, yy = y / G, zz = z / G.那么xx,yy,zz的lcm为K,gcd为1.
    K = p1^n1 * p2^n2 * …

  • 对于第i个质因子pi,它在三元组中的出现情况只能是

    1.必须出现一次p1^n
    2.必须出现一次p1^0
    3.有一个的元素的因子为p1^m (0 <= m < n)

  • 对于素因子pi共有6*ni种情况

    所以结果为p1*6*p2*6*p3*6…

#include<cstdio>

using namespace std;

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int G,L;
        scanf("%d%d",&G,&L);
        if(L%G) printf("0\n");
        else
        {
            int K = L / G;
            int res = 1;
            for(int i=2;K>1;i++)
            {
                int cnt = 0;
                while(K%i == 0 && K > 1)
                {
                    cnt++;
                    K /= i;
                }
                if(cnt)  res *= 6 * cnt;
            }
            printf("%d\n",res);
        }
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值