POJ 3292 Semi-prime H-numbers

题目描述

考虑到H-素数的定义与素数类似,那么能否用一种类似于筛素数的方式筛出所有的H-素数呢?

假设i是H-素数,那么i*(4*n+1)一定是H数且不是H-素数。

那么就可以筛出所有的H-素数,进而求出所有的H-合成数。

#include<complex>
#include<cstdio>
using namespace std;
const int N=1e6+7;
int tot;
int H[N],a[N];
bool is[N],check[N];
int qread()
{
    int x=0;
    char ch=getchar();
    while(ch<'0' || ch>'9')ch=getchar();
    while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x;
}
int main()
{
    for(int i=5;i<N;i+=4)
    {
        if(check[i])continue;
        H[++tot]=i;
        for(int j=i*5;j<N;j+=i*4)
            check[j]=1;
    }
    for(int i=1;i<=tot;i++)
        for(int j=1;j<=i && H[i]*H[j]<N;j++)
            is[H[i]*H[j]]=1;
    for(int i=1;i<N;i++)
        a[i]=a[i-1]+is[i];
    int x;
    while(1)
    {
        x=qread();
        if(!x)break;
        printf("%d %d\n",x,a[x]);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/LeTri/p/9063702.html