Description
用至少两个不同的Hash函数实现hashing,并比较冲突情况
Input
Each sampled input consists of two lines. In the first line, there are n (<100, 000)denoting the number of elements, and m (<100, 000)denoting the size of the hash table.
In the second line, there are n elements, seperated by spaces.
Output
For each of the input, you should use two kinds of hash function to output the number of collisions. The hash functions are:
1. The division method: h(k) = k mod m
2. The multiplication method: h(k) = floor( m ( kA mod 1)). A is golden ratio, which can be estimated as 0.6180339887
Sample Input
Sample Output
HINT
#include <iostream>
#include <cmath>
using namespace std;
#define maxlen 1000100
#define A 0.6180339887
int a[maxlen];
int b[maxlen];
int main()
{
int n,m;
while (cin >> n)
{
cin >> m;
int counta = 0, countb = 0;
int i;
for (i = 0; i < maxlen; i++)
{
a[i] = -1;
b[i] = -1;
}
int temp;
for (i = 0; i < n; i++)
{
cin >> temp;
int tempa = temp%m;
if (a[tempa] == -1)
a[tempa] = temp;
else
counta++;
int tempb = floor(m*(temp*A -(int)(temp*A)));
if (b[tempb] == -1)
b[tempb] = temp;
else
countb++;
}
cout << counta << endl<< countb << endl;
}
return 0;
}
/**************************************************************
Problem: ****
User: Avivadepp
Language: C++
Result: Accepted
Time:32 ms
Memory:9096 kb
****************************************************************/