1078 Hashing (25 分)
 

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 ( 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 (≤) and N (≤) 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 -

题意:

输入msize和N,如果msize不是素数的话就把msize变为比当前值大的最小素数,采用平方探测方法,求插入哈希表后元素的序号

题解:

平方探测方法

二次探测——


    
  其中,h是哈希寻址函数,key是要存储的值,M是哈希表的大小,一般使用素数可以达到一个较高的效率。

AC代码 :

#include<bits/stdc++.h>
using namespace std;
int m,n;
int a[];
bool prime(int x){
if(x<=) return false;
for(int i=;i*i<=x;i++){
if(x%i==) return false;
}
return true;
}
int main(){
cin>>m>>n;
for(int i=;i<;i++) a[i]=-;
while(!prime(m)) m++;
int x;
for(int i=;i<=n;i++){
cin>>x;
int f=;
for(int j=;j<m;j++){
int y=(x+j*j)%m;//平方探测
if(a[y]==- || a[y]==x){
cout<<y;
a[y]=x;
f=;
break;
}
}
if(!f) cout<<"-";
if(i!=n) cout<<" ";
}
return ;
}

PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)的更多相关文章

  1. pat 甲级 1078. Hashing (25)

    1078. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of t ...

  2. PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)

    1070 Mooncake (25 分)   Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...

  3. PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)

    1032 Sharing (25 分)   To store English words, one method is to use linked lists and store a word let ...

  4. PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*

    1029 Median (25 分)   Given an increasing sequence S of N integers, the median is the number at the m ...

  5. 【PAT甲级】1078 Hashing (25 分)(哈希表二次探测法)

    题意: 输入两个正整数M和N(M<=10000,N<=M)表示哈希表的最大长度和插入的元素个数.如果M不是一个素数,把它变成大于M的最小素数,接着输入N个元素,输出它们在哈希表中的位置(从 ...

  6. PAT 1145 1078| hashing哈希表 平方探测法

    pat 1145: 参考链接 Quadratic probing (with positive increments only) is used to solve the collisions.:平方 ...

  7. PAT甲级1078 Hashing【hash】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805389634158592 题意: 给定哈希表的大小和n个数,使用 ...

  8. PAT 甲级 1078 Hashing

    https://pintia.cn/problem-sets/994805342720868352/problems/994805389634158592 The task of this probl ...

  9. PAT Advanced 1078 Hashing (25) [Hash ⼆次⽅探查法]

    题目 The task of this problem is simple: insert a sequence of distinct positive integers into a hash t ...

随机推荐

  1. linux下安装cryptography兼论查找合适pip的whl文件技巧

    cryptography这个包,如果源码安装,需要GCC之类的编译,在生产环境不太现实. 所以选择了whl文件安装. 但在官方提供的whl文件里,没有我们熟悉的cp36-cp36m这样的命名文件,肿么 ...

  2. python笔记35-装饰器

    前言 python装饰器本质上就是一个函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外的功能,装饰器的返回值也是一个函数对象. 很多python初学者学到面向对象类和方法是一道大坎,那么p ...

  3. test20190901 NOI2019 模拟赛

    0+0+0=0.还是太菜,看不出题型. Masodik 你要从 (0,0) 点走到 (n,m),每次只能往 x 轴或者 y 轴正方向移动一个单位距离.从 (i,j) 移动到 (i,j+1) 的代价为 ...

  4. wordpress调用自定义菜单

    wordpress要调用自定义菜单首先要注册菜单,将代码添加到主题文件夹下的function.php中,比如wordpress自带主题2019的定义如下 // This theme uses wp_n ...

  5. Tips on Blind Source Separation

    盲源分离是指在不知道源信号和信道传输参数的情况下,根据输入信号的统计特性,仅由观测信号恢复出源信号各个独立成分的过程.盲源分离研究的信号模型主要有三种:线性混合模型.卷积混合模型和非线性混合模型. 1 ...

  6. LG1036

    当我们看到这道题的时候,我们不仅大吼一声,这不就是搜索嘛. 于是搜索两大刀!搜索目标和搜索状态! 搜索目标:求选数的方案,以及他们的和是否为质数. 搜索状态: 1.从后往前分析目标(或从前往后):和是 ...

  7. 持续集成学习5 jenkins自动化测试与构建

    一.jenkins参数 1.主要参数类型 2.触发构建参数 3.参数值的使用 4.给git仓库配置参数,让其构建的时候可以选择分支 5.配置password参数 6.添加Choice参数 7.其它好用 ...

  8. BZOJ 4771: 七彩树 可持久化线段树+树链的并

    这个思路挺有意思的 ~ 利用树链的并来保证每个颜色只贡献一次,然后用可持久化线段树维护 code: #include <set> #include <cstdio> #incl ...

  9. video.js学习笔记

    video.js学习笔记获取用户观看时长

  10. 洛谷 P4316绿豆蛙的归宿

    题目描述 记f[i]表示经过i号点的概率. 那么点v从点u到达的概率=经过点u的概率/点u的出度.由于v可以由多个点走到,所以f[v]+=f[u]/out[u]. 计算f的过程可以在拓扑中完成,同时可 ...