标签:
hash结构体
struct Hash_map { static const int mask=0x7fffff; int p[8388608],q[8388608]; void clear(){ for(int i=0;i<=mask;++i) q[i]=0; } int & operator [] (int k){ int i; for(i=k&mask; q[i]&&p[i]!=k; i=(i+1)&mask); p[i]=k; return q[i]; } }hash;
好像没A,概率论啊,烦死了。
#include<stdio.h> #include<iostream> #include<map> #include<vector> using namespace std; struct Hash_map { static const int mask=0x7fffff; int p[8388608],q[8388608]; void clear() { for(int i=0; i<=mask; ++i) q[i]=0; } int& operator [](int k) { int i; for(i=k&mask; q[i]&&p[i]!=k; i=(i+1)&mask); p[i]=k; return q[i]; } }hash; int main() { int n,kase = 0; cin>>n; while(n--) { if(kase) printf("\n"); vector<int> v1,v2,v3,v4,v5,v6; int m,ji = 0; cin>>m; while(m--) { int temp; cin>>temp; v1.push_back(temp); cin>>temp; v2.push_back(temp); cin>>temp; v3.push_back(temp); cin>>temp; v4.push_back(temp); } int len1 = v1.size(),len2 = v2.size(),len3 = v3.size(),len4 = v4.size(); hash.clear(); for(int i = 0;i < len1;i++) for(int j = 0;j < len2;j++) hash[v1[i] + v2[j]]++; for(int i = 0;i < len3;i++) for(int j = 0;j < len4;j++) ji += hash[-v3[i] -v4[j]]; printf("%d\n",ji); kase = 1; } return 0; }
UVa1152 - 4 Values whose Sum is 0(hash)
标签:
原文地址:http://blog.csdn.net/a197p/article/details/46575743