zoukankan      html  css  js  c++  java
  • hduoj 2136 Largest prime factor【素数筛+素数打表】

    Largest prime factor

    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. 

    Input

    Each line will contain one integer n(0 < n < 1000000). 

    Output

    Output the LPF(n). 

    Sample Input

    1
    2
    3
    4
    5

    Sample Output

    0
    1
    2
    1
    3

     【题意】

    就是求最大素因子的在素数表中的位置。 比如说这个数是74,那么它的最大素因子就是37,在素数表中的位置是12,(2是第一个,3是第二个,1是第0个)

    素数n = 1 * n故素数的LPF就是其本身;非素数4 = 2 * 2;4的最大素因子为2,8 = 2 * 2 * 2故8的最大素因子也是2,以此类推。

    【思路】

    用素数筛+素数打表

     1 #include<cstdio>
     2 #include<string.h>
     3 #include<algorithm>
     4 using namespace std;
     5 #define maxn 1000010
     6 
     7 int a[maxn];
     8 
     9 void init()
    10 {
    11     int k = 1;
    12     memset(a, 0, sizeof(a));
    13     for(int i = 2; i <= maxn; i++)
    14     {
    15         if(!a[i])//a[i]是素数时 
    16         {
    17             a[i] = k;
    18             for(int j = i + i; j <= maxn; j += i)
    19                 a[j] = k;
    20             k++;
    21         } 
    22     }    
    23 }
    24 int main()
    25 {
    26     int n;
    27     init();
    28     while(scanf("%d", &n) != EOF)
    29         printf("%d
    ", a[n]);
    30     return 0;
    31 }
  • 相关阅读:
    SQL补充
    SQL练习题
    HDU 2907
    Codeforces 814D
    Codeforces 814C
    Codeforces 1004D
    Codeforces 1004E
    CodeForces 909F
    CodeForces 909E
    CodeForces 909D
  • 原文地址:https://www.cnblogs.com/123tang/p/5974430.html
Copyright © 2011-2022 走看看