Quadratic probing (with positive increments only) is used to solve the collisions.
(哈希表)用二次方探测法来解决矛盾(此题只需要考虑正向);
二次方探测法证明,只需要算到步长为止,如果超过步长还是冲突则之后也是一样
摘自《算法笔记》
主要是题目没读懂,读懂了这道题难度一般
#include <cstdio>
#include <cmath>
bool isPrime(int n)
{
if(n<=1)return false;
for(int i = 2;i<=sqrt(n);i++)
{
if(n%i==0)return false;
}
return true;
}
int main()
{
int m,n;
int num[10010];
bool H[10010]={0};
scanf("%d%d",&m,&n);
int tm = m;
while(isPrime(tm)==false)
{
tm++;
}
for(int i = 0;i<n;i++)
{
scanf("%d",&num[i]);
int temp = num[i] %tm;
if(H[temp] == false)
{
H[temp] = true;
printf("%d",temp);
}
else if(H[temp] == true)
{
int temp1;
int flag = 0;
for(int j = 1;j<=tm;j++)
{
temp1 = (num[i] + j*j)%tm;
if(H[temp1] == false)
{
H[temp1] = true;
printf("%d",temp1);
flag = 1;
break;
}
}
if(flag == 0)
printf("-");
// temp++;
}
if(i<n-1)
printf(" ");
}
// printf("%d",n);
return 0;
}