LCM的个数
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
Description
对于我们来说求两个数的LCM(最小公倍数)是很容易的事,现在我遇到了一个问题需要大家帮助我来解决这问题,问题是:给你一个数n,然后统计有多少对(a<=b) LCM(a,b)=n;例如LCM(a,b)=12; 即(1,12),(2,12),(3,12),(4,12),(6,12),(12,12),(3,4),(4,6);
Input
输入数组有多组,每组数据包含一个整数n(n<=10^9);
Output
输出每组数据的对数。
Sample Input
2 3 4 6
Sample Output
2 2 3 5
解决该题需知道两点。
1、如果两个数的最小公倍数是x,那么这两个数都是x的因子。
2、一个数(以x为例)的因子必然分布在sqrt(x)的两侧且两侧个数相等。
然后暴力就可以了。
AC 代码:
#include<stdio.h>
#include<math.h>
#include<stdio.h>
#include<iostream>
using namespace std;
int con[10000009];
int gcd(int a,int b)
{
return b==0?a:gcd(b,a%b);
}
int main()
{
int n,i,p,j;
int ans;
while(scanf("%d",&n)!=EOF)
{
p=0;
ans=0;
for(i=1; i<=sqrt(n); i++)
{
if(n%i==0)
{
con[++p]=i;
if(i*i!=n)
con[++p]=n/i;
}
}
for(i=1; i<=p; i++)
for(j=1; j<=i; j++)
{
if(con[i]/gcd(con[i],con[j])*con[j]==n)
ans++;
}
printf("%d\n",ans);
}
return 0;
}