hdu 4497 GCD and LCM(2013 ACM-ICPC吉林通化全国邀请赛——题目重现)

时间:2023-03-08 20:04:32

质分解 + 简单计数。当时去比赛的时候太年轻了。。。这道题都没敢想。现在回过头来做了一下,发现挺简单的,当时没做这道题真是挺遗憾的。这道题就是把lcm  / gcd 质分解,统计每个质因子的个数,然后就可以统计出总数了。

统计的时候假如有2个3,这样的话肯定是有一个元素是含有全部的2个3的,也肯定有一个元素没有3,于是我们就可以直接得出,统计个数为元素个数x6, 然后每个质因子分配情况互不影响,于是可以用乘法原理。就可以得出最终答案了。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath> #define LL long long
#define CLR(a, b) memset(a, b, sizeof(a))
#define SL(a) strlen(a) using namespace std; const int N = 111111; vector<int> hav;
bool isp[N];
int p[N], cnt; void getp()
{
int i, j;cnt = 0;
isp[0] = isp[1] = 1;
for(i = 2; i < N; i ++)
{
if(!isp[i])
{
p[cnt ++] = i;
if(i <= 1111)for(j = i * i; j < N; j += i) isp[j] = 1;
}
}
} void get_hav(int h)
{
int i;
for(i = 0; i < cnt && h > 1; i ++)
{
if(h % p[i] == 0)
{
h /= p[i];
hav.push_back(p[i]);
i --;
}
}
if(!hav.size() && h != 1) hav.push_back(h);
} int main()
{
int l, g, i, num, t;
LL ans;
getp();
cin >> t;
while(t --)
{
cin >> g >> l;
if(l % g != 0) puts("0");
else
{
l /= g;
ans = 1;
hav.clear();
get_hav(l);
hav.push_back(-1);
num = 0;
for(i = 0; i < hav.size() - 1; i ++)
{
if(hav[i] == hav[i + 1])
{
num ++;
}
else
{
num ++;
ans *= (6 * num);
num = 0;
}
}
cout << ans << endl;
}
}
}