注意一下正向平方探查法的部分
t=(Key+j*j)%Msize
其中0<=j<=Msize
#include<iostream>
#include<cmath>
using namespace std;
bool is_prime(int x){
if(x==2) return true;
int k=(int)sqrt(x*1.0);
for(int i=2;i<=k;i++){
if(x%i==0) return false;
}
return true;
}
int Msize,N,M;
int ct=0;
int main(){
cin>>Msize>>N>>M;
while(!is_prime(Msize)) Msize++;
int hash[Msize]={0};
for(int i=0;i<N;i++){
int index;
int flag=false;
scanf("%d",&index);
for(int j=0;j<=Msize;j++){
int t=(index+j*j)%Msize;
if(hash[t]==0){
hash[t]=index;
flag=true;
break;
}
}
if(!flag) printf("%d cannot be inserted.\n",index);
}
for(int i=0;i<M;i++){
int index;
scanf("%d",&index);
for(int j=0;j<=Msize;j++){
ct++;
int t=(index+j*j)%Msize;
if(hash[t]==index||hash[t]==0){
break;
}
}
}
printf("%.1f\n",ct*1.0/M);
return 0;
}