It's almost the first time to do hash.AlgorithmToday, we use the map in STL and the link address written by ourselves to avoid conflicting hash arrays as poj 1840, surprisingly, the efficiency of the two methods is actually less than 1300 ms (lT = 5000 ms). I don't know whether the efficiency of my own hash function is good or the STL is too slow.
The followingCodeIs a self-written hash function:
# Include <iostream> <br/> using namespace STD; <br/> struct node <br/> {<br/> long num; <br/> int count; <br/> node * Next; <br/>}; <br/> const int P = 19997; <br/> node * hash [20000]; <br/> int ans = 0; <br/> void insert (long Val) <br/>{< br/> int T; <br/> If (Val> 0) <br/> T = Val % P; <br/> else <br/> T = (0-val) % P; <br/> node * n = hash [T]; <br/> while (n! = NULL) <br/>{< br/> If (n-> num = Val) <br/>{< br/> N-> count ++; <br/> break; <br/>}< br/> N = N-> next; <br/>}< br/> If (n = NULL) <br/>{< br/> N = new node; <br/> N-> COUNT = 1; <br/> N-> num = val; <br/> N-> next = hash [T]; <br/> hash [T] = N; <br/>}< br/> int find (long Val) <br/>{< br/> int T; <br/> If (Val> 0) <br/> T = Val % P; <br/> else <br/> T = (0-val) % P; <br/> node * n = hash [T]; <br/> while (n! = NULL) <br/>{< br/> If (n-> num = 0-val) <br/> return N-> count; <br/> N = N-> next; <br/>}< br/> return 0; <br/>}< br/> void left (int, int B) <br/>{< br/> long Val; <br/> int I = 0; <br/> Int J = 0; <br/> for (I =-50; I <= 50; I ++) <br/> {<br/> if (I = 0) <br/> continue; <br/> for (j =-50; j <= 50; j ++) <br/>{< br/> If (j = 0) <br/> continue; <br/> val = A * I * I + B * j; <br/> insert (VAL ); <br/>}< br/> void right (int c, int D, int e) <br/>{< br/> long Val; <br/> int I = 0; <br/> Int J = 0; <br/> int K = 0; <br/> for (I =-50; I <= 50; I ++) <br/> {<br/> if (I = 0) <br/> continue; <br/> for (j =-50; j <= 50; j ++) <br/>{< br/> If (j = 0) <br/> continue; <br/> for (k =-50; k <= 50; k ++) <br/>{< br/> If (k = 0) <br/> continue; <br/> val = C * I * I + D * j + E * K; <br/> ans + = find (VAL ); <br/>}< br/> void solve (int A, int B, int C, int D, int e) <br/>{< br/> left (a, B); <br/> right (c, d, e ); <br/>}< br/> int main () <br/>{< br/> int A, B, C, D, E; <br/> int I = 0; <br/> CIN> A; <br/> CIN> B; <br/> CIN> C; <br/> CIN> D; <br/> CIN> E; <br/> for (I = 0; I <20000; I ++) <br/>{< br/> hash [I] = NULL; <br/>}< br/> solve (A, B, C, D, E ); <br/> cout <ans <Endl; <br/>}