题意:在5个集合中,一组一个,找出使之和为0的情况是否存在?
思路:很明显的hash,然后我用字典树写了一发WA了。。什么鬼,改成线性查找TLE了,然后改用VC++编译器交RE。。这才发现是数组开小了,把测试组数N的范围当成n的范围,666。。。无语了。。
#include <bits/stdc++.h>
using namespace std;
long long a[6][255];
int n;
struct node
{
int next[11],v;
void init()
{
memset(next, -1, sizeof(next));
}
};
node L[4000005];
int tot = 0;
void add(long long a)
{
int now = 0;
while(a)
{
long long tmp;
if(a<0){
a*=-1;
tmp=10;
}
else {tmp = a%10;a/=10;}
int next = L[now].next[tmp];
if(next == -1)
{
next = ++tot;
L[next].v = -1;
L[next].init();
L[now].next[tmp] = next;
}
now = next;
}
L[now].v=0;
}
int query(long long a)
{
int now = 0;
while(a)
{
long long tmp;
if(a<0){
a*=-1;
tmp=10;
}
else {tmp = a%10;a/=10;}
int next = L[now].next[tmp];
if(next == -1)return -1;
now = next;
}
return L[now].v;
}
int main()
{
int T;scanf("%d",&T);
while(T--)
{
tot=0;L[0].init();
scanf("%d",&n);
for(int i=0;i<5;++i)
{
for(int j=0;j<n;++j)
{
scanf("%I64d",&a[i][j]);
}
}
long long x;bool f=false,flag=false;
for(int i=0;i<n;++i)
{
for(int j=0;j<n;++j)
{
x=a[0][i]+a[1][j];
if(x!=0)add(x);else flag=true;
}
}
for(int i=0;i<n;++i)
{
for(int j=0;j<n;++j)
{
for(int p=0;p<n;++p)
{
x=a[2][i]+a[3][j]+a[4][p];
if(x==0&&flag){f=true;break;}
if(query(-x)==0&&x!=0){f=true;break;}
}
if(f)break;
}
if(f)break;
}
if(f)puts("Yes");
else puts("No");
}
}