uva10892——LCM Cardinality(最小公倍数质因子分解)
来源:互联网 发布:数据光盘 编辑:程序博客网 时间:2024/05/25 18:11
10892 - LCM Cardinality
Time limit: 3.000 seconds
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=467&page=show_problem&problem=1833
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 LCMcardinality 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 Cis its cardinality. These two numbers are separated by a single space.
Sample Input Output for Sample Input
2
12
24
101101291
0
2 2
12 8
24 11
101101291 5
題目:給你一個數字N,問有多少對不同的a,b的最小公倍數數N。思路:
1. 设n=lcm(a,b)=(p1^r1)*(p2^r2)*(p3^r3)…(pm^rm)
又设a=(p1^a1)*(p2^a2)*(p3^a3)…(pm^am),b=(p1^b1)*(p2^b2)*(p3^b3)…(pm^bm)
则由lcm的定义有ri=max{ai,bi}
所以对于每个ri,ai和bi中至少有一个要取ri
2. 对于ai取ri的情况,bi可以取[0,ri-1]的任意整数,这有ri种情况;bi取ri的情况同样是ri种。最后加上ai和bi都取ri的情况,共有(2*ri+1)种情况
3. 最后,由于这么考虑把(a,b)和(b,a)算重复了,但(n,n)的情况只算了一遍,所以最后要ans=(ans+1)/2=ans/2+1(因为ans是奇数)
4. 优化:只考虑√n范围内的质数,但这样会存在漏掉一个大质数的情况(比如n=2*101等),这个大质数的幂次只能为1(即少算了一个*(2*1+1)),所以在这种情况发生时要补上ans*=3,写成位运算就是ans+=ans<<1了。
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
long long n, nn, ans, i, count;
while (scanf("%lld", &n), n)
{
nn = n;
ans = 1;
for (i = 2; i * i <= n; i += 2)///不用求素数,因为范围很小(注意n在不断减小)
{
if (n % i == 0)
{
count = 0;
while (n % i == 0)
{
n /= i;
++count;
}
ans *= (count << 1) + 1;
}
if (i == 2)
--i;///小技巧
}
if (n > 1)
ans += ans << 1;
ans = (ans >> 1) + 1;
printf("%lld %lld\n", nn, ans);
}
return 0;
}
- uva10892——LCM Cardinality(最小公倍数质因子分解)
- UVA10892 - LCM Cardinality(分解质因子)
- UVA10892 最小公倍数素因子分解
- Uva10892 LCM Cardinality
- UVA10892 LCM Cardinality(gcd)
- UVA 10892 LCM Cardinality (因子分解 水题)
- UVA 10892 LCM Cardinality——质因分解
- UVa 10892 LCM Cardinality (数论&素因子分解)
- lightoj1236——Pairs Forming LCM(素因子分解)
- UVA - 10892 LCM Cardinality (枚举因子)
- 最大公约数和最小公倍数问题--分解质因子
- Pairs Forming LCM(素因子分解+lcm)
- UVA10791 - Minimum Sum LCM(分解质因子)
- UVA - 10791 Minimum Sum LCM 分解质因子
- LCM Cardinality
- LCM的个数 UVa10892
- UVA 10892 LCM Cardinality(数论 质因数分解)
- UVa 10892 LCM Cardinality (分解质因数+数学)
- win10+centos7双系统安装记录
- SAPUI5教程——SAP Fiori应用类型分析(Transactional, Fact Sheets, Analytical)
- How to list files in a dictionary modified after a specific date
- 关键词优化的成功案例
- 工具设置
- uva10892——LCM Cardinality(最小公倍数质因子分解)
- js 动态拼接已有的变量名并调用
- pyqt5 QMYSQL driver not loaded
- wget
- 有关Python初学者对于yield于生成器(generator)疑惑的终极解答
- Linux下防火墙设置
- html5 nodejs&websocket聊天demo
- 面向对象高级特性
- 浅谈前端集成解决方案