Largest prime factor
来源:互联网 发布:人生必读书籍软件 编辑:程序博客网 时间:2024/06/05 06:56
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
杭电上的一道题目,挺好的一道题,自己的方法通不过,老是超时,上网查到一个好方法,觉得很经典,有必要分析一下。题目的意思首先要明确:求最大质因子在素数表中的位置。2在素数表中的位置为1,3为2,5的位置是3……依此类推(1的位置为0)。
题目说任何数都能由素数相乘得到。
素数的倍数肯定不是素数,这点可以明确,如2是素数,则4,6,8是合数。则4的最大质因数(2)在素数表中的位置为1,6的最大质因数为3,所以只需知道3在素数表中的位置即可。因为每个数都能由素数相乘得到,于是乎可以这样假设,2是所有2的倍数的整数的最大质因数,则所有以2为倍数的整数的最大质因数在素数表中的位置为1,姑且先这么认为,然后再以3为步长,再以5为步长,于是像6,15的最大质因子在素数表中的位置分别调整为2和3了。按照这个思路算法如下:
#include<stdio.h>int prim[1000000];int main(){//cnt为跟踪素数表的位置int n,cnt = 1,i,j;for(i = 2;i < 1000000;i++){if(prim[i])//值非0,为合数,不是我们的目标continue;for(j = i;j < 1000000;j += i)prim[j] = cnt;cnt++;}while(scanf("%d",&n) != EOF)printf("%d\n",prim[n]);return 0;}
讲的有点罗嗦了,其实想通了,也很简单。
- HDU2136:Largest prime factor
- Largest prime factor
- Largest prime factor
- Largest prime factor
- hdoj Largest prime factor
- hdu Largest prime factor
- Largest prime factor 2136
- Largest prime factor
- Largest prime factor
- HDU2136 Largest prime factor
- hdoj Largest prime factor
- Largest prime factor
- hdu2136 Largest prime factor
- hdu Largest prime factor
- Largest prime factor
- HDU2136 Largest prime factor
- 2136 Largest prime factor
- Largest prime factor
- C++ union 使用总结
- const, volitale and explicit
- 如果安卓“终结”,将会有什么样的改变?
- 浮点计算数值误差及PI的蒙特卡罗近似计算
- 求21位数的水仙花数
- Largest prime factor
- javafx.scene.Group的疑问setAutoSizeChildren
- 面向对象的5条基本设计原则
- NOJ [1356] Hey! Where Is My Sweets(╯▔皿▔)╯ 一个数拆分成最多m个数的不重复的方法数
- javafx.application.Application
- alg : 单向链表回环的判断 on drv
- 编译 Windows 版的 netCDF 4.3.0
- Application Execution Modes
- 黑马程序员 7K月薪面试题破解之一 -- 交通灯管理系统