# 1078. Hashing (25)

100 ms

65536 kB

16000 B

Standard

CHEN, Yue

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 -

```#include <iostream>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>

using namespace std;
const int maxn=1e5;
int msize,n;
int tag[maxn+5];
int prime[maxn+5];
bool check[maxn+5];
int num[maxn+5];
int tot;
void eular()
{
memset(check,false,sizeof(check));
tot=0;
for(int i=2;i<=maxn+5;i++)
{
if(!check[i]) prime[tot++]=i;
for(int j=0;j<tot;j++)
{
if(i*prime[j]>maxn+5) break;
check[i*prime[j]]=true;

}
}
}
int main()
{
eular();
scanf("%d%d",&msize,&n);
for(int i=1;i<=n;i++)
scanf("%d",&num[i]);
for(int i=0;i<tot;i++)
{
if(prime[i]>=msize)
{
msize=prime[i];
break;
}
}
memset(tag,0,sizeof(tag));
for(int i=1;i<=n;i++)
{
int flag=0;
for(int j=0;j<msize;j++)
{
int h=(num[i]+j*j)%msize;
if(!tag[h])
{
tag[h]=1;
printf("%d",h);
flag=1;break;
}
}
if(!flag)
printf("-");
if(i!=n)
printf(" ");
else
printf("\n");
}
return 0;

}```

0 条评论

• ### ZOJ 3661 Palindromic Substring(回文树)

Palindromic Substring ---- Time Limit: 10 Seconds      Memory Limit: 65536 KB --...

• ### PAT 1018 Public Bike Management（Dijkstra 最短路）

1018. Public Bike Management (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16...

• ### ZOJ 3212 K-Nice

K-Nice ---- Time Limit: 1 Second      Memory Limit: 32768 KB      Special Judge ...

• ### ZOJ 3661 Palindromic Substring(回文树)

Palindromic Substring ---- Time Limit: 10 Seconds      Memory Limit: 65536 KB --...

• ### POJ 3581 Prime Gap（二分）

I once faced with a problem. I wrote a piece of code related with notifcation. E...

• ### Code Forces 652C Foe Pairs

C. Foe Pairs time limit per test 1 second memory limit per test 256 megaby...

• ### CodeForces 665A Buses Between Cities

A. Buses Between Cities time limit per test 1 second memory limit per test ...