zoukankan      html  css  js  c++  java
  • HDU 4497 GCD and LCM(分解质因子+排列组合)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4497

    题意:已知GCD(x, y, z) = G,LCM(x, y, z) = L。告诉你G、L,求满足要求的(x, y, z)有多少组,并且要考虑顺序。

    思路:如果L%G != 0显然不存在这样的(x, y, z),相反肯定存在。具体做法就是将L/G分解质因子,得到:L/G = P1^t1 * P2^t2 * ... * Pk^tk,我们来考虑任意一个因子Pi^ti,此时(x/G, y/G, z/G) = (x0, y0,z0)将x0,y0,z0分别分解质因子,得到对应位为Pi^t1, Pi^t2,Pi^t3,为了满足要求,(t1, t2, t3)中一定有一个为0(三个数互质),(t1, t2, t3)中一定有一个为ti且剩下的一个小于等于ti(三个数的最小公倍数为L/G)。min(t1, t2, t3) = 0, max(t1, t2, t3) = ti,所有情况就为6*ti(排列组合或者容斥原理),最后的答案就是(6*t1) * (6*t2) * ... * (6*tk)。

    code:

     1 #include <cstdio>
     2 
     3 int main()
     4 {
     5     int nCase;
     6     scanf("%d", &nCase);
     7     while (nCase--) {
     8         int L, G;
     9         scanf("%d %d", &G, &L);
    10         if (L % G) {
    11             printf("0
    ");
    12             continue;
    13         }
    14         L /= G;
    15         int ans = 1;
    16         for (int i = 2; i * i <= L; ++i) {
    17             if (L % i == 0) {
    18                 int t = 0;
    19                 while (L % i == 0) {
    20                     L /= i;
    21                     ++t;
    22                 }
    23                 ans *= (6 * t);
    24             }
    25         }
    26         if (L != 1) ans *= 6;
    27         printf("%d
    ", ans);
    28     }
    29     return 0;
    30 }
  • 相关阅读:
    xgboost
    GBDT 梯度提升决策树简述
    minimal pairs
    Describe your hometown
    英语短句
    英汉翻译
    英语音译词
    power的读音
    英语口语(英语词根与单词的说文解字(李平武 2008版)读书笔记)
    Jar包转成Dll的方式(带嵌套的jar也能做)
  • 原文地址:https://www.cnblogs.com/ykzou/p/4781326.html
Copyright © 2011-2022 走看看