• HDU_2136——最大质因数,素数筛选法


    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.
     
    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
     1 /*
     2 求素数的问题,但数据范围是0 < n < 1000000这么大,
     3 很明显平时我们穷举数字,再一个个去判断是否是素数,
     4 肯定超时,这就要用到效率非常高的求素数的算法了,
     5 竞赛中一般用素数筛选法来处理此类问题,关于素数筛选法生成素数表,
     6 这个网上资料也很多,可以自己搜索学习,简单的说是用了一个思想:
     7 (素数的倍数一定是个合数)。建立一个全1的数组(下标2~N),
     8 先将下标是2的倍数的全置0,再将下标是3的倍数全置0,……,以此类推,
     9 最后剩下的,仍是1的那些下标,就肯定是素数了。
    10 这样做远远比过去穷举数字快是不是??你们可以体会下。
    11 */
    12 #include <cstdio>
    13 const int MAX=1000000;
    14 int prime[MAX]={0};
    15 int mark[MAX]={0};
    16 
    17 void init()
    18 {
    19     for(int i=2,n=1;i<MAX;i++)    //枚举 
    20         {
    21             if(prime[i]==0)        //0标志位代表是素数 
    22                 {
    23                     mark[i]=n++;    //记录这个素数对应的序号 
    24                     for(int j=i;j<MAX;j=j+i)    //素数的倍数,肯定不是素数 
    25                         {
    26                             prime[j]=i;                //去掉标志位,记录最大质因数 
    27                         }
    28                 }
    29         }
    30 }
    31 int main()
    32 {
    33     int n;
    34     init();
    35     while(~scanf("%d",&n))
    36         {
    37             printf("%d
    ",mark[prime[n]]);    //prime中存放这个数的最大质因数 
    38         }
    39     return 0;
    40 }
    ——现在的努力是为了小时候吹过的牛B!!
  • 相关阅读:
    Spring Boot应用程序属性
    Spring Boot Bean和依赖注入
    Spring Boot构建系统
    Spring Boot Tomcat部署
    Spring Boot引导过程
    Spring Boot快速入门
    Spring Boot简介
    eclipse中将项目打包成jar的两种方法,及其问题与解决方法
    配置Zuul代理下游的认证
    WireMock和Spring MVC模拟器
  • 原文地址:https://www.cnblogs.com/pingge/p/3190003.html
Copyright © 2020-2023  润新知