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 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 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 print 4+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 ≤ N ≤ 2 31 − 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 # 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

12 10 5 0

Sample Output

Case 1: 7

Case 2: 7

Case 3: 6

被这个题目卡了好久,,,,好久

题目大意:给你一个整数n,让你求这个n的因数构成的和最小。

题解:我们需要将其进行分解,对n进行唯一分解定理 n = a1^p1 * a2^p2 * a3^p3…,当ai^pi作为一个单独的整数时最优。

注意事项:

1 分解出来的质因子数目必须大于等于2 ,比如 8 只能分出来2 所以对这一类的数值要特判

2 素数特判,对于素数,,不能分解质因子,,特判

3对1进行特判

4注意n ,在分解n的时候可能会改变n的大小,所以要提前保存一下n

5 TLE 注意 分解质数时,不能用i*i<=n 来判断要先对取根号,否则会TLE

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
typedef long long ll; int main(){
ll n,m;
int k=;
while(~scanf("%lld",&n,&m)&&n){
ll nn=n;
if(n==) {
printf("Case %d: %lld\n",++k,n+);
continue ;
}
ll cnt=;
ll m=sqrt(n+);
ll ans=;
for(int i=;i<=m;i++){
if(n%i==){
cnt++;
ll sum=;
while(n%i==){
sum*=i;
n/=i;
}
ans+=sum;
}
}
if(n>){//这一步的判断必须放在下一步的判断前边
cnt++;
ans+=n;
}
if(cnt==||cnt==){
printf("Case %d: %lld\n",++k,nn+);
}
else {
printf("Case %d: %lld\n",++k,ans);
} } return ;
}

F - Minimum Sum LCM的更多相关文章

  1. Minimum Sum LCM(uva10791+和最小的LCM+推理)

    L - Minimum Sum LCM Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submi ...

  2. UVA.10791 Minimum Sum LCM (唯一分解定理)

    UVA.10791 Minimum Sum LCM (唯一分解定理) 题意分析 也是利用唯一分解定理,但是要注意,分解的时候要循环(sqrt(num+1))次,并要对最后的num结果进行判断. 代码总 ...

  3. UVA 10791 Minimum Sum LCM(分解质因数)

    最大公倍数的最小和 题意: 给一个数字n,范围在[1,2^23-1],这个n是一系列数字的最小公倍数,这一系列数字的个数至少为2 那么找出一个序列,使他们的和最小. 分析: 一系列数字a1,a2,a3 ...

  4. 数论-质因数(gcd) UVa 10791 - Minimum Sum LCM

    https://vjudge.net/problem/UVA-10791/origin 以上为题目来源Google翻译得到的题意: 一组整数的LCM(最小公倍数)定义为最小数,即 该集合的所有整数的倍 ...

  5. UVa 10791 Minimum Sum LCM【唯一分解定理】

    题意:给出n,求至少两个正整数,使得它们的最小公倍数为n,且这些整数的和最小 看的紫书--- 用唯一分解定理,n=(a1)^p1*(a2)^p2---*(ak)^pk,当每一个(ak)^pk作为一个单 ...

  6. Minimum Sum LCM UVA - 10791(分解质因子)

    对于一个数n 设它有两个不是互质的因子a和b   即lcm(a,b) = n 且gcd为a和b的最大公约数 则n = a/gcd * b: 因为a/gcd 与 b 的最大公约数也是n 且 a/gcd ...

  7. 最小公倍数的最小和(Minimum Sum LCM )

    #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> u ...

  8. Minimum Sum LCM(uva 10791)

    题意(就是因为读错题意而wa了一次):给一个数字n,范围在[1,2^23-1],这个n是一系列数字的最小公倍数,这一系列数字的个数至少为2 例如12,是1和12的最小公倍数,是3和4的最小公倍数,是1 ...

  9. 题解:UVA10791 Minimum Sum LCM

    原题 题目大意 输入整数\(n(1\le n<2^{31})\) ,求至少两个正整数,是它们的最小公倍数为$ n$,且这些整数的和最小.输出最小的和. 有多组测试输入,以\(0\)结束. 题解 ...

随机推荐

  1. 第十七周Java实验作业

    实验十七  线程同步控制 实验时间 2018-12-10 1.实验目的与要求 (1) 掌握线程同步的概念及实现技术: 多线程并发运行不确定性问题解决方案:引入线程同步机制,使得另一线程使用该方法,就只 ...

  2. 第九周Java实验作业

    实验九 异常.断言与日志 实验时间 2018-10-25 1.实验目的与要求 (1) 掌握java异常处理技术: Java的异常处理机制可以控制程序从错误产生的位置转移到能够进行错误处理的位置. Ja ...

  3. MATLAB 颜色图函数(imagesc/scatter/polarPcolor/pcolor)

    2维的热度图 imagesc imagesc(x, y, z),x和y分别是横纵坐标,z为值,表示颜色 imagesc(theta,phi,slc); colorbar xlabel(); ylabe ...

  4. 使用PostgreSQL注意事项

    一.大小写特别敏感 大写字段需要用“”引号(pg字段名使用“”,MySQL字段名使用``) ******表名以及字段名如果是小写但是为关键字,比如name,则也需使用"": 二.分 ...

  5. 谷歌2019 学术指标发榜:CVPR首次进入Top 10,何恺明论文引用最高!

    [导读]今天,谷歌发布了2019最新版学术指标,对收录的会议和期刊的影响力进行排名.AI类的多个顶会进入榜单Top 100,CVPR更是进入前10,而何恺明的"深度残差网络"单篇引 ...

  6. KDD 2019放榜!录取率仅14%,强调可重现性

    [导读]KDD 2019录取结果终于放榜了,今年Research和ADS两个 track共评审论文1900篇,其中Research track的录取率只有14%.今年也是KDD第一次采用双盲评审政策, ...

  7. POJ - 3255 SPFA+邻接表求次短路径

    题意:给出m条边 , n个顶点,u [ i ]到v [ i ] 的距离w [ i ],求除了最短路的那条最短的边的长度. 思路:之前有做过相似的题,使用迪杰斯特拉算法求单源最短路径,并且记录路径,枚举 ...

  8. 本地缓存Ehcache

    1,什么是Ehcache    Ehcache是纯java的开源缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider.它主要面向通用缓存.Java EE和轻量级容器, ...

  9. 谈谈flex布局实现水平垂直居中

    我们在这要谈的是用flex布局来实现水平和垂直居中.随着移动互联网的发展,对于网页布局来说要求越来越高,而传统的布局方案对于实现特殊布局非常不方便,比如垂直居中.所以09年,W3C 提出了一种新的方案 ...

  10. iOS - scrollView与headerView的视差滚动实现思路

    假设场景:viewController里面有一个scrollView,该scrollView有一个headerView.现在需要将scrollView的滚动contentOffset与headerVi ...