#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
bool isprime(int a){
if(a==1){
return false;
}else if(a==2){
return true;
}else if(a>2){
for(int i=2;i<=(int)sqrt(a);i++){
if(a%i==0){
return false;
}
}
return true;
}
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("in.txt","r",stdin);
#endif
int ms,n,m;
cin>>ms>>n>>m;
while(!isprime(ms)){
ms++;
}
int hash[ms]={};
for(int i=0;i<n;i++){
int temp;
cin>>temp;
if(hash[temp%ms]==0) hash[temp%ms]=temp;
else{
int flag=0;
for(int j=0;j<ms;j++){
if(hash[(temp+j*j)%ms]==0) {
hash[(temp+j*j)%ms]=temp;flag=1;break;
}
}
if(flag==0) cout<<temp<<' '<<"cannot be inserted."<<endl;
}
}
int ans=0;
for(int i=0;i<m;i++){
int temo;
cin>>temo;
int cnt=1;
for(int j=0;j<ms;j++){
if(hash[(temo+j*j)%ms]==temo||hash[(temo+j*j)%ms]==0) {break;}
else cnt++;
}
ans+=cnt;
}
printf("%.1lf\n",(ans*1.0/m));
return 0;
}
1145 Hashing - Average Search Time
猜你喜欢
转载自blog.csdn.net/csg3140100993/article/details/81329088
今日推荐
周排行