HDU 2136 Largest prime factor (筛选法求素数)

Largest prime factor

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 7297    Accepted Submission(s): 2589

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

Author

Wiskey

题目大意:每个素数在素数表中都有一个序号,设1的序号为0,则2

的序号为1,3的序号为2,5的序号为3,以此类推。现在要求输出所

给定的数n的最大质因子的序号,0<n<1000000。

#include<iostream>
#include<stdio.h>
using namespace std;
int pi;
bool flag[1000010];
int in;
int n;
int a[1000010];
int getprime()
{
    for(int i=2;i<1000010;++i)
    {
        if(!flag[i])
        {
            in++;
            for(int j=i;j<1000010;j+=i)
            {
                flag[j]=true;
                a[j]=in;//记录序号
            }
        }
    }
}
int main(int argc, char *argv[])
{
    //freopen("2136.in","r",stdin);
    getprime();
    while(scanf("%d",&n)!=EOF)
    {
        printf("%d\n",a[n]);
    }
    return 0;
}
时间: 2024-09-28 07:26:06

HDU 2136 Largest prime factor (筛选法求素数)的相关文章

hdu how many prime numbers 筛选法求素数

/* * hdu How many prime numbers * date 2014/5/13 * state AC */ #include <iostream> #include <cmath> #include <cstdio> using namespace std; bool isPrime(int x) { int sqr=sqrt(x*1.0); for(int i=2;i<=sqr;i++) { if(x%i==0)return false; }

hdu 2136 (Largest prime factor)就是简单 的筛选素数法

Largest prime factor Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7009    Accepted Submission(s): 2482 Problem Description Everybody knows any number can be combined by the prime number. Now

HDU 2136 Largest prime factor (最大素因子序号,cin超时呀!!!)

Largest prime factor Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10004    Accepted Submission(s): 3534 Problem Description Everybody knows any number can be combined by the prime number. No

HDU 2136 Largest prime factor 数论

Largest prime factor Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u 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

HDU 2136 Largest prime factor (素数打表。。。)

题意:给你一个数,让你求它的最大因子在素数表的位置. 析:看起来挺简单的题,可是我却WA了一晚上,后来终于明白了,这个第一层循环不是到平方根, 这个题和判断素数不一样,只要明白了这一点,就很简单了. 代码如下: #include <iostream> #include <cstdio> #include <algorithm> #include <queue> #include <vector> #include <cstring>

HDU 2136 Largest prime factor

水题,晒一遍素数并标注就OK了. #include<cstdio> #include<cstring> #include<string> #include<queue> #include<algorithm> #include<map> #include<stack> #include<iostream> #include<list> #include<set> #include<

HDU 2136 Largest prime factor 参考代码

#include <iostream> #include <vector> #include <cmath> using namespace std; const int MAX=1000001; bool nums[MAX]; int indexes[MAX]; void eraosthenes() { int n=sqrt((double)MAX); for(int i=2; i<=n; i++) { int pos=i*i; while(pos<MAX

筛选法求素数

筛选法求素数,不断的用3,5,7,等素数作为筛子,筛除这些数的倍数,即将合数筛除.用辅助数组p记录数i是否是素数. vector<int> prime(int n) { vector<int> p(n+1); for(int i=2;i<=n;i+=2) { if(i%2==0&&i>2) p[i]=0; else p[i]=1; } for(int i=3;i<=(int)(sqrt((double)n));i+=2) { if(p[i]) fo

【算法】普通方法和筛选法求素数

素数指的是因子只有1和本身的数(1不是素数),求解素数在数学上应用非常广泛,而求解n以内的素数也是我们编程时常遇到的问题,在这个问题上,筛选法求解素数运行得非常快.下面首先介绍如何判断一个是不是素数,然后介绍用普通方法求n以内的素数,接着是筛选法求n以内的素数,最后是两种算法的运行时间比较 判断一个数是不是素数 算法思想:判断小于等于一个数的平方的所有大于1的整数是不是能整除这个数,如果能,则表明这个数不是素数:反之,则是素数. //判断一个数是否为素数 bool isPlain(int val