hdu 2136 数论+筛选质数+打表
2014-03-13 12:58
399 查看
题目来源:
http://acm.hdu.edu.cn/showproblem.php?pid=2136
Total Submission(s): 6237 Accepted Submission(s): 2206
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
Each line will contain one integer n(0 < n < 1000000).
[align=left]Output[/align]
Output the LPF(n).
[align=left]Sample Input[/align]
1
2
3
4
5
[align=left]Sample Output[/align]
0 1 2 1 3
代码如下:
http://acm.hdu.edu.cn/showproblem.php?pid=2136
Largest prime factor
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6237 Accepted Submission(s): 2206
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
Each line will contain one integer n(0 < n < 1000000).
[align=left]Output[/align]
Output the LPF(n).
[align=left]Sample Input[/align]
1
2
3
4
5
[align=left]Sample Output[/align]
0 1 2 1 3
代码如下:
1 #include<iostream> 2 #include<stdlib.h> 3 #include<stdio.h> 4 #include<math.h> 5 #include<string.h> 6 #include<string> 7 #include<queue> 8 #include<algorithm> 9 #include<map> 10 #define N 1000005 11 using namespace std; 12 int elem ; // 记录n 的最大质因子的位置 13 int cnt; 14 int a ; 15 // 判断质数 16 int prime(int x) 17 { 18 if(x==0 || x==1) return 0; 19 if(x==2) return 1; 20 if(x%2==0) return 0; //剪枝,偶数都不是质数 21 for(int i=3; i*i <=x; i+=2)// 奇质数 22 if(x%i==0 ) return 0; 23 return 1; 24 } 25 // 筛选打表 26 void init() 27 { 28 elem[1]=0; 29 cnt=0; 30 for(int i=2;i<N ;i++) //质数筛选 31 { 32 if(prime(i)) 33 { 34 cnt++; 35 for(int j=i;j<N ; j+=i) 36 elem[j]=cnt; // 记录j(j以 i 为因子) 的最大质因子的位置 37 } 38 } 39 } 40 int main() 41 { 42 init(); 43 int n; 44 while(scanf("%d",&n) !=EOF) 45 { 46 printf("%d\n",elem ); 47 } 48 return 0; 49 }
相关文章推荐
- HDU 2136(数论,筛选打表)
- hdu 2136 Largest prime factor(数论:打表)
- HDU 2136 素数打表+求质数因子
- HDU 2136 Largest prime factor(素数筛选+打表)
- hdu 2136(质数筛选+整数分解)
- hdu 2136 Largest prime factor(素数打表)
- hdu 2582(数论相关定理+素数筛选+整数分解)
- hdu 2136 Largest prime factor【数论、动态规划、二分查找】
- hdu 1262 寻找素数对 数论 打表。
- HDU 4919 Exclusive or (数论 or 打表找规律)
- HDU2136解题报告(筛选法求素数)
- 51Nod 质数中的质数(质数筛选好题)(数论)
- 数论 - 筛法暴力打表 --- hdu : 12876 Quite Good Numbers
- HDU 2136 Largest prime factor(数论)
- HDU_2136——最大质因数,素数筛选法
- hdu 2136 Largest prime factor(打表)
- 【HDU】2136 - Largest prime factor(打表)
- (数论)素数和因式分解hdu-2136;hdu-1492;hdu-1215;hdu1452;hdu-1005;hdu-1852
- [hdu 4215]Number Theory? 数论+打表
- HDU 2136 Largest prime factor (筛选法求素数)