標籤:
題目連結: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\n");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\n", ans);28 }29 return 0;30 }
HDU 4497 GCD and LCM(分解質因子+排列組合)