哈希素数
const ll bas[]={655717,1635947,2756749,5496013,15485863,21742157,31871237,40114259};
线性探测
namespace Hash {
const int hashmod = 1635947;
int v[hashmod];
ll h[hashmod];
bool vis[hashmod];
void init() {
memset(v, 0, sizeof v);
memset(vis, 0, sizeof vis);
}
int &get(unsigned long long S) {
int t2 = S%hashmod, i;
for (i = t2; vis[i]; i = (i + 1) % hashmod);
h[i] = S, vis[i] = 1;
return v[i];
}
};
链表
namespace Hash {
const int hashmod = 1635947;
int next[hashmod], v[hashmod], head[hashmod], cnt;
ll h[hashmod];
void init() {
memset(head,-1,sizeof head);
memset(v,0,sizeof v);
cnt = 0;
}
int &get(unsigned long long x) {
int y = x%hashmod;
for(int i=head[y]; i!=-1; i=next[i]) if(h[i]==x) return v[i];
h[cnt]=x,next[cnt]=head[y],head[y]=cnt++;
return v[cnt-1];
}
}
unordered_map 重写hash函数
#include <unordered_map>
struct Hash {
long long operator() (long long x) const {
x+=0x9e3779b97f4a7c15;
x=(x^(x>>30))*0xbf58476d1ce4e5b9;
x=(x^(x>>27))*0x94d049bb133111eb;
return x^(x>>31);
}
};
unordered_map<long long,int,Hash> s;