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. 大多数人不知道的表格其他写法的onmouseover效果

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  2. 【2019多校第一场补题 / HDU6578】2019多校第一场A题1001Blank——dp

    HDU6578链接 题意 有一串字符串,仅由 {0,1,2,3}\{0, 1, 2, 3\}{0,1,2,3} 组成,长度为 nnn,同时满足 mmm 个条件.每个条件由三个整数组成:l.r.xl.r ...

  3. 【2019牛客暑期多校第一场】E题ABBA

    题目链接 大致题意 有(n+m)(n + m)(n+m)个字母A和(n+m)(n + m)(n+m)个字母B,组成一个长度为 2∗(n+m)2*(n + m)2∗(n+m)的字符串,并且使得字符串中有 ...

  4. iOS URL schemes

    来源:知乎 launch center pro支持的参数主要有两个,[prompt]文本输入框和[clipboard]剪贴板 淘宝宝贝搜索 taobao://http://s.taobao.com/? ...

  5. 推荐 | 7个你最应该知道的机器学习相关github项目

    欢迎大家关注我们的网站和系列教程:http://www.tensorflownews.com/,学习更多的机器学习.深度学习的知识! 磐石 目录: 介绍 Person Blocker(人体自动遮挡) ...

  6. API开放平台接口设计-------基于OAuth2.0协议方式

    1,简介OAuth http://www.ruanyifeng.com/blog/2019/04/oauth_design.html OAuth 是什么? http://www.ruanyifeng. ...

  7. nginx负载均衡例子

    upstream demo { ip_hash;//客户连接后, 一直用这个IP,直到会话结束,否则,动态程序可能会在换IP后出错 server 192.168.1.1:80 weight=5 | d ...

  8. logstash用jdbc插件将数据库内容导入elasticsearch时间字段相差5小时

    logstash将mysql的数据导入elasticsearch之后发现时间字段的相差5个小时 解决办法: 在数据库连接配置后面加上?serverTimezone=UCT这个就OK了 logstash ...

  9. 算法(algorithm)

    算法是什么? 算法是指令的集合,是为解决特定问题而规定的一系列操作. 它是明确定义的可计算过程,以一个数据集合作为输入,并产生一个数据集合作为输出. 一个算法通常来说具有以下五个特性: 1.输入:一个 ...

  10. IE,Google Chrome等浏览器,调试模式在控制台可以手动调用页面的方法来调试

    IE,Google Chrome等浏览器,调试模式在控制台可以手动调用页面的方法来调试,这种方式也可以进断点.