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集合中是否存在,统计存在个数(分子),分母就是两个集合大小减去分 ...
随机推荐
- mac使用apktool打包回apk
升级最新版的apktool 下载目录:https://ibotpeaches.github.io/Apktool/install/. 权限修改 将上述的文件放在usr/local/bin目录下并执行 ...
- MinGW GCC 8.1.0 2018年5月2日 出炉啦
MSYS_MinGW-w64_GCC_810_x86-x64.7z for x86 x64 59.0 MB发布日期: 2018-05-04 下载地址:https://sourceforge.net/p ...
- 响应式bootstrap - demo
参考资料: bootstrap:http://www.bootcss.com/ 汤姆大叔的博客: <深入理解Bootstrap>http://item.jd.com/11462962.ht ...
- 【原创】大叔问题定位分享(3)Kafka集群broker进程逐个报错退出
kafka0.8.1 一 问题现象 生产环境kafka服务器134.135.136分别在10月11号.10月13号挂掉: 134日志 [2014-10-13 16:45:41,902] FATAL [ ...
- Python 通用爬虫 和讯博客 scrapy
目标站点需求分析 通用爬虫,获取和讯博客所有博文 涉及的库 scrapy,re,requests,mysql URL RULE 解析单页源码 保存到数据库 结果
- intellij idea创建maven项目
1.安装好JDK,Tomcat,安装好maven: 2.配置maven全局变量:file->Other Settings ->Default Settings->Build,Exec ...
- git命令之git remote的用法
git remote git remote -v git init git add xxx git commit -m 'xxx' git remote add origin ssh://softw ...
- windows下的node环境搭建
node环境的搭建过程: 1.首先在nodejs.org官网上下载一个msi安装文件,安装(过程很简单,基本上是一路next) 2.安装完成后,简单测试下是否安装成功,在cmd下输入两个命令: nod ...
- 4.9cf自训9..
cf401D 状态压缩dp好题,每次把新加入集合的数字放在最后即可 /* 它可以通过重新排列数字n, 它没有任何前导零, x除以m后的余数等于0. 每次把新加的数放在最后 dp[i][j]表示状态i下 ...
- Django + Uwsgi + Nginx 的生产环境部署
使用runserver可以使我们的django项目很便捷的在本地运行起来,但这只能在局域网内访问,如果在生产环境部署django,就要多考虑一些问题了.比如静态文件处理,安全,效率等等,本篇文章总结归 ...