poj 2785 4 Values whose Sum is 0
http://poj.org/problem?id=2785
简单hash,不过不能用set,而且数组不能开太大。
才几十行的代码,当时个人赛居然不会... - -
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdlib> 2 #include <cstring> 3 #include <cstdio> 4 5 using namespace std; 6 7 const int maxn = 4001; 8 const int mod = 15999997; 9 int hash[maxn * maxn], cnt[maxn * maxn]; 10 int rec[maxn][4]; 11 12 void insert(int n){ 13 int p = (n << 2) % mod; 14 15 if (p < 0) p += mod; 16 while (cnt[p] && hash[p] != n) p++; 17 hash[p] = n; 18 cnt[p]++; 19 } 20 21 int count(int n){ 22 int p = (n << 2) % mod; 23 24 if (p < 0) p += mod; 25 while (cnt[p] && hash[p] != n) p++; 26 return cnt[p]; 27 } 28 29 int deal(int n){ 30 memset(cnt, 0, sizeof(cnt)); 31 for (int i = 0; i < n; i++){ 32 for (int j = 0; j < n; j++){ 33 insert(rec[i][0] + rec[j][1]); 34 } 35 } 36 int ret = 0; 37 38 for (int i = 0; i < n; i++){ 39 for (int j = 0; j < n; j++){ 40 ret += count(- rec[i][2] - rec[j][3]); 41 } 42 } 43 44 return ret; 45 } 46 47 int main(){ 48 int n; 49 50 while (~scanf("%d", &n)){ 51 for (int i = 0; i < n; i++){ 52 for (int j = 0; j < 4; j++){ 53 scanf("%d", &rec[i][j]); 54 } 55 } 56 printf("%d\n", deal(n)); 57 } 58 59 return 0; 60 }
——written by Lyon