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. 李宏毅老师机器学习课程笔记_ML Lecture 1: 回归案例研究

    引言: 最近开始学习"机器学习",早就听说祖国宝岛的李宏毅老师的大名,一直没有时间看他的系列课程.今天听了一课,感觉非常棒,通俗易懂,而又能够抓住重点,中间还能加上一些很有趣的例子 ...

  2. Spring02——Spring 中 Bean 的生命周期及其作用域

    在前一篇文章中,我们已经介绍了 Spring IOC 的相关知识,今天将为个位介绍 Spring 中 Bean 的相关知识.关注我的公众号「Java面典」,每天 10:24 和你一起了解更多 Java ...

  3. Android适配器

    Android适配器 安卓的适配器在我看来是一个非常重要的知识点,面对形式相同但数据源较多的情况时,适配器是一个比较好的解决方法.数据适配器是建立了数据源与控件之间的适配关系,将数据源转换为控件能够显 ...

  4. coding++:Spring_IOC(控制反转)详解

    IoC是什么: 1):Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想. 2):在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的 ...

  5. Sprinboot 整合 RabbitMQ,RabbitMQ 消息重试机制

    当消费者消费消息的时候,出现错误,RabbitMQ 本身会有

  6. NSObject常用方法

    类 @interface NSObject <NSObject> { Class isa OBJC_ISA_AVAILABILITY; } // 初始化加载 + (void)load; / ...

  7. 9.Maven的生命周期

    Clean Lifecycle: 在进行真正的构建之前进行一些清理工作. Default Lifecycle :构建的核心部分,编译,测试,打包,部署等等. Site Lifecycle : 生成项目 ...

  8. Java面试金典

    1,将构造函数声明为私有的作用 构造函数私有化,保证类以外的地方不能直接实例化该类,这种情况下,要创建这个类的实例,只能提供一个公共静态方法,像工厂方法模式,由于构造函数私有化,不能被继承. 2,在t ...

  9. .Net微服务实践(二):Ocelot介绍和快速开始

    目录 介绍 基本原理 集成方式 快速开始 创建订单服务 创建产品服务 创建网关 运行验证 最后 上篇.Net微服务实践(一):微服务框架选型 我们对微服务框架整体做了介绍,接下来我们从网关Ocelot ...

  10. Supervisor 使用和进阶4 (Event 的使用)

    本文主要介绍 supervisor Event 的功能. supervisor 作为一个进程管理工具,在 3.0 版本之后,新增了 Event 的高级特性, 主要用于做(进程启动.退出.失败等)事件告 ...