uva 10892 LCM Cardinality
来源:互联网 发布:java 读取磁盘 io 编辑:程序博客网 时间:2024/05/19 03:16
原题:
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
中文:
给你一个数n,让你找出能形成最小公倍数为n的两个数的个数。
#include <bits/stdc++.h>using namespace std;long p[150001];bool tag[150001];int get_prime(){ long cnt=0; for(long i=2;i<150001;i++) { if(!tag[i]) p[cnt++]=i; for(long j=0;j<cnt&&p[j]*i<150001;j++) { tag[i*p[j]]=1; if(i%p[j]==0) break; } } return cnt;}long decomposition(long x){ long k=0,cnt,res=1; while(x>1) { cnt=0; while(x%p[k]==0) { x/=p[k]; cnt++; } res*=(cnt*2)+1; k++; } return res;}int main(){ ios::sync_with_stdio(false); cout<<get_prime(); long n; while(cin>>n,n) { if(n==1) cout<<n<<" "<<1<<endl; else { long ans=decomposition(n); cout<<n<<" "<<ans/2+1<<endl; } } return 0;}
解答:
先在纸上写一写发现,如果两个数要是能形成最小公倍数为n,那么必须要满足如下的形式。
首先把n用唯一分解定理分解可以得到
如果要想有两个数a,b的公倍数的是n。那么a用唯一分解定理分解,b用唯一分解定理分解,a和b的相同素因子的系数取最大的那个必须要等于
也就是最小公倍数的那个公式
其中
那么如果
不过两个数(a,b)和(b,a)会重复(n,n)也会重复。所以要除以2加1。
先筛个素数表,然后用唯一分解定理找出所有质因子的个数。最后计算即可
- UVa:10892 LCM Cardinality
- UVA 10892-LCM Cardinality
- UVa 10892 - LCM Cardinality
- UVA 10892 - LCM Cardinality
- UVA 10892 LCM Cardinality
- uva 10892 LCM Cardinality
- UVa-10892 LCM Cardinality
- UVA-10892 LCM Cardinality
- LCM Cardinality(UVA 10892)
- uva 10892 - LCM Cardinality(数论)
- UVA 10892 - LCM Cardinality(数论)
- UVA - 10892 LCM Cardinality (枚举因子)
- UVA 10892 LCM Cardinality (因子分解 水题)
- UVa 10892 LCM Cardinality (数论+组合数学)
- UVa 10892 LCM Cardinality (数论&素因子分解)
- Uva 10892 - LCM Cardinality 解题报告(因式分解)
- UVa 10892 - LCM Cardinality (因式分解 组合数学)
- UVA 10892 LCM Cardinality(数论 质因数分解)
- setAction、setData通过按钮启动其他程序:
- 哈弗曼编码的实现
- Amaze UI React框架快速体验上手
- 如何让linux终端显示/不显示 路径
- iOS编程环境搭建
- uva 10892 LCM Cardinality
- 375. Guess Number Higher or Lower II
- 文章标题
- 网络流专题2
- Pro_1_UNIX下最简单的C_S程序_2016_8_5
- 文章标题
- 我的Unity 3D之旅——杂记之LightMapping和Light Probe
- java如何实现二叉树
- oracle安装配置