UVa 10791 - Minimum Sum LCM(唯一分解)
2015-07-27 19:47
274 查看
首先筛选出2到(231−1)‾‾‾‾‾‾‾‾‾√\sqrt{(2^31-1)}的素数,然后对每个输入的数进行分解。最后若只有一个素因子,则得出的和要加1。输入为1时,输出为2。
#include<cstdio> #include<cstring> #include<cmath> typedef long long LL; using namespace std; const int maxn=46350; bool np[maxn]; int p[4800],prime; int e[4800]; void init(){ for(int i=2;i<maxn;++i){ if(!np[i]) p[prime++]=i; for(int j=0;j<prime&&i*p[j]<maxn;++j){ np[i*p[j]]=true; if(!(i%p[j])) break; } } return; } LL fexp(int a,int b){ LL cur=1,tmp=a; while(b){ if(b&1) cur=cur*tmp; tmp=tmp*tmp; b>>=1; } return cur; } int main(){ init(); int t=0; LL n; while(~scanf("%lld",&n)&&n){ if(n==1){ printf("Case %d: 2\n",++t); continue; } memset(e,0,sizeof(e)); int cnt=0; LL ans=0; for(int i=0;i<prime;++i){ while(n%p[i]==0) n/=p[i],++e[i]; if(e[i]>0) ++cnt,ans+=fexp(p[i],e[i]); if(n==1) break; } if(n!=1) ++cnt,ans+=n; if(cnt==1) ++ans; printf("Case %d: %lld\n",++t,ans); } return 0; }
相关文章推荐
- html dom
- 沉浸式状态栏
- SVN版本控制业务流程详解
- 杭电1019
- uva 12304(圆的相关函数模板)
- http格式
- 2015多校联合训练总结(2)
- USACO——Sorting a Three-Valued Sequence
- 音乐播放器实现功能之—
- 各种排序算法总结
- hdu 1520
- 下雨有感
- 2003 求绝对值
- 快速学习stl中集合set
- [LeetCode] Unique Paths
- hdu2492 Ping pong
- 杭电ACM1282——回文数猜想
- selection-内容选中跟光标移动
- PAINT和WM_ERASEBKGND消息
- new/delete 和malloc/free 的区别一般汇总