hdu4497 GCD and LCM

來源:互聯網
上載者:User
GCD and LCM

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 78 Accepted Submission(s): 43

Problem DescriptionGiven 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.

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

OutputFor 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

Source2013 ACM-ICPC吉林通化全國邀請賽——題目重現

Recommendliuyiding很明顯,m/n!=0的話,就直接輸出0就可以了!否剛,直接分解質因數m/n,找到,每個質因子的個數,這樣,我們,就可以得出每個質因數為a1^k1,那是題目就是要把這k1個a1分到三個數中,那麼排列組合就是k1*A(3,2),也就是,6*k1,種,直接算出來就行了!

#include <iostream>#include <stdio.h>#include <algorithm>#include <string.h>#include <math.h>using namespace std;#define MAXN 100000int num[MAXN];int main(){    int tcase,n,m,i,ans,sum,tempm;    scanf("%d",&tcase);    while(tcase--)    {        scanf("%d%d",&n,&m);        memset(num,0,sizeof(num));        if(m%n!=0)        {            printf("0\n");            continue;        }        m=m/n;tempm=sqrt(m)+1;        for(i=2,ans=0;i<=tempm;i++)        {            if(m%i==0)            {                while(m%i==0)                {                    num[ans]++;m/=i;                }                ans++;            }        }        if(m!=1)            num[ans++]=1;        for(sum=1,i=0;i<ans;i++)            sum*=6*num[i];        printf("%d\n",sum);    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.