题目https://pintia.cn/problem-sets/994805342720868352/problems/994805389634158592

题意:

给定哈希表的大小和n个数,使用平方探测法解决冲突,为每个数在哈希表中的位置。

如果给定的哈希表的大小不是质数,将其改为最小的比他大的质数。

思路:

比较基础的题目。有两个要注意的点!

1、初始化notprime数组时,需要注意1,也不是质数。特殊处理notprime[1] = true

2、注意考虑prob的边界,应该是哈希表的大小。

  因为对于$prob > msize$,设$prob = msize + i$, 则$prob^2 = msize^2 + 2{msize}{i}+i^2$

  根据同余$prob^2%msize = i^2%msize$

 #include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stack>
#include<queue> #define inf 0x7fffffff
using namespace std;
typedef long long LL;
typedef pair<string, string> pr; int msize, n;
const int maxn = 1e4 + ;
int notprime[maxn * ]; void init()
{
notprime[] = true;
int now = ;
while(now < 1e5){
if(!notprime[now]){
int tmp = now + now;
while(tmp < 1e5){
notprime[tmp] = true;
tmp += now;
}
}
now++;
}
} int h[maxn];
int pos[maxn]; int main()
{
init();
scanf("%d%d", &msize, &n);
while(notprime[msize]){
msize++;
}
for(int i = ; i < n; i++){
int v;
scanf("%d", &v);
int p = v % msize, prob = ;
while(h[(p + prob * prob) % msize] && prob < msize){
prob++;
}
if(h[(p + prob * prob) % msize]){
pos[i] = -;
}
else{
h[(p + prob * prob) % msize] = v;
pos[i] = (p + prob * prob) % msize;
}
} if(pos[] == -){
printf("-");
}
else{
printf("%d", pos[]);
}
for(int i = ; i < n; i++){
if(pos[i] == -){
printf(" -");
}
else{
printf(" %d", pos[i]);
}
}
printf("\n"); return ;
}

PAT甲级1078 Hashing【hash】的更多相关文章

  1. pat 甲级 1078. Hashing (25)

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

  2. PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)

    1078 Hashing (25 分)   The task of this problem is simple: insert a sequence of distinct positive int ...

  3. PAT 甲级 1078 Hashing

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

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

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

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

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

  6. PAT 甲级 1145 Hashing - Average Search Time (25 分)(读不懂题,也没听说过平方探测法解决哈希冲突。。。感觉题目也有点问题)

    1145 Hashing - Average Search Time (25 分)   The task of this problem is simple: insert a sequence of ...

  7. PAT 甲级 1145 Hashing - Average Search Time

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

  8. PAT甲级——A1078 Hashing

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

  9. PAT 甲级真题题解(63-120)

    2019/4/3 1063 Set Similarity n个序列分别先放进集合里去重.在询问的时候,遍历A集合中每个数,判断下该数在B集合中是否存在,统计存在个数(分子),分母就是两个集合大小减去分 ...

随机推荐

  1. Nginx 站点设置目录列表显示

    Nginx 站点目录列表显示. 可以编辑添加在 server { } 模块 或者 location { } 模块下. autoindex on; # 开启目录文件列表 autoindex_exact_ ...

  2. ADO五大对象

     ADO.NET五大对象, 1. Connection:与数据源建立连接. 2. Command:对数据源执行SQL命令并返回结果. 3. DataReader:读取数据源的数据,只允许对将数据源以只 ...

  3. 405 css样式的研究 list-style-type 属性研究

    CSS 列表的样式 list-style-type.list-style-position和list-style-image 属性 在CSS中,列表元素是一个块框,列表中的每个表项也是一个块框,只是在 ...

  4. 五子棋.html

    二维数组的定义 , canvas对象的使用 二维数组:以下用new,其实简化 [] 即可, var tArray = new Array(); //先声明一维 for(var k=0;k<i;k ...

  5. kerbose常用操作

    1.查看有那些用户认证 kadmin.local -q "list_principals" 2.用keytab文件进行认证 kinit -kt /root/keytab/hive. ...

  6. 2018-2019-1 20189208《Linux内核原理与分析》第八周作业

    学习笔记 1.ELF目标文件格式 编译器生成目标文件,目标文件与目标平台二进制兼容. ELF:可执行或可链接的格式,是目标文件格式标准. ELF类型: 可重定位文件:编译器汇编器创建的 .o 文件,最 ...

  7. 【转】Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历))

    Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历)) 本文由 Luzhuo 编写,转发请保留该信息. 原文: http://blog. ...

  8. AUTOCAD参数约束功能

    概要:http://through-the-interface.typepad.com/through_the_interface/2011/08/a-simplified-net-api-for-a ...

  9. 【原创】Linux基础之sudo

    sudo允许用户以其他用户的身份(比如root)执行命令,比如切换用户.执行命令.读写文件等: 配置 sudo配置在:/etc/sudoers ## Sudoers allows particular ...

  10. Java的家庭记账本程序(H) :微信小程序 image 标签,在模拟器中无法显示图片?(已解决)

    日期:2019.2.25 博客期:036 星期一 吼!今天我还是继续研究了自己的微信小程序,还没有连接数据库,只是在xml的设计上添加了不少东西,大家可以看我的截图,嗯~说到今天的收获,就是 marg ...