Trouble&&http://acm.hdu.edu.cn/showproblem.php?pid=4334
2012-08-03 14:44
519 查看
[align=left]Problem Description[/align]
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?
[align=left]Input[/align]
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.
[align=left]Output[/align]
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".
[align=left]Sample Input[/align]
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
[align=left]Sample Output[/align]
No
Yes题意:给你5行数,问你是不是存在这一组数,在每行取一个,使得所求的和为0,思路:如果直接暴力,五成for循环肯定会挂掉,,这里可以用hash表优化成三层for循环,,AC代码:
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?
[align=left]Input[/align]
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.
[align=left]Output[/align]
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".
[align=left]Sample Input[/align]
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
[align=left]Sample Output[/align]
No
Yes题意:给你5行数,问你是不是存在这一组数,在每行取一个,使得所求的和为0,思路:如果直接暴力,五成for循环肯定会挂掉,,这里可以用hash表优化成三层for循环,,AC代码:
#include<iostream> #include<string.h> #include<string> #define N 100005 using namespace std; __int64 aa[5][205]; __int64 Hash ; bool flag ; int _Hash(__int64 num) { int t=num%N; if(t<0) t+=N; while(flag[t]&&Hash[t]!=num) t=(t+1)%N; return t; } int main() { int T; scanf("%d",&T); while(T--) { memset(flag,false,sizeof(flag)); int n; scanf("%d",&n); for(int i=0;i<5;i++){ for(int j=0;j<n;j++) scanf("%I64d",&aa[i][j]); } memset(flag,0,sizeof(flag)); for(int i=0;i<n;i++) for(int j=0;j<n;j++){ __int64 num=-(aa[0][i]+aa[1][j]); int pos=_Hash(num); Hash[pos]=num; flag[pos]=true; } bool fflag=false; for(int i=0;i<n;++i) for(int j=0;j<n;++j) for(int k=0;k<n;++k) { __int64 num=(aa[2][i]+aa[3][j]+aa[4][k]); int pos=_Hash(num); if(flag[pos]) {fflag=true;i=j=n;break;} } if(fflag) printf("Yes\n"); else printf("No\n"); }return 0; }
相关文章推荐
- find the safest road&&http://acm.hdu.edu.cn/showproblem.php?pid=1596
- http://acm.hdu.edu.cn/showproblem.php?pid=1020&&encoding
- http://acm.hdu.edu.cn/webcontest/contest_showproblem.php?pid=1004&ojid=1&cid=6866&hide=1&problem=Pro
- Robberies&&http://acm.hdu.edu.cn/showproblem.php?pid=2955
- Number Sequence&&http://acm.hdu.edu.cn/showproblem.php?pid=1005
- A + B Problem II &&http://acm.hdu.edu.cn/showproblem.php?pid=1002
- The 36th ACM/ICPC Asia Regional Beijing Site&&http://acm.hdu.edu.cn/showproblem.php?pid=4046
- 二分查找求函数的区间最小值&&http://acm.hdu.edu.cn/showproblem.php?pid=2899
- Text Reverse&&http://acm.hdu.edu.cn/showproblem.php?pid=1062
- Happy 2004&&http://acm.hdu.edu.cn/showproblem.php?pid=1452
- Buy the Ticket&&http://acm.hdu.edu.cn/showproblem.php?pid=1133
- 导弹拦截二&&http://acm.hdu.edu.cn/showproblem.php?pid=1257
- 最小路径覆盖&&http://acm.hdu.edu.cn/showproblem.php?pid=4160
- http://acm.hdu.edu.cn/webcontest/contest_showproblem.php?cid=963&pid=1019&ojid=1
- Max Sum &&http://acm.hdu.edu.cn/showproblem.php?pid=1003
- http://acm.hdu.edu.cn/showproblem.php?pid=1055&&Color a Tree
- A C&&http://acm.hdu.edu.cn/showproblem.php?pid=1570
- HDU Today&&http://acm.hdu.edu.cn/showproblem.php?pid=2112
- Substrings &&http://acm.hdu.edu.cn/showproblem.php?pid=1238
- Red and Black&&http://acm.hdu.edu.cn/showproblem.php?pid=1312