//下面的做法为直接链址法 #include <iostream> #include <memory.h> using namespace std; const int MAX = 18750001;//这个数的由来,由其系数最大值决定:50^4 = 18750000; short hash[MAX*2 + 10]; int main() { short a1, a2, a3, a4, a5, x1, x2, x3, x4, x5; int sum, ans = 0; cin >> a1 >> a2 >> a3 >> a4 >> a5; for (x1 = -50; x1 <= 50; x1++){ if (x1 == 0) continue; for (x2 = -50; x2 <= 50; x2++){ if (x2 == 0) continue; for (x3 = -50; x3 <= 50; x3++){ if (x3 == 0) continue; sum = a1*x1*x1*x1 + a2*x2*x2*x2 + a3*x3*x3*x3; sum += MAX; hash[sum]++; } } } for (x4 = -50; x4 <= 50; x4++){ if (x4 == 0) continue; for (x5 = -50; x5 <= 50; x5++){ if (x5 == 0) continue; sum = -(a4*x4*x4*x4 + a5*x5*x5*x5); sum += MAX; ans += hash[sum]; } } cout << ans << endl; system("pause"); } */ //下面的做法为除余取模法! /* #include <iostream> #include <memory.h> using namespace std; const int key = 1000007;//这个数的由来,由其系数的乘积最大值决定:3*50^4=18750000,由于有可能为负数, //要保证取余之后再相加的key值为正数,又因为要取素数,而又要增加其散列表的散列程度,所以就得出这个数了! //还有很多数符合要求的,这就要开情况而取数了(其数过大会增加其内存,太小其散列程度不好) int hash[key+1][5]; int cnt[key+1]; int search(int val) { int tmp = val % key, c = 0, i; if (tmp < 0) tmp += key; for (i = 0; i < cnt[tmp]; i++){ if (hash[tmp][i] == val) c++; } return c; } int main() { int a1, a2, a3, a4, a5, x1, x2, x3, x4, x5, sum, ans = 0, keyval; memset(hash, -1, sizeof(hash)); memset(cnt, 0, sizeof(cnt)); cin >> a1 >> a2 >> a3 >> a4 >> a5; for (x1 = -50; x1 <= 50; x1++){ if (x1 == 0) continue; for (x2 = -50; x2 <= 50; x2++){ if (x2 == 0) continue; sum = a1*x1*x1*x1 + a2*x2*x2*x2; keyval = sum%key; if (keyval < 0) keyval += key; hash[keyval][cnt[keyval]++] = sum; } } for (x3 = -50; x3 <= 50; x3++){ if (x3 == 0) continue; for (x4 = -50; x4 <= 50; x4++){ if (x4 == 0) continue; for (x5 = -50; x5 <= 50; x5++){ if (x5 == 0) continue; sum = -(a4*x4*x4*x4 + a5*x5*x5*x5 + a3*x3*x3*x3); ans += search(sum); } } } cout << ans << endl; system("pause"); } */ /* //下面的是链表防冲突法来做的,有点像字典树的写法!(其实字典树是一种特殊的哈希表) #include <iostream> using namespace std; struct Node { int cnt; long val; Node *next; }; const int key = 19997; struct Node *hash[20000]; int ans = 0; void insert(long val) { int keyval = val % key; if (keyval < 0) keyval = -keyval; Node *n = hash[keyval]; while (n != NULL){ if (n->val == val){ n->cnt++; break; } n = n->next; } if (n == NULL){ n = new Node; n->val = val; n->cnt = 1; n->next = hash[keyval]; hash[keyval] = n; } return ; } int search(long val) { int keyval = val % key; if (keyval < 0) keyval = -keyval; Node *n = hash[keyval]; while (n != NULL){ if (n->val == val) return n->cnt; n = n->next; } return 0; } //当这里为三重循环的时候,频繁的insert会造成超时!要注意! void left(int a1, int a2) { int x1, x2; long val; for (x1 = -50; x1 <= 50; x1++){ if (x1 == 0) continue; for (x2 = -50; x2 <= 50; x2++){ if (x2 == 0) continue; val = a1*x1*x1*x1 + a2*x2*x2*x2; insert(val); } } } void right(int a4, int a5, int a3) { int x4, x5, x3; long val; for (x4 = -50; x4 <= 50; x4++){ if (x4 == 0) continue; for (x5 = -50; x5 <= 50; x5++){ if (x5 == 0) continue; for (x3 = -50; x3 <= 50; x3++){ if (x3 == 0) continue; val = a4*x4*x4*x4 + a5*x5*x5*x5 + a3*x3*x3*x3; ans += search(val); } } } } void solve(int a1, int a2, int a3, int a4, int a5) { left(a1, a2); right(a4, a5, a3); return ; } int main() { int a1, a2, a3, a4, a5, i; cin >> a1 >> a2 >> a3 >> a4 >> a5; for (i = 0; i < 20000; i++) hash[i] = NULL; solve(a1, a2, a3, a4, a5); cout << ans << endl; system("pause"); } */