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;}