poj 1840 Eqs
http://poj.org/problem?id=1840
简单hash。。。。很想知道那些200ms以内的是怎么搞的。。。
View Code
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 5 using namespace std; 6 7 const int mod = 4000037; 8 const int HASH = 0x20decade; 9 const int inf = 0x7fffffff; 10 11 int hash[mod]; 12 int used[mod] = {0}; 13 int tri[101]; 14 15 void pre() { 16 for (int i = -50; i <= 50; i++) { 17 tri[i + 50] = i * i * i; 18 } 19 } 20 21 void insert(int _x) { 22 int p = ((_x ^ HASH) & inf) % mod; 23 24 while (hash[p] != _x && used[p]) p = (p + 1) % mod; 25 hash[p] = _x; 26 used[p]++; 27 } 28 29 int find(int _x) { 30 int p = ((_x ^ HASH) & inf) % mod; 31 32 while (hash[p] != _x && used[p]) p = (p + 1) % mod; 33 34 return used[p]; 35 } 36 37 int main() { 38 int a[5]; 39 bool nz = false; 40 41 pre(); 42 for (int i = 0; i < 5; i++) { 43 scanf("%d", &a[i]); 44 if (a[i]) nz = true; 45 } 46 if (!nz) { 47 puts("10000000000"); 48 49 return 0; 50 } 51 for (int i = 0; i <= 100; i++) { 52 if (i == 50) continue; 53 int t1 = tri[i] * a[0]; 54 55 for (int j = 0; j <= 100; j++) { 56 if (j == 50) continue; 57 int t2 = tri[j] * a[1]; 58 59 for (int k = 0; k <= 100; k++) { 60 if (k == 50) continue; 61 insert(t1 + t2 + tri[k] * a[2]); 62 } 63 } 64 } 65 __int64 cnt = 0; 66 67 for (int i = 0; i <= 100; i++) { 68 if (i == 50) continue; 69 int t = tri[i] * a[3]; 70 71 for (int j = 0; j <= 100; j++) { 72 if (j == 50) continue; 73 cnt += find(- (t + tri[j] * a[4])); 74 } 75 } 76 77 printf("%I64d\n", cnt++); 78 79 return 0; 80 }
——written by Lyon