234人阅读 评论(0)

# GCD and LCM

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

Problem Description
Given 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.

Input
First 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.

Output
For 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

给出gcd和lcm，求出满足条件的(x,y,z)这样的三元组有多少个。
把这些数分解因子，gcd因子个数是这些数里面拥有这个因子最少的数的因子个数，gcd因子个数是这些数里面拥有这个因子最多的数的因子个数。所以这里面先用lcm除以gcd，把除了之后得到的数分解因子，对于每个因子，假设个数是m，x,y,z里面一定有一个数的这个因子个数是m，一个数因子个数为m，剩下一个在0到n之间。因此对于这个因子，三个数的组合有(m-1)*6+6种。
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;

const int MAXN=300010;
const int SIZE=4096;
const int INF=0x3f3f3f3f;

int T;
LL G,L;

int main(){
freopen("in.txt","r",stdin);
scanf("%d",&T);
while(T--){
scanf("%lld%lld",&G,&L);
if(L%G!=0||L<G){
printf("0\n");
continue;
}
LL n=L/G;
LL sq=sqrt(n)+1;
int ans=1;
for(LL i=2;i<=n&&i<=sq;i++){
int m=0;
while(n%i==0){
n/=i;
m++;
}
if(m>0) ans*=(m-1)*6+6;
}
if(n>1) ans*=6;
printf("%d\n",ans);
}
return 0;
}


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：108355次
• 积分：3622
• 等级：
• 排名：第9324名
• 原创：267篇
• 转载：6篇
• 译文：0篇
• 评论：1条
文章分类
最新评论