uva 11105 Semi-Prime H-number 筛法

<pre name="code" class="html">
点击打开链接
 
</pre><pre code_snippet_id="1973199" snippet_file_name="blog_20161108_4_7065142" name="code" class="cpp">
</pre><pre code_snippet_id="1973199" snippet_file_name="blog_20161108_5_2746744" name="code" class="cpp">#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <map>
using namespace std;
typedef long long ll;
const int N=1e6+1;
const ll inf=1<<30;
vector<int> h;
int vis[N];
ll pri[N];
int main()
{
	int n;
	memset(vis,0,sizeof(vis));
	for(int i=0;4*i+1<=N;i++)
	{
		h.push_back(i*4+1);
	}
	vis[h[0]]=1;
	int pn=0;
	for(int i=1;i<h.size();i++)//晒出h素数 
	{
		if(!vis[h[i]])	
		{
			pri[pn++]=h[i];
			for(int j=h[i]+h[i];j<=N;j+=h[i])
			{
				vis[j]=1;//H素数只能写成1*h 素数的倍数不是素数 
			}
		}
	}
	
	while(cin>>n&&n)
	{
		memset(vis,0,sizeof(vis));
		ll ans=0;
		// 1e4 cout<<pn<<endl;
		
		//筛出H素数后 定义Semi-prime为两个H素数乘积 暴力即可 
		for(int i=0;i<pn;i++)
		{
			if(pri[i]*pri[i]>n) //n<=1e6  
			break;
			for(int j=i;j<pn;j++)
			{
				int mul=pri[i]*pri[j];
				if(mul<=n)
				{	
					if(!vis[mul])
					{
						vis[mul]=1;
						ans++;
					}
				}
				else
				break;
			}
		}
		cout<<n<<' '<<ans<<endl;
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值