A little faster than a linked version of the list.
1#include <cstdio>2#include <iostream>3#include <algorithm>4#include <cstring>5#include <cmath>6 #defineREP (S, n) for (int i = s; i <= n; i + +)7 using namespacestd;8 Const intMAXN = -+Ten;9 Const intHash_max =5677913;Ten Const intHash_check =2787907; One Const intHash_delta =2890003; A Const intHash_find =40009; - Const intINF =1000000001; - intHash[hash_max]; the BOOLVis[hash_max]; - intHASH_SORTN (intv) { -V%=Hash_max; - if(V <0) v + =Hash_max; + returnv; - } + intHash_checkn (intv) { A if(V &1) {v = v% Hash_check;if(V <0) v + =Hash_check;} at Else{v = v% Hash_check;if(V <0) v + = Hash_check; V + =Hash_delta;} - returnv; - } - intHASH_FINDN (intv) { -v = (v * hash_find)%Hash_max; - if(V <0) v + =Hash_max; in returnv; - } to intFind_insert (intv) { + intids =Hash_sortn (v); - if(!vis[ids]) {//consistency Sniffing theHash[ids] =v; *Vis[ids] =true; $ returnIDs;Panax Notoginseng } - Else if(v = = Hash[ids])returninf; the intID =Hash_checkn (v); + if(!vis[id]) {//Two-time parity sniffing AHash[id] =v; theVis[id] =true; + returnID; - } $ for(inti = ID;; i + = HASH_FINDN (i) + IDs * i) {//Complete Probing $I%=Hash_max; - if(I <0) i + =Hash_max; - if(!vis[i]) {//Two-time parity sniffing theHash[i] =v; -Vis[i] =true;Wuyi returni; the } - Else if(v = = Hash[i])returninf; Wu } - } About BOOLFindintv) { $ intids =Hash_sortn (v); - if(!vis[ids])return false; - if(Hash[ids] = = v)return true; - intID =Hash_checkn (v); A if(!vis[id])return false; + if(Hash[id] = = v)return true; the for(inti = ID;; i + = HASH_FINDN (i) + IDs * i) {//Complete Probing -I%=Hash_max; $ if(I <0) i + =Hash_max; the if(!vis[i])return false; the Else if(v = = Hash[i])return true; the } the } - intB[MAXN], C[MAXN], D[MAXN], N; in BOOLCheckintv) { the for(inti = n; I I--){ the inttemp = v-B[i]; About for(intj = N; J J--){ the if(Find (Temp-c[j]))return true; the } the } + return false; - } the voidReadint&x) {Bayix =0;intsig =1;CharCH =GetChar (); the while(!isdigit (CH)) {if(ch = ='-') sig =-1; CH =GetChar ();} the while(IsDigit (ch)) x =Ten* x + CH-'0', ch =GetChar (); -X *= sig;return ; - } the voidinit () { the read (n); the inttemp; theREP (1, N) Read (temp), Find_insert (temp); -REP (1, N) read (b[i]); theREP (1, N) read (c[i]); theREP (1, N) read (d[i]); theSort (d +1, D +1+n);94Sort (c +1, C +1+n); theSort (b +1, B +1+n); the return ; the }98 intAns =2000000000; About voidWork () { - for(inti = n; I I--){101 if(check (D[i])) {ans = d[i];return ; } 102 }103 return ;104 } the voidprint () {106 if(ans = =2000000000) puts ("No Answer.");107 Elseprintf"%d\n", ans);108 return ;109 } the intMain () {111 init (); the Work ();113 print (); the return 0; the}
Fast Hash Table