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

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);
    }
}

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

时间: 2024-12-10 03:05:47

UVA 10892 LCM Cardinality (分解因数+暴力)的相关文章

Uva 10892 LCM Cardinality (数论/暴力)

题意:给出数n,求有多少组A,B的最小公约数为n; 思路:3000ms,直接暴力寻找,找到所有能把n整除的数 pi, 枚举所有pi 代码: #include <iostream> #include <cstdio> #include <vector> #define ll long long using namespace std; ll gcd(ll a,ll b) { if(b==0) return a; else return gcd(b,a%b); } int

UVA - 10892 LCM Cardinality (枚举因子)

A pair of numbers has a unique LCM but a single number can be the LCM of more than one possible pairs. Forexample 12 is the LCM of (1, 12), (2, 12), (3,4) etc. For a given positive integer N, the number of different integer pairs withLCM is equal to

UVA 10892 LCM Cardinality 数学

A pair of numbers has a unique LCM but a single number can be the LCM of more than one possiblepairs. For example 12 is the LCM of (1, 12), (2, 12), (3,4) etc. For a given positive integer N, thenumber of di?erent integer pairs with LCM is equal to N

UVA 10892 LCM Cardinality

题目链接:UVA-10892 题意为给定一个数n,问有多少组不同的数对<a,b>的lcm等于n.看了AC,∑ndless的题解,直接摘抄了. 代码: 1 #include"cstdio" 2 #include"iostream" 3 #include"cstring" 4 #include"algorithm" 5 #include"cstdlib" 6 #include"vector

UVA10892 - LCM Cardinality(分解质因子)

题目链接 题意:输入正整数n,统计有多少对正整数a <= b,满足lcm(a, b) = n. 思路:分解质因子,然后直接暴力求出对数 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; typedef long long ll; const int MA

LCM Cardinality 暴力

LCM CardinalityTime Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description Problem FLCM CardinalityInput: Standard Input Output: Standard Output Time Limit: 2 Seconds A pair of numbers has a unique LCM but a singl

把一个100以内的自然数分解因数。大小端的判断。

写一个程序,把一个100以内的自然数分解因数.(自然数分解因数就是将一个自然数分解为几个素数的乘积,提示,由于该数不是很大,所以可以将质数保存在数组中,以加快计算速度) 1 #include<stdio.h> 2 #include<math.h> 3 int Count(int n) 4 { 5 int i = 2; 6 for(i = 2;i<=sqrt(n);i++) 7 { 8 if(n%i==0) 9 { 10 printf("%d*",i); 1

[算法]分解因数

将101到200间的数分解因数 log = {} def prime_log(num): if num < 3: return 0 flags = [True] * num flags[0], flags[1] = False, False for i in range(2, int(num**0.5+1)): if flags[i]: flags[i*i:num:i] = [False]*len(flags[i*i:num:i]) return flags primes = prime_log

递归--练习5--noi1751分解因数

递归--练习5--noi1751分解因数 一.心得 想清楚子问题 想清楚递推表达式 没有全部AC说明还有自己没有想到的位置,试边界情况和查看题目要求 二.题目 1751:分解因数 总时间限制:  1000ms 内存限制:  65536kB 描述 给出一个正整数a,要求分解成若干个正整数的乘积,即a = a1 * a2 * a3 * ... * an,并且1 < a1 <= a2 <= a3 <= ... <= an,问这样的分解的种数有多少.注意到a = a也是一种分解. 输