POJ-3292-Semi-prime H-numbers-数筛法

http://poj.org/problem?id=3292


按素筛法筛出1e6的 H-prime-number

再存起来,暴力求出所有Semi-prime H-numbers,存起来

每次二分查找即可

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include<stack>
using namespace std;  
int hprim[1000000];
bool f[1000000+500];
int h_s_p[120000+500]; 
int main()
{
	__int64 i,j;
	f[1]=false;
	for (i=4;i<=1000000;i+=4)		//筛H-prime-number
	{
		__int64 re=i+1;
		if (f[re]==false)
		{
			for (j=re*2;j<=1000000;j+=re)
				f[j]=true;
		}
	}
	int ok=0;
	for (i=4;i<=1000000;i+=4)	//存储h-prime-number
	{
		if (f[i+1]==false)
			hprim[++ok]=i+1;
	}

	int cun=0;
	for (i=1;i<=ok;i++)		//构造H--semi-primes
	{
		for (j=i;j<=ok;j++)
		{
			__int64 ret=hprim[i];
				ret*=hprim[j];
			if (ret>1000001)
				break; 
			h_s_p[++cun]=ret;
		}
	}

	sort(h_s_p+1,h_s_p+1+cun);
	cun=unique(h_s_p+1,h_s_p+cun)-h_s_p; //去重 
	int n;
	while(scanf("%d",&n)!=EOF)
	{
			if (!n)break;
		int it=upper_bound(h_s_p+1,h_s_p+1+cun,n)-h_s_p;
		it--;
		printf("%d %d\n",n,it); 
	}
	return 0;
} 


你可能感兴趣的:(POJ-3292-Semi-prime H-numbers-数筛法)