zoukankan      html  css  js  c++  java
  • hdu 2136(质数筛选+整数分解)

    Largest prime factor

    Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 9993    Accepted Submission(s): 3528


    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
     
    #include <stdio.h>
    #include <math.h>
    #include <iostream>
    #include <algorithm>
    #include <string.h>
    #include <stdlib.h>
    using namespace std;
    typedef long long LL;
    const int N = 1000000;
    bool p[N]; ///为false代表是素数
    int idx[N];
    void init(){
        memset(p,false,sizeof(p));
        int id = 1;
        for(int i=2;i<N;i++){
            if(!p[i]){
                idx[i] = id++;
                for(LL j=(LL)i*i;j<N;j+=i){
                    p[j] = true;
                }
            }
        }
    }
    int getMax(int n){
        int Max = -1;
        for(int i=2;i*i<=n;i++){
            if(n%i==0){
                while(n%i==0){
                    n/=i;
                }
                Max = max(i,Max);
            }
        }
        if(n>1) Max = max(Max,n);
        return Max;
    }
    int main()
    {
        init();
        int n;
        while(scanf("%d",&n)!=EOF){
            if(n==1) printf("0
    ");
            else{
                int Max = getMax(n);
                printf("%d
    ",idx[Max]);
            }
        }
    }

     O(n)的素数筛

    /*求第n个质数*/
    #include <stdio.h>
    #include <math.h>
    #include <iostream>
    #include <algorithm>
    #include <string.h>
    #include <stdlib.h>
    using namespace std;
    typedef long long LL;
    const int N = 49979693;
    int n,m;
    int p[3000001];//存储素数
    bool a[N];
    
    //O(n) 素数筛
    void init() {
        memset(a,false,sizeof(a));//初始全部为素数
        int num=0;
        for(int i=2;i<N;++i) {
            if(!a[i]) p[num++]=i;
            for(int j=0;(j<num&&i*p[j]<N);++j) {
                a[i*p[j]]=1;
                if(i%p[j] == 0) break;
            }
        }
    }
    
    int main(){
        init();
        int n;
        int k = 0;
        while(scanf("%d",&n)!=EOF){
           if(n==0) break;
           printf("Case %d: %d
    ",++k,p[n-1]);
        }
    }
  • 相关阅读:
    windows的80端口被占用时的处理方法
    Ansible自动化运维工具安装与使用实例
    Tomcat的测试网页换成自己项目首页
    LeetCode 219. Contains Duplicate II
    LeetCode Contest 177
    LeetCode 217. Contains Duplicate
    LeetCode 216. Combination Sum III(DFS)
    LeetCode 215. Kth Largest Element in an Array(排序)
    Contest 176 LeetCode 1354. Construct Target Array With Multiple Sums(优先队列,递推)
    Contest 176
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5520376.html
Copyright © 2011-2022 走看看