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 MAXN = 1000;

ll arr[MAXN];
ll n;

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

int main() {
    while (scanf("%lld", &n) && n) {
        memset(arr, 0, sizeof(arr));
        int cnt = 0;
        for (int i = 1; i <= sqrt(n); i++) {
            if (n % i == 0) {
                arr[cnt++] = i;
                arr[cnt++] = n / i;
            }
        }
        if (arr[cnt - 1] == arr[cnt - 2])
            cnt--;
        int ans = 0;
        for (int i = 0; i < cnt; i++) {
            for (int j = i; j < cnt; j++) {
                if (((arr[i] * arr[j]) / gcd(arr[i], arr[j])) == n)  {
                    ans++;
                }
            }
        }

        printf("%lld %d\n", n, ans);
    }
    return 0;
}

时间: 2024-10-06 00:45:01

UVA10892 - LCM Cardinality(分解质因子)的相关文章

HDU 4497 GCD and LCM(分解质因子+排列组合)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4497 题意:已知GCD(x, y, z) = G,LCM(x, y, z) = L.告诉你G.L,求满足要求的(x, y, z)有多少组,并且要考虑顺序. 思路:如果L%G != 0显然不存在这样的(x, y, z),相反肯定存在.具体做法就是将L/G分解质因子,得到:L/G = P1^t1 * P2^t2 * ... * Pk^tk,我们来考虑任意一个因子Pi^ti,此时(x/G, y/G, z/

分解质因子(个人模版)

分解质因子: 1 memset(prime,0,sizeof(prime)); 2 memset(num,0,sizeof(num)); 3 for(int i=2;i<=5000005;i++) 4 { 5 if(prime[i]==0) 6 { 7 for(int j=i;j<=5000005;j+=i) 8 { 9 int temp=j; 10 while(temp%i==0) 11 { 12 num[j]++; 13 temp/=i; 14 } 15 prime[i]=1; 16 }

POJ 1730 Perfect Pth Powers (枚举||分解质因子)

Perfect Pth Powers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16638   Accepted: 3771 Description We say that x is a perfect square if, for some integer b, x = b2. Similarly, x is a perfect cube if, for some integer b, x = b3. More g

poj3993Not So Flat After All(筛法素数+分解质因子)

题目链接: 啊哈哈,点我点我 题意: 题意是给出两个数字,然后有由一分解定理得,每个数可以分解成若干质因数的乘积,这样就可以在一个n维的坐标系下表示出这个点...比如给出50和24 因为24=2^3*3^1*5^0  而50=2^1*3^0*5^2那么这两个点就可以在一个3维德坐标系下表示出这两个点..24=(3,1,0)  50=(1,0,2)  那么共同拥有的维度就是3  而两个点在n维坐标系下的距离就是|3-1|+|1-0|+|0-2|=5,这样题意完全理解... 思路: 先筛出10000

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

HDU 3641 Treasure Hunting (二分+分解质因子)

题目链接:HDU 3641 Treasure Hunting 题意:求X!%M==0中最小的的X.其中M=a1^b1*a2^b2*a3^b3.... 思路:求余为0想到整除,即分母的因子构成的集合是分子的因子构成的集合的子集.因子又想到,任何一个整数都可以分解成若干个素数相乘. 注意:题目数据很大,查到答案时二分. AC代码: #include<stdio.h> #include<string.h> #define ll __int64 bool Prime[210]; ll nu

分解质因子问题

题目大意:有t(1<=t<=104)个数arr[1],arr[2]-.arr[t],设每个数是n(2<=n<=109),任务是将这个n的质因子分解出来,包括重复的质因子,时限是1000MS..比如n=18,而18=2*3*3,所以输出的结果就是2 3 3. n的范围是[2,109],很容易想到n的质因子的范围是[2,sqrt(n)],所以可以预处理出[2,sqrt(109)]的所有素数(注:sqrt()就是求解一个数的平方根),这个范围内的素数大概只有3千多个,拿这个n跟所有的素数

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 wit

BNU 13259.Story of Tomisu Ghost 分解质因子

Story of Tomisu Ghost It is now 2150 AD and problem-setters are having a horrified time as the ghost of a problem-setter from the past, Mr. Tomisu, is frequently disturbing them. As always is the case in most common ghost stories, Mr. Tomisu has an u