1145 Hashing - Average Search Ti
2018-08-05 本文已影响0人
zjh3029
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;
bool isprime(int inp)
{
if (inp <= 0) return false;
for (int i = 2; i <=inp/2; i++)
{
if (inp%i==0)
{
return false;
}
}
return true;
}
int main()
{
int Msize, N, M;
cin >> Msize >> N >> M;
while(isprime(Msize) == false) Msize++;
//cout << Msize << endl;
vector<int> v(Msize);
fill(v.begin(), v.end(), -1);
int a,b;
bool flag = false;
for (int i = 0; i < N; i++)
{
cin >> a;
for (int j = 0; j < Msize; j++)
{
b=(a + j * j) % Msize;
if (v[b] == -1)
{
v[b] = a;
flag = true;
break;
}
}
if (flag==false)
{
cout << a << " cannot be inserted." << endl;
}
flag = false;
}
double ans = 0.0;
for (int i = 0; i < M; i++)
{
cin >> a;
int cnt = 1;
for (int j = 0; j < Msize; j++)
{
b = (a + j * j) % Msize;
if (v[b] == a||v[b]==-1) break;
cnt++;
}
ans += cnt;
}
printf("%.1lf\n", ans / M);
system("pause");
return 0;
}