您的位置:首页 > 其它

hdu4497 GCD and LCM

2015-07-10 17:30 387 查看

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



[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
For each test case, print one line with the number of solutions satisfying the conditions above.

[align=left]Sample Input[/align]

2
6 72
7 33


[align=left]Sample Output[/align]

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>
#pragma comment(linker, "/STACK:102400000,102400000")
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: