UVa10892

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. 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 109).
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

题意:

输入正整数n,统计有多少a<=b满足lcm(a,b)=n。输出n以及满足条件的整数对数。

分析:

只需要枚举并存储数n的所有因子于数组arr,然后两两组合arr中的数,如果两数的最小公倍数是n则满足条件的整数对加1。

 1 #include <cstdio>
 2 #include <cmath>
 3 #define ll long long
 4 const int MAX_N = 1000;
 5 ll arr[MAX_N + 1]; // 存储所有的因子,从小到大
 6 ll gcd(ll a,ll b){return b == 0 ? a : gcd(b,a % b);}
 7 ll lcm(ll a,ll b){return a * b / gcd(a,b);}
 8 // 找出所有的因子
 9 int find_all(ll n){
10     int m = sqrt(n + 0.5);
11     int cnt = 0;
12     for(int i = 1 ; i <= m ; i++)if(n % i == 0)
13         arr[cnt++] = i,arr[cnt++] = n / i;
14     if(cnt >= 2 && arr[cnt - 1] == arr[cnt - 2]) cnt--;
15     return cnt; // 不同因子的个数
16 }
17 ll n;
18 int main(){
19     while(scanf("%lld",&n) && n){
20         int cnt = find_all(n),ans = 0;
21         for(int i = 0 ; i < cnt ; i++)
22             for(int j = i ; j < cnt ; j++)
23             if(lcm(arr[i],arr[j]) == n) ans++;
24         printf("%lld %d\n",n,ans);
25     }
26     return 0;
27 }

时间: 2024-10-14 07:08:56

UVa10892的相关文章

uva10892(暴力枚举)

把n的所有因子求出来,总数不会太多,所以直接O(n2)的暴力枚举所有对行不行. 有几个细节要注意,详见代码. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<map> #include<set> #include<vector> #include<algorit

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的个数 UVa10892

1.题目描述:点击打开链接 2.解题思路:本题要求统计有多少个a,b,使得lcm(a,b)=n,其中n是给定的一个整数.不难发现,这里的a,b一定都是n的约数.根据题目给定的范围,不妨事先计算出所有的约数,.接下来利用二重循环枚举约数即可.注意:由于还要满足a≤b.那么只需要算出不超过sqrt(n)的所有约数即可,剩下的部分可以根据约数的对称性得到.这样的约数肯定不超过1000个,时间可以承受. 3.代码: #define _CRT_SECURE_NO_WARNINGS #include<ios

题解 UVa10892

题目大意 多组数据,每组数据给定一个整数 \(n\),求满足 \(LCM(x,y)=n\) 的不同无序整数对 \((x,y)\) 的数目. 分析 若有 \(LCM(x,y)=n\),则有 \(GCD(n/x,n/y)=1\),问题便转化为了求 \(n\) 的所有因数中互质的数量,枚举即可. #include<bits/stdc++.h> using namespace std; int n, ans; int tot, fac[40000]; int gcd(int x, int y) { i

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

《算法竞赛入门经典——训练指南》第二章题库

UVa特别题库 UVa网站专门为本书设立的分类题库配合,方便读者提交: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=442 注意,下面注有"extra"的习题并没有在书中出现,但在上面的特别题库中有,属于附加习题. 基础练习 (Basic Problems) UVa11388 GCD LCM UVa11889 Benefit UVa10943 How do y

[数]被数学淹没不知所措

啊假期结束了,可以休息一阵子了 UVA-10892 分解&选择 #include<iostream> #include<cstdio> #include<cstring> using namespace std; typedef long long ll; bool isp[1000005]; int p[80005]; int f[100005]; int m,co; void pri() { co = 0; for (int i = 2; i < 10