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

UVa 10892 - LCM Cardinality

时间:2019-02-23 01:11:16      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:oss   最小   最小公倍数   return   nat   ret   inline   inpu   tput   

Description

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 di?erent 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 ?nd out the LCM cardinality of a number.

Input

The input ?le contains at most 101 lines of inputs. Each line contains an integer N \((0 \lt N \le 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

Resume

对于给定的数N,求出以N为最小公倍数的有序数对个数。

Analysis

刚看到题目,苦思冥想,以为要什么神奇的数论方法来解决,甚至想到了标准分解,无奈要求的质数太大。
后来在同学的启迪下,发现一个数的因数并不多,大概在 \(\ln N\) 级别上(没找到准确的理论),那就可以直接暴力枚举解决喽。
时间复杂度 \(O(N) = \sqrt {N} {(\ln N)}^2\) 左右(迷。

Code

//////////////////////////////////////////////////////////////////////
//Target: UVa 10892 - LCM Cardinality
//@Author: Pisceskkk
//Date: 2019-2-16
//////////////////////////////////////////////////////////////////////

#include<cstdio>
#include<algorithm>
using namespace std;
int n,cnt,f[10000],ans;
int gcd(int a,int b){return b==0?a:gcd(b,a%b);}
int lcm(int a,int b){return 1ll*a*b/gcd(a,b);}        //1LL 防止爆int
int main(){
    while(~scanf("%d",&n) && n){
        ans = cnt = 0;
        for(int i=1;i*i<=n;i++){
            if(n%i == 0){
                f[++cnt]=i;
                if(i*i != n)f[++cnt]=n/i;
            }
        }
        sort(f+1,f+1+cnt);
        for(int i=1;i<=cnt;i++){
            for(int j=1;j<=i;j++){
                if(lcm(f[j],f[i]) == n)ans++;
            }
        }
        printf("%d %d\n",n,ans);
    }
    return 0;
}

UVa 10892 - LCM Cardinality

标签:oss   最小   最小公倍数   return   nat   ret   inline   inpu   tput   

原文地址:https://www.cnblogs.com/pisceskkk/p/10421323.html

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