LCM的个数


LCM的个数

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem 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

Hint

 

Source

fmh

代码:

import java.util.Scanner;

public class Main {
    static int gcd(int a,int b)
    {
        while(b>0)
        {
            int r = a % b;
            a = b;
            b = r;
        }
        return a;
    }
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        while(reader.hasNext())
        {
            int a[] = new int[10000];
            int n = reader.nextInt();
            int j = 0;
            for(int i=1;i*i<=n;i++)
            {
                if(n%i==0)
                {
                    a[j++] = i;
                    if(i*i!=n)
                       a[j++] = n/i;
                }
            }
            int ant = 0;
            for(int i=0;i<j;i++)
            {
                for(int k=i;k<j;k++)
                {
                    if(a[i]/gcd(a[i],a[k])*a[k]==n)
                        ant++;
                }
            }
            System.out.println(ant);
        }
        reader.close();
    }
}

猜你喜欢

转载自blog.csdn.net/ACMerdsb/article/details/82803077
lcm