UVA - 10791 - Minimum Sum LCM (数论相关!)
来源:互联网 发布:淘宝助理怎么批量上传 编辑:程序博客网 时间:2024/05/16 12:34
题目链接:Minimum Sum LCM
UVA - 10791
Description
Minimum Sum LCM
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 theLCM of a set of positive integers. For example 12 can be expressed as theLCM of 1, 12 or 12,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 whoseLCM 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 print 4+3 = 7 asLCM of 4 and 3 is 12 and7 is the minimum possible summation.
Input
The input file contains at most 100 test cases. Each test case consists of a positive integerN ( 1N231 - 1).
Input is terminated by a case where N = 0. This case should not be processed. There can be at most100 test cases.
Output
Output of each test case should consist of a line starting with `Case #: ' where # is the 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
121050
Sample Output
Case 1: 7Case 2: 7Case 3: 6
Problem setter: Md. Kamruzzaman
Special Thanks: Shahriar Manzoor
Miguel Revilla 2004-12-10
Source
Root :: AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 6. Mathematical Concepts and Methods
Root :: AOAPC I: Beginning Algorithm Contests -- Training Guide (Rujia Liu) :: Chapter 2. Mathematics ::Basic Problems
Root :: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim) :: Mathematics :: Number Theory ::Working with Prime Factors
Root :: Prominent Problemsetters :: Md. Kamruzzaman (KZaman)
Root :: Competitive Programming 2: This increases the lower bound of Programming Contests. Again (Steven & Felix Halim) :: Mathematics :: Number Theory ::Working with Prime Factors
思路:分解质因子,将最小公倍数分解质因子,最小的ans便为各个质因子的相应次方数之和。
此题难点较多:
1、当N = 1时,应输出2;
2、当N是素数的时候,输出N+1;
3、当只有单质因子时,sum=质因子相应次方+1;
4、当N=2147483647时,它是一个素数,此时输出2147483648,但是它超过int范围,应考虑用long long。
AC代码:
/*************************************************************************> File Name: e.cpp> Author: zzuspy> Mail: zzuspy@qq.com > Created Time: 2014年12月01日 星期一 19时18分33秒 ************************************************************************/#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <cstdlib>#include <cmath>#include <stack>#include <queue>#define LL long long#define max3(a,b,c) max(a,max(b,c))#define min3(a,b,c) min(a,min(b,c))using namespace std;int main(){int n, cas = 1;while(scanf("%d", &n), n){int m = (int)sqrt((double)n+0.5);int t = n, num = 0;LL ans = 0;for(int i=2; i<=m; i++) //分解这个数if(t%i == 0){num++; //记录质因子的个数int tmp = 1;while(t%i==0){tmp*=i;t/=i;}ans+=tmp;}if(n==t) //本身为素数时ans = (LL)n + 1; //必须要加个(LL)else if(num == 1||t!=1) // 单质因子或是剩下一个大于sqrt(n)的质因子的情况 ans += t;// 单质因子情况下t为1,剩余一个大于sqrt(n)质因子时t为剩余质因子 printf("Case %d: %lld\n", cas++, ans);}return 0;}
- UVA - 10791 - Minimum Sum LCM (数论相关!)
- UVA 10791 Minimum Sum LCM (数论)
- UVA 10791 Minimum Sum LCM (数论)
- uva 10791 Minimum Sum LCM(数论)
- UVA 10791 - Minimum Sum LCM(数论)
- UVa 10791 - Minimum Sum LCM (数论 推理)
- UVa 10791 Minimum Sum LCM (数论&素因子分解)
- Minimum Sum LCM(UVA 10791)
- uva 10791 - Minimum Sum LCM
- uva 10791 - Minimum Sum LCM
- UVa 10791 - Minimum Sum LCM
- UVA 10791 - Minimum Sum LCM
- UVA 10791 Minimum Sum LCM
- UVa:10791 Minimum Sum LCM
- uva 10791 - Minimum Sum LCM
- UVA - 10791 Minimum Sum LCM
- UVA 10791 - Minimum Sum LCM
- UVA 10791 - Minimum Sum LCM
- android手机屏幕适应
- [LeetCode]Flatten Binary Tree to Linked List
- 周末小记——一次不成功的组织同学聚会
- hdu 4405 Aeroplane chess (概率)
- Android:Layout_weight的深刻理解
- UVA - 10791 - Minimum Sum LCM (数论相关!)
- QTextEdit 读取一行 删除一行
- 出于安全原因 某些设置由系统管理员管理
- python最简单的计算器
- Android实例demo14之查看网络图片、根据编码格式查看html
- 【VNC】修改VNC分辨率大小
- Oanda外汇账户截至20141201净值403
- Android Push Notification实现信息推送使用
- iOS CoreData