现在的位置: 首页 > 综合 > 正文

HDOJ 2136 Largest prime factor 13.04.21 周赛结题报告 (素数筛选法)

2018年01月12日 ⁄ 综合 ⁄ 共 903字 ⁄ 字号 评论关闭

Largest prime factor

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

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
解题思路:
题意是求所给数的最大质因数。并且求出这个质因数在素数表中为第几位。
一开始把题目理解错了,以为是把一个数拆分成两个质因数相加。结果果断TLE。。
只能说英语不行啊。。。
素数筛选。在给素数标记时有个巧妙的小窍门,详见代码。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int a[1000005][2]= {0};
int main ()
{
    int n,i,j,t=1;
    for (i=2; i<=1000000; i++)
    {
        if (a[i][0]==0)
        {
            for (j=i*2; j<=1000000; j+=i)
            {
                a[j][0]=1;
                a[j][1]=t;  //用t来表示这是第几个素数
            }
            a[i][1]=t;
            t++;    //每次标记完一个素数后,将标记量加1
        }
    }
    while(scanf("%d",&n)!=EOF)
    {
        printf("%d\n",a[n][1]);
    }
    return 0;
}

抱歉!评论已关闭.