PAT甲级1078 Hashing【hash】
题目: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】的更多相关文章
- pat 甲级 1078. Hashing (25)
1078. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of t ...
- PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive int ...
- PAT 甲级 1078 Hashing
https://pintia.cn/problem-sets/994805342720868352/problems/994805389634158592 The task of this probl ...
- PAT Advanced 1078 Hashing (25) [Hash ⼆次⽅探查法]
题目 The task of this problem is simple: insert a sequence of distinct positive integers into a hash t ...
- PAT 1145 1078| hashing哈希表 平方探测法
pat 1145: 参考链接 Quadratic probing (with positive increments only) is used to solve the collisions.:平方 ...
- PAT 甲级 1145 Hashing - Average Search Time (25 分)(读不懂题,也没听说过平方探测法解决哈希冲突。。。感觉题目也有点问题)
1145 Hashing - Average Search Time (25 分) The task of this problem is simple: insert a sequence of ...
- PAT 甲级 1145 Hashing - Average Search Time
https://pintia.cn/problem-sets/994805342720868352/problems/994805343236767744 The task of this probl ...
- PAT甲级——A1078 Hashing
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
- PAT 甲级真题题解(63-120)
2019/4/3 1063 Set Similarity n个序列分别先放进集合里去重.在询问的时候,遍历A集合中每个数,判断下该数在B集合中是否存在,统计存在个数(分子),分母就是两个集合大小减去分 ...
随机推荐
- 一篇文让你看懂NB-IoT、LoRa、eMTC、Sigfox及ZigBee的应用场景【转】
转自:https://blog.csdn.net/nicholas_dlut/article/details/81051269
- mysql 迁移
背景 这次做oracle数据迁移,也想总结像mysql的数据迁移方式.简单列下吧,因为具体方式网上很多. 方式 可以通过修改mysql.ini的数据文件目录位置方法实现拷贝迁移,此种方式简单 通过备份 ...
- Windows Internals 笔记——作业
1.Windows提供了一个作业内核对象,它允许我们将进程组合在一起并创建一个“沙箱”来限制进程能够做什么.创建只包含一个进程的作业同样非常有用,因为这样可以对进程施加平时不能施加的限制. 2.如果进 ...
- Java获取Ip发送邮件
import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import javax.servlet. ...
- Ubuntu16.04安装VS Code
1. 官网下载VS Code安装包, 如名字为 vscodename.deb 2. 在 vscodename.deb 所在的文件夹打开 Terminal,输入 sudo dpkg -i vscoden ...
- Consideration about improving mathematics study
In this article, I’ll present my ideas about how to improve mathematics study, which are the forewor ...
- EF执行SQL返回动态类型
using System; using System.Data.Common; using System.Data.Entity.Core.Objects; using System.Data.Ent ...
- Python学习之MySQLdb模块
摘要: MySQLdb模块用于操作mysql数据库.1.安装MySQLdb模块 yum install MySQL-python -y2.操作流程①.导入模块: import MySQLdb②.连接数 ...
- Linux-vi编辑器简单使用(保证存活)
vi编辑器基本模式 命令行模式(command mode) 光标移动.复制粘贴.删除 插入模式(insert mode) 文字输入 底行模式(last line mode) 保存.退出 模式转换 co ...
- Orleans框架简单示范
希望现在学习Orleans不会晚,毕竟Server Fabric都开源了.本篇博客从Sample的HelloWorld示例程序来解读Orleans的Grains. Server配置参考 : https ...