UVa 11105 - Semi-prime H-numbers(筛选法)

来源:互联网 发布:用telnet测试端口 编辑:程序博客网 时间:2024/06/15 09:25

类似于筛选素数的方法,枚举前缀和即可。

#include <cstdio>using namespace std;const int maxn = 1000010;bool nhp[maxn];int hp[maxn],hpnum;int cnt[maxn];void pre() {    for(int i = 5; i < 1010; i += 4)        if(!nhp[i])            for(int j = i * i; j < maxn; j += i)                nhp[j] = true;    for(int i = 5; i < maxn; i+=4)        if(!nhp[i]) hp[hpnum++] = i;    for(int i = 0; i < hpnum; ++i)        for(int j = 0; j < hpnum && hp[i] * hp[j] < maxn; ++j)            cnt[hp[i]*hp[j]] = 1;    for(int i = 1; i < maxn; ++i) cnt[i] += cnt[i - 1];}int main() {    pre();    int n;    while(~scanf("%d", &n) && n)        printf("%d %d\n", n, cnt[n]);    return 0;}
0 0
原创粉丝点击