码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 10892 LCM Cardinality (分解因数+暴力)

时间:2015-08-09 15:39:04      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:uva   分解因数   

LCM Cardinality


A pair of numbers has a unique LCM but a single number can be the LCM of more than one possible pairs. For example 12 is the LCM of (1, 12), (2, 12), (3,4) etc. For a given positive integer N, the number of different integer pairs with LCM is equal to N can be called the LCM cardinality of that number N. In this problem your job is to find out the LCM cardinality of a number.

Input
The input file contains at most 101 lines of inputs. Each line contains an integer N (0 < N < 2*10^9).
Input is terminated by a line containing a single zero. This line should not be processed.

Output
For each line of input except the last one produce one line of output. This line contains two integers N
and C. Here N is the input number and C is its cardinality. These two numbers are separated by a single space.

Sample Input
2
12
24
101101291
0

Sample Output
2 2
12 8
24 11
101101291 5


题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1833

题目大意:问n是多少个数对的最小公倍数

题目分析:考虑到n只有2e9,直接分解因数爆搞


#include <cstdio>
#include <cstring>
#define ll long long
int const MAX = 1e5;
int const LIM = 2e9;
int fac[MAX], cnt, n;

int gcd(int a, int b)
{
    while(b)
    {
        int tmp = a;
        a = b;
        b = tmp % b;
    }
    return a;
}

ll lcm(int a, int b)
{
    return (ll) a * b / gcd(a, b);
}

void cal()
{
    cnt = 0;
    for(int i = 1; i * i <= n; i++)
    {
        if(n % i == 0)
        {
            fac[cnt++] = i;
            fac[cnt++] = n / i;
        }
        if(fac[cnt - 1] == fac[cnt - 2])
            cnt --;
    }
}

int main()
{
    while(scanf("%d", &n) != EOF && n)
    {
        cal();
        int ans = 0;
        for(int i = 0; i < cnt; i++)
            for(int j = i; j < cnt; j++)
                if(lcm(fac[i], fac[j]) == n)
                    ans ++;
        printf("%d %d\n", n, ans);
    }
}









版权声明:本文为博主原创文章,未经博主允许不得转载。

UVA 10892 LCM Cardinality (分解因数+暴力)

标签:uva   分解因数   

原文地址:http://blog.csdn.net/tc_to_top/article/details/47376285

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!