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. redis的分布式解决方式--codis (转)

    codis是豌豆荚开源的分布式server.眼下处于稳定阶段. 原文地址:https://github.com/wandoulabs/codis/blob/master/doc/tutorial_zh ...

  2. ArrayList线程不安全?

    ArrayList是线程不安全的,轻量级的.如何使ArrayList线程安全? 1.继承Arraylist,然后重写或按需求编写自己的方法,这些方法要写成synchronized,在这些synchro ...

  3. hdu3966(树链剖分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意:一颗树上,每个点有权值,定义三种操作: 1)I操作表示从a到b节点之间的节点都加上一个值 ...

  4. .NET开发必看资料53个+经典源码77个

    目录0豆下载:http://down.51cto.com/data/426019 附件预览: 基于.net构架的留言板项目大全源码 http://down.51cto.com/zt/70 ASP.ne ...

  5. 国内云存储对比: 阿里云、腾讯云、Ucloud、首都在线

    阿里云的数据存储<http://www.aliyun.com/product/rds/> RDS — 关系型数据库服务(Relational Database Service,简称RDS) ...

  6. linq 中执行方法

    Database1Entities db = new Database1Entities(); protected void Page_Load(object sender, EventArgs e) ...

  7. oracle11g创建新的用户和改动最大连接数

    create user test identified by root; grant create session,resource to root; alter user test account ...

  8. HDU 1201

    18岁生日 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  9. BrowserSync使用

    在Gulp中使用BrowserSync 2016-02-24 23:47 by 那时候的我, 116 阅读, 0 评论, 收藏, 编辑 博客已迁移至http://lwzhang.github.io. ...

  10. 8皇后-----回溯法C++编程练习

    /* * 八皇后问题回溯法编程练习 * 在8×8的棋盘上,放置8个皇后,两个皇后之间不能两两攻击 * 也即,直线,垂直45度.135度方向不能出现两个皇后 * * copyright Michael ...