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

题意:输入正整数n,<注意n=2^31-1是素数。结果是2^31已经超int。用long long,>找至少两个数,使得他们的LCM为n且要输出最小的和;

思路:既然LCM是n,那么一定是n的质因子组成的数,又要使和最小,那么就是ans+=[质因子]^[个数]+...;

之前我一直超时,感觉都无语了。

转载请注明出处:寻找&星空の孩子

题目链接:UVA
10791

也欢迎来我开的专题刷题。

哈哈http://acm.hust.edu.cn/vjudge/contest/view.action?cid=77956#overview

AC代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<algorithm>
  4. #include<math.h>
  5. using namespace std;
  6. #define LL long long
  7.  
  8. LL n,sum;
  9.  
  10. inline LL divisor(LL x)
  11. {
  12. int t=0,cnt;
  13. LL tp;
  14. for(int i=2; i<=sqrt(n); i++)
  15. {
  16. cnt=0;
  17. tp=1;
  18. if(x%i==0&&i!=n)
  19. {
  20. while(x)
  21. {
  22. if(x%i==0)
  23. {
  24. cnt++;
  25. x=x/i;
  26. tp=tp*i;
  27. }
  28. else {sum+=tp;break;}
  29. }
  30. t++;
  31. }
  32. if(!x) break;
  33. }
  34. if(x>1){sum+=x;t++;}
  35. // printf("sum=%lld\n",sum);
  36. return t;
  37. }
  38.  
  39. int main()
  40. {
  41.  
  42. int ca=1;
  43. while(scanf("%lld",&n),n)
  44. {
  45. sum=0;
  46. LL m=divisor(n);
  47. if(sum==0||m==1)sum=n+1;
  48. printf("Case %d: %lld\n",ca++,sum);
  49. }
  50. return 0;
  51. }

超时代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<algorithm>
  4. using namespace std;
  5. #define LL long long
  6.  
  7. LL n,sum;
  8.  
  9. inline LL divisor(LL x)
  10. {
  11. int t=0,cnt;
  12. LL tp;
  13. for(int i=2; i<=x; i++)//这么写,就超时 了。。。。。
  14. {
  15. cnt=0;
  16. tp=1;
  17. if(x%i==0&&i!=n)
  18. {
  19. while(x)
  20. {
  21. if(x%i==0)
  22. {
  23. cnt++;
  24. x=x/i;
  25. tp=tp*i;
  26. }
  27. else {sum+=tp;break;}
  28. }
  29. t++;
  30. }
  31. if(!x) break;
  32. }
  33. return t;
  34. }
  35.  
  36. int main()
  37. {
  38.  
  39. int ca=1;
  40. while(scanf("%lld",&n),n)
  41. {
  42. sum=0;
  43. LL m=divisor(n);
  44. if(sum==0||m==1)sum=n+1;
  45. printf("Case %d: %lld\n",ca++,sum);
  46. }
  47. return 0;
  48. }

Minimum Sum LCM(uva10791+和最小的LCM+推理)的更多相关文章

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

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

  2. 数学 - Whu 1603 - Minimum Sum

    Minimum Sum Problem's Link ------------------------------------------------------------------------- ...

  3. Minimum Sum(思维)

    Problem 1603 - Minimum Sum Time Limit: 2000MS   Memory Limit: 65536KB    Total Submit: 563  Accepted ...

  4. Minimum Sum of Array(map迭代器)

    You are given an array a consisting of n integers a1, ..., an. In one operation, you can choose 2 el ...

  5. Minimum Sum of Array(map)

    You are given an array a consisting of n integers a1, ..., an. In one operation, you can choose 2 el ...

  6. Whu 1603——Minimum Sum——————【单个元素贡献、滑窗】

    Problem 1603 - Minimum Sum Time Limit: 2000MS   Memory Limit: 65536KB   Total Submit: 623  Accepted: ...

  7. HDU-3743 Minimum Sum,划分树模板

    Minimum Sum 被这个题坑了一下午,原来只需找一个最中间的数即可,我以为是平均数. 题意:找一个数使得这个数和区间内所有数的差的绝对值最小.输出最小值. 开始用线段树来了一发果断T了,然后各种 ...

  8. geeksforgeeks@ Minimum sum partition (Dynamic Programming)

    http://www.practice.geeksforgeeks.org/problem-page.php?pid=166 Minimum sum partition Given an array, ...

  9. HDU 3473 Minimum Sum (划分树)

    题意:给定一个数组,有Q次的询问,每次询问的格式为(l,r),表示求区间中一个数x,使得sum = sigma|x - xi|最小(i在[l,r]之间),输出最小的sum. 思路:本题一定是要O(nl ...

随机推荐

  1. xcode project

    An Xcode project is a repository for all the files, resources, and information required to build one ...

  2. LVS+Keepalived实现高可用负载均衡(转)

    LVS+Keepalived实现高可用负载均衡   一.原理         1.概要介绍         如果将TCP/IP划分为5层,则Keepalived就是一个类似于3~5层交换机制的软件,具 ...

  3. JsonCpp Documentation

    JsonCpp - JSON data format manipulation library JsonCpp Documentation 0.6.0-rc2 Introduction JSON (J ...

  4. Spring实战笔记2---Bean的装配

    创建应用对象之间协作关系的行为通常成为装配,该篇的主要内容有两个,一个Spring装配Bean的几种方式以及Spring表达式,事实上这两者是分不开的,在Spring中,对象无需自己负责查找或者创建与 ...

  5. pygame系列_游戏窗口显示策略

    在这篇blog中,我将给出一个demo演示: 当我们按下键盘的‘f’键的时候,演示的窗口会切换到全屏显示和默认显示两种显示模式 并且在后台我们可以看到相关的信息输出: 上面给出了一个简单的例子,当然在 ...

  6. Apache James使用的方法及相关心得(转)

    经过一番的辛苦努力,终于把James 配置搞定啦,好记性不如烂笔头啊,赶紧记下我的成功经过,以备以后查阅! 首先要做的就是配置域名的MX 记录啦: 先添加一条A记录: mail.abc.com 指向 ...

  7. UVA 707 - Robbery(内存搜索)

    UVA 707 - Robbery 题目链接 题意:在一个w * h的图上.t个时刻,然后知道一些信息,每一个时刻没有小偷的矩阵位置,问哪些时刻能够唯一确定小偷位置.和确定小偷是否已经逃走,假设没逃走 ...

  8. Visual Studio Code中配置GO开发环境

    在Visual Studio Code中配置GO开发环境 一.GO语言安装 详情查看:GO语言下载.安装.配置 二.GoLang插件介绍 对于Visual Studio Code开发工具,有一款优秀的 ...

  9. windows phone 页面导航(6)

    原文:windows phone 页面导航(6) 页面导航的例子我们使用的是两个页面,从第一个页面(MainPage)导航到第二个页面(SecondPage),然后可以从第二个页面导航到第一个页面 , ...

  10. Effective C++规定45 附加代码

    这部分是额外的代码的博客.键45条款想法已经实现. #include<iostream> using namespace std; template<typename T> c ...