hdu 4715 Difference Between Primes 2013年ICPC热身赛A题 素数水题

题意:给出一个偶数(不论正负),求出两个素数a,b,能够满足 a-b=x,素数在1e6以内。

只要用筛选法打出素数表,枚举查询下就行了。

我用set储存素数,然后遍历set里面的元素,查询+x后是否还是素数。

注意,偶数有可能是负数,其实负数就是将它正数时的结果颠倒就行了。

代码:

/*
 *  Author:      illuz <iilluzen[at]gmail.com>
 *  Blog:        http://blog.csdn.net/hcbbt
 *  File:        10.cpp
 *  Create Date: 2013-09-08 12:55:09
 *  Descripton:  10 
 */

#include <cstdio>
#include <cmath>
#include <set>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

set<int> m;
#define MAX_P  1000001
int nList[MAX_P] = {0};
void Calc() 
{
	int n,p,t,sq=(int)sqrt(MAX_P*2+1);
	for (n=3;n<=sq;n+=2)
	{
		if (nList[n>>1]) continue;
		for (t=n*n;t<=MAX_P<<1;t+=n<<1) //筛选循环
			nList[t>>1] = 1;
	}
	m.insert(2);
	for (n=t=1;t<MAX_P;++t)
	{
		if (nList[t]) continue;
		m.insert((t<<1)+1);
		if (++n==10)
			n=0;
	}
}
/****** TEMPLATE ENDS ******/

const int MAXN = 0;

int main() {
	Calc();
	int t, n;
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n);
		int nn = abs(n);
		bool flag = false;
		for (set<int>::iterator i = m.begin(); i != m.end(); i++) {
			if (m.count(*i + nn) != 0) {
				flag = true;
				if (n < 0) printf("%d %d\n", *i, *i + nn);
				else printf("%d %d\n", *i + nn, *i);
				break;
			}
		}
		if (!flag) printf("FAIL\n");
	}
	return 0;
}



  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值