# PTA甲级 1078 Hashing (25分)-散列+素数

PS

## 题目原文

The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. The hash function is defined to be H(key)=key%TSize where TSize is the maximum size of the hash table. Quadratic probing (with positive increments only) is used to solve the collisions.

Note that the table size is better to be prime. If the maximum size given by the user is not prime, you must re-define the table size to be the smallest prime number which is larger than the size given by the user.

### Input Specification:

Each input file contains one test case. For each case, the first line contains two positive numbers: MSize (≤104) and N (≤MSize) which are the user-defined table size and the number of input numbers, respectively. Then N distinct positive integers are given in the next line. All the numbers in a line are separated by a space.

### Output Specification:

For each test case, print the corresponding positions (index starts from 0) of the input numbers in one line. All the numbers in a line are separated by a space, and there must be no extra space at the end of the line. In case it is impossible to insert the number, print “-” instead.

### Sample Input:

4 4
10 6 4 15


### Sample Output:

0 1 4 -


## 生词如下:

hash table 散列表

probing 探查

distinct 有区别的

Quadratic probing (with positive increments only) is used to solve the collisions.

## 思路:

① 直接套用素数函数-这里注意1不是素数.

② 要看得懂题目,题目都讲了要用二次探测法.

## 代码如下:

#include<iostream>
#include<vector>
using namespace std;
//判断是否是素数的函数
bool isPrime(int);
int main(void) {
int M, N;
cin >> M >> N;
while (!isPrime(M)) { ++M; }
//cout << M;
vector<bool> Data(M, false);
bool first=false;
for (int i = 0; i < N; ++i) {
int origan = 0,t=0;
cin >> origan;
t = origan % M;

if (!Data[t]) {
Data[t] = true;
if (!first)	first = true;
else  cout << " ";
cout << t;
}
else {
bool Isput = false;
//关键的二次探测法的代码.
for (int i = 1; i <=M-1; ++i) {
t = (origan + i * i) % M;
if (!Data[t]) {
Data[t] = true;
cout << " "<<t;
Isput = true;
break;
}
}
if (!Isput)	cout << " -";
}
}
return 0;
}
bool isPrime(int M) {
if (M <= 1)	return false;
for (int i = 2; i * i <= M; i++) {
if (M % i == 0)	return false;
}
return true;
}


#include <iostream>
using namespace std;
int Magnitude, n, hashTable[10100];
bool isprime(int num) {
if (num == 1) return false;
for (int i = 2; i * i <= num; i++)
if (num % i == 0) return false;
return true;
}
//插入函数,柳神还是柳神,从0开始就可以少些一段的代码.而且其实本质都是相同的.
//我写的麻烦了.
void insert(int key) {
for (int step = 0; step < Magnitude; step++) {
int index = (key + step * step) % Magnitude;
if (hashTable[index] == 0) {
hashTable[index] = 1;
cout << index % Magnitude;
return;
}
}
cout << '-';
}
int main() {
cin >> Magnitude >> n;
while (!isprime(Magnitude)) Magnitude++;
for (int i = 0; i < n; i++) {
int key;
cin >> key;
if (i != 0) cout << ' ';
insert(key);
}
return 0;
}


05-16 127
10-03 794

04-14 71
05-20 317
08-23 91
05-21 760
02-16 108
05-19 1082