题目学习——hduoj-2136
来源:互联网 发布:hash-256函数算法原理 编辑:程序博客网 时间:2024/05/19 14:36
期末结束终于可以放下其他事情来学习自己感兴趣的东西了>_<
Largest prime factor
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Problem Description
Everybody knows any number can be combined by the prime number.
Now, your task is telling me what position of the largest prime factor.
The position of prime 2 is 1, prime 3 is 2, and prime 5 is 3, etc.
Specially, LPF(1) = 0.
Now, your task is telling me what position of the largest prime factor.
The position of prime 2 is 1, prime 3 is 2, and prime 5 is 3, etc.
Specially, LPF(1) = 0.
Input Each line will contain one integer n(0 < n < 1000000).
Output Output the LPF(n).
Sample Input
12345
Sample Output
01213
Author
Wiskey
在学习Eratosthenes筛选法后,想硬套,然后暴力扫,结果TLE了
#include <iostream>#include <cstdio>using namespace std;const int maxp=1000005;bool notprime[maxp];int prime[maxp];void loc(){ notprime[0]=1,notprime[1]=1; prime[0]=0; for(int i=2;i<maxp;++i) { if(!notprime[i]) { prime[++prime[0]]=i; for(long long int j=(long long int)i*i;j<maxp;j+=i) notprime[j]=1; } }}int main(){ int n; loc(); while(scanf("%d",&n)!=EOF) { if(!notprime[n]) { for(int i=1;i<=prime[0];++i) { if(prime[i]==n) { cout<<i<<endl; break; } } } else if(n==1||n==0) cout<<'0'<<endl; else { for(int i=prime[0];i>0;--i) { if(!(n%prime[i])) { cout<<i<<endl; break; } } } } return 0;}
后来在discuss看到用notprime[j]=i表示非质数j的质因子是i,但没考虑到i是最小的,而不是题目要求最大的。
#include <iostream>#include <cstdio>using namespace std;const int maxp=1000005;int notprime[maxp];int prime[maxp];void loc(){ notprime[0]=1,notprime[1]=1; prime[0]=0,prime[1]=0; for(int i=2;i<maxp;++i) { if(!notprime[i]) { prime[i]=++prime[0]; for(long long int j=(long long int)i*i;j<maxp;j+=i) notprime[j]=i; } }}int main(){ int n; loc(); while(scanf("%d",&n)!=EOF) { if(!notprime[n]) cout<<prime[n]<<endl; else cout<<prime[notprime[n]]<<endl; } return 0;}于是乎,将0~i*i的notprime|i保持更新,虽然不是很好的算法,800msAC
#include <iostream>#include <cstdio>using namespace std;const int maxp=1000005;int notprime[maxp];int prime[maxp];void loc(){ notprime[0]=1,notprime[1]=1; prime[0]=0,prime[1]=0; for(int i=2;i<maxp;++i) { if(!notprime[i]) { prime[i]=++prime[0]; for(int j=2*i;j<maxp;j+=i) notprime[j]=i; } }}int main(){ int n; loc(); while(scanf("%d",&n)!=EOF) { if(!notprime[n]) cout<<prime[n]<<endl; else cout<<prime[notprime[n]]<<endl; } return 0;}
学习内容:http://www.cppblog.com/menjitianya/archive/2015/12/02/212395.html
在此向dalao致敬。
阅读全文
0 0
- 题目学习——hduoj-2136
- 题目学习——HDUOJ-1492
- 题目学习——HDUOJ-2674
- 题目学习——HDUOJ-2669
- 课堂学习——HDUOJ-1215
- 课堂学习——HDUOJ-1452
- hduoj 题目分类
- HDUOJ题目分类
- hduoj题目详细分类
- hduoj题目分类
- HDUoj PBD 题目1703
- HDUOJ 无题 题目1871
- hduoj 排序 题目1106
- hduoj 4196 ——Remoteland
- BigNums——HDUOJ 1002
- BigNums——HDUOJ 1042
- BigNums——HDUOJ 1047
- BigNums——HDUOJ 1063
- bootstrap checkbox ratio 元素换行对齐的问题
- struts2是如何启动的?
- 图片选择和图片压缩
- spark-jobserver使用笔记
- kotlin项目开发总结
- 题目学习——hduoj-2136
- Jdk类库注解
- Unity3D的四种坐标系
- 数据结构与算法——希尔排序
- struts2框架的核心控制器是什么?它有什么作用?
- Python——psutil的使用(收集系统基础信息)
- Apache Hadoop与第三方Hadoop-CDH,HDP,MapR的分析与比较
- caffe_mobile
- struts2配置文件的加载顺序?