HDU 4497 GCD and LCM 素因子分解+ gcd 和 lcm

时间:2023-03-09 02:48:12
HDU 4497 GCD and LCM 素因子分解+ gcd 和 lcm

题意:

给两个数,lll 和 ggg,为x , y , z,的最小公倍数和最大公约数,求出x , y , z 的值有多少种可能性

思路:

将x , y , z进行素因子分解

素因子的幂次
x a1 a2 a3 a4
y b1 b2 b3 b4
z c1 c2 c3 c4
gcd min(a1,b1,c1) min(a2,b2,c3)…
lcm max(a1,b1,c1) max(a2,b2,c3)…

第一组样例:

6=21 * 31

72= 23 * 32

最大公约数和最小公倍数约分得 12=22 * 31(是否约分并不重要,幂次相减就可以了),设最大公约数为1,最小公倍数为12

设 x为1 ,y=12,也就是

素因子的幂次
x a1=0, a2 =0
y b1 = 2 , b2=1
z c1=0~2 , c2= 0~1

将素因子按列对齐,当出现两个幂次相等时,排列组合为3,当三个幂次都不相等时,排列组合为6,

上述例子c1的范围是0~2,当c1=0和c1=2时合并为1个6,所以是2 * 6,c2=1和c2=0合并为一个6,所以是1 * 6,根据两个素因子相乘,得出2* 6 * 1*6。

#include<algorithm>
#include<stdio.h>
#include<map>
#include<vector>
using namespace std;
int main()
{ int t,n,m,g,l,acc;
scanf("%d",&t);
while(t--)
{
struct
{
vector<int>e;
map<int,int>mp;
} gcd,lcm; scanf("%d%d",&g,&l);
if(l%g)//一组数的最大公约数和最小公倍数肯定能整除
{
printf("0\n");
continue;
}
acc=l/g;
int ans=1;
for(int i=2;; i++)
{
if(acc%i==0)
{
int t=0;
while(acc%i==0)
{
acc=acc/i;
t++;
}
ans=ans*6*t;
}
if(acc==1)
break;
}
printf("%d\n",ans);
}
return 0;
}