Trouble
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5528 Accepted Submission(s): 1537
Problem Description
Hassan is in trouble. His mathematics teacher has given him a very difficult problem called 5-sum. Please help him.
The 5-sum problem is defined as follows: Given 5 sets S_1,...,S_5 of n integer numbers each, is there a_1 in S_1,...,a_5 in S_5 such that a_1+...+a_5=0?
The 5-sum problem is defined as follows: Given 5 sets S_1,...,S_5 of n integer numbers each, is there a_1 in S_1,...,a_5 in S_5 such that a_1+...+a_5=0?
Input
First line of input contains a single integer N (1≤N≤50). N test-cases follow. First line of each test-case contains a single integer n (1<=n<=200). 5 lines follow each containing n integer numbers in range [-10^15, 1 0^15]. I-th line denotes set S_i for 1<=i<=5.
Output
For each test-case output "Yes" (without quotes) if there are a_1 in S_1,...,a_5 in S_5 such that a_1+...+a_5=0, otherwise output "No".
Sample Input
2 2 1 -1 1 -1 1 -1 1 -1 1 -1 3 1 2 3 -1 -2 -3 4 5 6 -1 3 2 -4 -10 -1
Sample Output
No Yes
Source
Recommend
zhoujiaqi2010 | We have carefully selected several similar problems for you:
4331
4332
4333
4335
4336
一共5组,每组n个数,每组里边取一个数使a1+a2+a3+a4+a5=0;问可以不可以
ac代码
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define ll long long
#define mod 1000003ll
using namespace std;
ll a[6][205];
struct s
{
ll v;
int next;
}edge[40005];
int head[1000005],cnt;
void add(ll key)
{
ll rkey;
if(key>0)
rkey=key%mod;
else
rkey=(-key)%mod;
for(int i=head[rkey];i!=-1;i=edge[i].next)
{
ll v=edge[i].v;
if(v==key)
return;
}
edge[cnt].v=key;
edge[cnt].next=head[rkey];
head[rkey]=cnt++;
}
int jud(ll key)
{
ll rkey;
if(key>0)
rkey=key%mod;
else
rkey=(-key)%mod;
for(int i=head[rkey];i!=-1;i=edge[i].next)
{
if(edge[i].v==key)
return 1;
}
return 0;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
cnt=0;
memset(head,-1,sizeof(head));
int i,j,n,k;
scanf("%d",&n);
for(i=1;i<=5;i++)
for(j=1;j<=n;j++)
scanf("%lld",&a[i][j]);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
add(a[1][i]+a[2][j]);
int flag=0;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
for(k=1;k<=n;k++)
{
if(jud(-(a[3][i]+a[4][j]+a[5][k])))
{
flag=1;
break;
}
}
if(flag)
break;
}
if(flag)
break;
}
if(flag)
printf("Yes\n");
else
printf("No\n");
}
}