您的位置:首页 > 其它

[HDU2136] Largest prime factor(素数筛)

2017-05-10 14:53 337 查看

传送门

 

题意

给出若干个数n(n<=1000000),求每个n的最大质因子的排名。

质数的排名:如果素数p是第k小的素数,那么p的排名就是k。

 

思路

乍一看不知道怎么搞。

其实可以想想我们怎么筛素数的,每个数都会被它的质因数筛去。

这就和题目一样了。

 

代码

#include <cstdio>

const int MAXN = 1000001;
int notpri[MAXN], cnt = 1;

int main()
{
int i, j, x;
for(i = 2; i < MAXN; i++) if(!notpri[i])
{
for(j = i; j < MAXN; j += i) notpri[j] = cnt;
cnt++;
}
while(~scanf("%d", &x)) printf("%d\n", notpri[x]);
return 0;
}
View Code

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: