二次探测是0,1,4,9….
而二次探测再散列是0,1,-1,4,-4….
#include<iostream>
#include<cmath>
#include<vector>
using namespace std;
int main()
{
int M, N;
bool flag;
cin >> M >> N;
M = M > N ? M : N;
while (1)//把M变为大于等于M的最大素数
{
flag = true;
if (M <= 2) { M = 2;break; }
if (M % 2 == 0) { M++;continue; }
for (int t = 3;t <= sqrt(M);t += 2)
{
if (M % t == 0) {
flag = false;break;
}
}
if (!flag) { M += 2;continue; }
break;
}
vector<bool> is(M,false);
while (N--)
{
int temp,k;
cin >> temp;
for (k = 0;k < M;k++)
if (!is[(temp + k*k) % M])
{
is[(temp + k*k)%M] = true;
printf("%d", (temp + k*k) % M);
break;
}
if (k == M) printf("-");
if (N) printf(" ");
}
cout << endl;
}