UVA 10791 - Minimum Sum LCM(数论)
2013-11-05 22:14
531 查看
Minimum Sum LCM |
![](http://uva.onlinejudge.org/external/107/p10791.jpg)
LCM (Least Common Multiple) of a set of integers is defined as the minimum number, which is a multiple of all integers of that set. It is interesting to note that any positive integer can be expressed
as the LCM of a set of positive integers. For example 12 can be expressed as the LCM of 1, 12 or12, 12 or 3, 4 or 4, 6 or 1, 2, 3, 4 etc.
In this problem, you will be given a positive integer N. You have to find out a set of at least two positive integers whose LCM is N.
As infinite such sequences are possible, you have to pick the sequence whose summation of elements is minimum. We will be quite happy if you just print the summation of the elements of this set. So, for N =
12, you should print4+3 = 7 as LCM of 4 and 3 is 12 and 7 is the minimum possible summation.
Input
The input file contains at most 100 test cases. Each test case consists of a positive integer N ( 1![](http://uva.onlinejudge.org/external/107/10791img2.png)
N
![](http://uva.onlinejudge.org/external/107/10791img2.png)
231 -
1).
Input is terminated by a case where N = 0. This case should not be processed. There can be at most 100 test
cases.
Output
Output of each test case should consist of a line starting with `Case #: ' where # isthe test case number. It should be followed by the summation as specified in the problem statement. Look at the output for sample input for details.
Sample Input
12 10 5 0
Sample Output
Case 1: 7 Case 2: 7 Case 3: 6
题意:给定一个n,求最小LCM。。LCM求法为,把n分解成几个乘数相加,要求这些乘数互质。
思路:先分解成全是质数p1^n1*p2^n2....pn^nn。最小和为p1^n1+p2^n2+...+pn^nn。
注意几个WA点。要用longlong。不然当输入为2147483647会输出2147483648.超范围。还有如果不能分解或者只能分解出一个因数答案应该为n+1.
代码:
#include <stdio.h> #include <string.h> #include <math.h> long long n, ans, i, j, bo; int main() { int t = 0; while (~scanf("%d", &n) && n) { long long save = n; ans = bo = 0; long long num = sqrt(n); for (i = 2; i <= num; i ++) { long long sum = 1; if (n % i == 0) { bo ++; while (n % i == 0) { n /= i; sum *= i; } ans += sum; } } if (n != 1) ans += n; if (bo < 2) { ans = save + 1; } printf("Case %d: %lld\n", ++ t, ans); } return 0; }
相关文章推荐
- leetcode Linked List Cycle II 继续写。
- Lucene (create Index) & search Index
- android 二维码识别技术分享
- Android学习篇章50-MediaStore-Intent启动照相
- uva-10106
- jquery发送ajax请求返回数据格式
- json---java对象转换
- ACM第一课---数论之欧拉函数
- 一些常用shell脚步
- Hadoop笔试/面试题
- phpcms v9 更改首页
- 动态js执行出错:obj.innerHTML="javascript:delete(param1+','+param2)"
- 文件数据的读/写
- 关于自信的定义
- andorid 反编译
- Android学习篇章49-SurfaceView-照相
- shel 编程拾锦
- java socket编程小例子
- [翻译Joel On Software]无痛功能规范-第一部分:为什么要?/Painless Functional Specifications -Part 1: Why Bother?
- IPC Binder