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代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
#define LL long long LL n,sum; inline LL divisor(LL x)
{
int t=0,cnt;
LL tp;
for(int i=2; i<=sqrt(n); i++)
{
cnt=0;
tp=1;
if(x%i==0&&i!=n)
{
while(x)
{
if(x%i==0)
{
cnt++;
x=x/i;
tp=tp*i;
}
else {sum+=tp;break;}
}
t++;
}
if(!x) break;
}
if(x>1){sum+=x;t++;}
// printf("sum=%lld\n",sum);
return t;
} int main()
{ int ca=1;
while(scanf("%lld",&n),n)
{
sum=0;
LL m=divisor(n);
if(sum==0||m==1)sum=n+1;
printf("Case %d: %lld\n",ca++,sum);
}
return 0;
}

超时代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define LL long long LL n,sum; inline LL divisor(LL x)
{
int t=0,cnt;
LL tp;
for(int i=2; i<=x; i++)//这么写,就超时 了。。。。。
{
cnt=0;
tp=1;
if(x%i==0&&i!=n)
{
while(x)
{
if(x%i==0)
{
cnt++;
x=x/i;
tp=tp*i;
}
else {sum+=tp;break;}
}
t++;
}
if(!x) break;
}
return t;
} int main()
{ int ca=1;
while(scanf("%lld",&n),n)
{
sum=0;
LL m=divisor(n);
if(sum==0||m==1)sum=n+1;
printf("Case %d: %lld\n",ca++,sum);
}
return 0;
}

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. python中print,return和yield的区别

    def func1(): for i in range(1, 5): print i def func2(): for i in range(1, 5): return i def func3(): ...

  2. FZU 2113(数位dp)

    题目连接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=38054 题意:求区间[a,b]中包含'1'的个数. 分析:数位dp ...

  3. 项目中那些事|ListView中嵌套ListView问题

    要在一个ListView中放入另一个ListView,也即在一个ListView的每个 item 中放入另外一个ListView.但刚开始的时候,会发现放入的子ListView会显示不完全(我这里只显 ...

  4. python面向对象具体解释(上)

    创建类 Python 类使用 class 关键字来创建.简单的类的声明能够是关键字后紧跟类名: class ClassName(bases): 'class documentation string' ...

  5. hdu 4472 Count (2012 ACM-ICPC 成都现场赛)

    递推,考虑到一n可以由i * j + 1组合出来,即第二层有j个含有i个元素的子树...然后就可以了.. #include<algorithm> #include<iostream& ...

  6. lca转RMQ

    这个博客写得好 #include <stdio.h> #include <vector> #include <string.h> using namespace s ...

  7. 黄聪:Microsoft Enterprise Library 5.0 系列教程(六) Security Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(六) Security Application Block 开发人员经常编写需要安全功能的应用程序.这些应用程序 ...

  8. 王立平--Program Files (x86)

    window7根据系统.program files(x86) 它是应用程序目录,在64下位系统.为了更好的相容性32位程序,在一些安装32位程序(请注意,有些节目自己是32位),将默认被安装progr ...

  9. VC6 鼠标钩子 最简单样例

    Windows系统是建立在事件驱动的机制上的,说穿了就是整个系统都是通过消息的传递来实现的.而钩子是Windows系统中非常重要的系统接口,用它能够截获并处理送给其它应用程序的消息,来完毕普通应用程序 ...

  10. Netty In Action中国版 - 第二章:第一Netty程序

    本章介绍 获得Netty4最新的版本号 设置执行环境,以构建和执行netty程序 创建一个基于Netty的server和client 拦截和处理异常 编制和执行Nettyserver和client 本 ...