HDU 4497 GCD and LCM(分解質因子+排列組合)

來源:互聯網
上載者:User

標籤:

題目連結: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(分解質因子+排列組合)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.