UVA 10892 LCM Cardinality(数论 质因数分解)
LCM Cardinality
Input: Standard Input
Output: Standard Output
Time Limit: 2 Seconds
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 10110129 |
题意:给出a和b的最小公倍数N。找出符合条件的a、b有多少对。
分析: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 <cstdio>
#include <cmath> int n; void get_ans() {
int tmp = n;
int m = (int)sqrt(n + 0.5);
long long ans = 1;
for(int i = 2; i <= m; i += 2) {
if(n % i == 0) {
int cnt = 0;
while(n % i == 0) {
n /= i;
cnt++;
}
ans *= (cnt << 1) + 1;
}
if(i == 2) i--;
}
if(n > 1) ans += (ans<<1);
ans = (ans >> 1) + 1;
printf("%d %lld\n", tmp, ans);
} int main() {
while(~scanf("%d", &n) && n) {
get_ans();
}
return 0;
}
UVA 10892 LCM Cardinality(数论 质因数分解)的更多相关文章
- UVA 10892 - LCM Cardinality
Problem F LCM Cardinality Input: Standard Input Output: Standard Output Time Limit: 2 Seconds A pair ...
- 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 ...
- Uva 10892 LCM Cardinality (数论/暴力)
题意:给出数n,求有多少组A,B的最小公约数为n; 思路:3000ms,直接暴力寻找,找到所有能把n整除的数 pi, 枚举所有pi 代码: #include <iostream> #inc ...
- UVA 10892 - LCM Cardinality(数学题)
题目链接 写写,就ok了. #include <cstdio> #include <cstring> #include <string> #include < ...
- HDU3988-Harry Potter and the Hide Story(数论-质因数分解)
Harry Potter and the Hide Story Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 ...
- Pairs Forming LCM (LightOJ - 1236)【简单数论】【质因数分解】【算术基本定理】(未完成)
Pairs Forming LCM (LightOJ - 1236)[简单数论][质因数分解][算术基本定理](未完成) 标签: 入门讲座题解 数论 题目描述 Find the result of t ...
- 数学概念——J - 数论,质因数分解
J - 数论,质因数分解 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- 简单数论之整除&质因数分解&唯一分解定理
[整除] 若a被b整除,即a是b的倍数,那么记作b|a("|"是整除符号),读作"b整除a"或"a能被b整除".b叫做a的约数(或因数),a ...
- hdu1405 第六周J题(质因数分解)
J - 数论,质因数分解 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Desc ...
随机推荐
- Java--Eclipse关联Java源码
打开Eclipse,Window->Preferences->Java 点Edit按钮后弹出: 点Source Attachment后弹出: 选择Java安装路径下的src.zip文件即可 ...
- Eclipse用法和技巧六:自动生成get和set方法1
java的类中,除了常量声明为静态且公有的,一般的对象数据作用域,都是声明为私有的.这样做能保护对象的属性不会被随意改变,调试的时候也会方便很多:在类的公有方法中大一个调用栈就能看到哪里改了属性值.声 ...
- Silverlight技术调查(2)——跨域访问
原文 Silverlight技术调查(2)——跨域访问 此调查web容器采用的是Tomcat,若允许所有域访问,只需在webapps下的根应用ROOT中,加入配置文件:clientaccesspoli ...
- PHP环境部署问题集合
Forbidden You don't have permission to access /phpmyadmin/ on this server. 安装wamp 后访问 http://localho ...
- scrum经验
Scrum是基于过程控制理论的经验方法,倡导自组织团队:其运行框架核心是迭代增量型并行开发,也是“适应性”的软件开发方法.Scrum提供了高度可视化的用于管理软件开发复杂性管理的敏捷项目管理的实践框架 ...
- HDU 4350 Card
打表找规律,比赛应付了一下,其实还可以把内存再优化一半掉,下面的0都是手动填充的,可以优化掉 题意: T个测试数据 下面52个数字表示 从栈顶到栈底的52个数 n l r表示 从栈顶下数 [l,r] ...
- Swift - 使用下划线(_)来分隔数值中的数字
为了增强较大数值的可读性,Swift语言增加了下划线(_)来分隔数值中的数字. 不管是整数,还是浮点数,都可以使用下划线来分隔数字. 1 2 3 4 //数值可读性 let value1 = 10_0 ...
- android ListView和GridView拖拽移位实现代码
关于ListView拖拽移动位置,想必大家并不陌生,比较不错的软件都用到如此功能了.如:搜狐,网易,百度等,但是相比来说还是百度的用户体验较好,不偏心了,下面看几个示例: 首先 ...
- 十天学习PHP之第三天
1)按右边的结构:查看改动表结构 2)按右边的浏览:查看表中的数据 3)按右边的SQL:执行SQL语句 4)按右边的插入:插入一行记录 5)按右边的清空:删除表中全部记录 6)按右边的删除: ...
- 国际化之DateFormat、NumberFormat
之所以在国际化中介绍DateFormat和NumberFormat这两个类,一是因为本身这两个类是地区敏感类,即可用传入Locale对象:二是因为这两个类具有不同的输出模式,而这些模式能在国际化的动态 ...