HDOJ 题目4334 Trouble(hash)

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?
 

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代码
152186302015-10-23 21:48:21Accepted43341856MS6272K1738 BC++Who_you?

#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");
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值