This is the determination of A + B + C + D + E = 0 with a large data volume. It can be done using a hash table or an ordered table to find a solution. Time Complexity: the former is close to O (N ^ 3), and the latter is O (n ^ 3 ).
The Code is as follows:
Hash table:
#include<iostream>#include<map>#define MOD 1000003LLusing namespace std;typedef long long LL;const int N = 205;LL a[6][N];int head[1000005], idx;struct Node{ LL v; int next; }e[40005];void add(LL key){ int rkey, flag = 0; if (key > 0) { rkey = key % MOD; } else { rkey = (-key) % MOD; } for (int i = head[rkey]; i != -1; i = e[i].next) { if (e[i].v == key) { flag = 1; break; } } if (!flag) { ++idx; e[idx].v = key; e[idx].next = head[rkey]; head[rkey] = idx; }}bool find(LL x){ int rkey; if (x > 0) { rkey = x % MOD; } else { rkey = (-x) % MOD; } for (int i = head[rkey]; i != -1; i = e[i].next) { if (e[i].v == x) { return true; } } return false;}int main(){ int t, n, flag; LL key; scanf("%d",&t); while(t-- && scanf("%d",&n)){ idx = -1; flag = 0; memset(head, 0xff, sizeof (head)); for(int i = 1; i <= 5; i++){ for(int j = 1; j <= n; j++){ scanf("%I64d",&a[i][j]); } } for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++){ key = a[1][i] + a[2][j]; add(key); } } for(int i = 1; i <= n && !flag; i++){ for(int j = 1; j <= n && !flag; j++){ for(int k = 1; k <= n; k++){ if (find(-(a[3][i]+a[4][j]+a[5][k]))) { flag = 1; break; } } } } puts( flag? "Yes" : "No"); } return 0;}
Sequential table search: put all the combinations of A + B in one table, and C + D in another table, all sorted and de-duplicated, and then define two pointers pointing to the first table respectively, the end of the second table. Then go to both sides.
#include <cstdlib>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long int Int64;int N, fidx, sidx;Int64 seq[5][205];Int64 flist[400005], slist[400005];int main(){ int T, ca = 0, flag, p1, p2; Int64 sum, obj; scanf("%d", &T); while (T--) { fidx = sidx = flag = 0; scanf("%d", &N); for (int i = 0; i < 5; ++i) { for (int j = 1; j <= N; ++j) { scanf("%I64d", &seq[i][j]); } } for (int i = 1; i <= N; ++i) { for (int j = 1; j <= N; ++j) { flist[++fidx] = seq[0][i] + seq[1][j]; slist[++sidx] = seq[2][i] + seq[3][j]; } } sort(flist+1, flist+1+fidx); sort(slist+1, slist+1+sidx); fidx = unique(flist+1, flist+1+fidx) - (flist+1); sidx = unique(slist+1, slist+1+sidx) - (slist+1); for (int i = 1; i <= N && !flag; ++i) { p1 = 1, p2 = sidx; obj = -seq[4][i]; while (p1 <= fidx && p2 >= 1) { sum = flist[p1] + slist[p2]; if (obj > sum) { ++p1; } else if (obj < sum) { --p2; } else { flag = 1; break; } } } printf(flag ? "Yes\n" : "No\n"); } return 0; }