[hdu P4334] Trouble

[hdu P4334] Trouble

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?

InputFirst 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.OutputFor 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

第一次手写Hash表。。。只不过跑的有点慢。。。

发现hash最常用的还是用邻接表挂链。。其实还蛮好写的,只是以前没写过而已。

code:

(第一次写class)

 1 %:pragma GCC optimize(2)
 2 #include<bits/stdc++.h>
 3 #define LL long long
 4 #define r register
 5 #define Ms(a,x) memset(a,x,sizeof a)
 6 using namespace std;
 7 const int N=205;
 8 int n; LL a[6][N],sum;
 9 class Hashmap {
10     private:
11         #define p 2040803
12         LL lnk[p+5],son[N*N]; int nxt[N*N],tot;
13     public:
14         void clear() {Ms(lnk,-1),Ms(nxt,-1),tot=0;}
15         void insert(LL x) {
16             LL k=x%p; if (k<0) k+=p;
17             if (lnk[k]==-1) {lnk[k]=++tot,son[tot]=x; return;}
18             for (r int j=lnk[k]; ; j=nxt[j])
19                 if (nxt[j]==-1) {nxt[j]=++tot,son[j]=x; return;}
20         }
21         bool find(LL x) {
22             LL k=x%p; if (k<0) k+=p;
23             for (r int j=lnk[k]; ~j; j=nxt[j]) if (son[j]==x) return 1;
24             return 0;
25         }
26 }app;
27 inline LL read() {
28     LL x=0,f=1; char ch=getchar();
29     while (ch<'0'||ch>'9') f=(ch=='-')?-1:1,ch=getchar();
30     while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
31     return x*f;
32 }
33 int main() {
34     for (int T=read(); T; T--) {
35         n=read(),app.clear();
36         for (int i=1; i<=5; i++)
37             for (int j=1; j<=n; j++) a[i][j]=read();
38         for (r int i=1; i<=n; i++)
39             for (r int j=1; j<=n; j++)
40                 sum=-(a[1][i]+a[2][j]),app.insert(sum);
41         bool tag=0;
42         for (r int i=1; i<=n&&!tag; i++)
43             for (r int j=1; j<=n&&!tag; j++)
44                 for (r int k=1; k<=n&&!tag; k++) {
45                     sum=a[3][i]+a[4][j]+a[5][k];
46                     if (app.find(sum)) tag=1;
47                 }
48         printf("%s\n",tag?"Yes":"No");
49     }
50     return 0;
51 }
View Code

转载于:https://www.cnblogs.com/whc200305/p/7731652.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值