有趣的游戏

34 篇文章 0 订阅
4 篇文章 0 订阅

【简要题面】有四组数字 a , b , c , d a,b,c,d abcd a , b , c a,b,c a,b,c的个数小于1000,d的个数小于1e5。对于每个d求是否存在 a i + b i + c i = d a_i+b_i+c_i=d ai+bi+ci=d a i 、 b i 、 c i a_i、b_i、c_i aibici均为 a 、 b 、 c a、b、c abc数组中的一个数)。

【分析】
由于 a 、 b 、 c a、b、c abc中数字的大小未定,所以我们很可惜的不能用f数组进行预处理,实际上复杂度也不允许我们这么做。比较巧妙的做法是先将 a 、 b a、b ab中的数字先相加用map存,再在读入d数组的时候枚举a数组和d数组相减,并在map中查询是否存在 d i − a j d_i-a_j diaj。鉴于map自带 l o g n + s t l 常 数 log_n+stl常数 logn+stl所以我们可以用 H a s h t a b l e Hash table Hashtable代为实现。

【code】

#pragma GCC optimize(2)
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1100;
const int mod=1e6+7;
const int key=1e6+9;
int la,lb,lc,ld;
int a[maxn],b[maxn],c[maxn];
struct hash_table{
	struct node{
		int val,nxt;
	}num[mod<<1];
	int head[mod],tot;
	void add(int x){int p=1ll*x*key%mod;num[++tot]=(node){x,head[p]},head[p]=tot;}
	bool find(int x){
		int p=1ll*x*key%mod;
		for(int i=head[p];i;i=num[i].nxt){
			if(num[i].val==x)return 1;
		}
		return 0;
	}
}mp;
inline void read(int &x){
	x=0;char tmp=getchar();int fl=1;
	while(tmp<'0'||tmp>'9'){if(tmp=='-')fl=-fl;tmp=getchar();}
	while(tmp>='0'&&tmp<='9') x=(x<<1)+(x<<3)+tmp-'0',tmp=getchar();
	x=x*fl;
}
inline bool check(int x){
	for(int i=1;i<=la;i++){
		if(x-a[i]<0) break;
		if(mp.find(x-a[i]))return 1;
	}
	return 0;
}
int main(){
//	freopen("game.in","r",stdin);
//	freopen("game.out","w",stdout);
	cin>>la>>lb>>lc;
	for(int i=1;i<=la;i++) read(a[i]);
	for(int i=1;i<=lb;i++) read(b[i]);
	for(int i=1;i<=lc;i++) read(c[i]);
	if(la>lb) swap(a,b),swap(la,lb);
	if(la>lc) swap(a,c),swap(la,lc);
	for(int i=1;i<=lb;i++)
		for(int j=1;j<=lc;j++)
			if(!mp.find(b[i]+c[j]))
			mp.add(b[i]+c[j]);
	sort(a+1,a+la+1);
	cin>>ld;
	while(ld--){
		int x;read(x);
		if(check(x)) printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值