2012 Multi-University Training Contest 4 D - Trouble
hash
1 # include <stdio.h> 2 # include <string.h> 3 4 # define N 205 5 # define M 100007 6 7 typedef long long int LL; 8 9 int n; 10 LL a[6][N]; 11 int head[M], next[N*N+N]; 12 13 int hash(LL x) 14 { 15 return ((x%M)+M)%M; 16 } 17 18 void init_hash_table(void) 19 { 20 memset(head, -1, sizeof(head)); 21 } 22 23 void insert_hash_table(LL num, int i) 24 { 25 int hkey = hash(num); 26 /* 27 int u = head[hkey]; 28 while (~u) 29 { 30 if (a[u/n][4]+a[u%n][5]==num) return ; 31 u = next[u]; 32 }*/ 33 next[i] = head[hkey]; 34 head[hkey] = i; 35 } 36 37 int lookup_hash_table(LL num) 38 { 39 int hkey = hash(num); 40 int u = head[hkey]; 41 while (~u) 42 { 43 if (a[4][u/n]+a[5][u%n] == num) return 1; 44 u = next[u]; 45 } 46 return 0; 47 } 48 49 int solve(void) 50 { 51 int i, j, k, nn = 0; 52 init_hash_table(); 53 for (i = 0; i < n; ++i) 54 for (j = 0; j < n; ++j) 55 { 56 insert_hash_table(a[4][i]+a[5][j], i*n+j) ; 57 } 58 for (i = 0; i < n; ++i) 59 for (j = 0; j < n; ++j) 60 for (k = 0; k < n; ++k) 61 { 62 if (lookup_hash_table(-a[1][i]-a[2][j]-a[3][k])) 63 return 1; 64 } 65 return 0; 66 } 67 68 int main() 69 { 70 freopen("in.txt", "r", stdin); 71 72 int T, i, j, k; 73 74 scanf("%d", &T); 75 for (i = 1; i <= T; ++i) 76 { 77 scanf("%d", &n); 78 for (j = 1; j <= 5; ++j) 79 for (k = 0; k < n; ++k) 80 scanf("%I64d", &a[j][k]); 81 if (solve()) 82 puts("Yes"); 83 else 84 puts("No"); 85 } 86 87 return 0; 88 }