题目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. Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)

    题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...

  2. 双系统下Ubuntu扩展根目录空间方法

    最近,在Ubuntu16.04上装了个matlab,突然发现根目录空间只剩1G了,这哪儿够用啊,就想着有没有一种方法不用重装系统就可以扩展根目录空间呢?别说还真有,看下文. 开始之前先分出一些未分配空 ...

  3. 51nod 1215 数组的宽度

    若一个数在一段区间内作为最大值存在,那么答案应该加上这个数 若一个数在一段区间内作为最小值存在,那么答案应该减去这个数 所以我们利用单调栈,高效求出a[i]在哪个区间内作为最大/最小值存在,从而确定, ...

  4. MapReduce输出文件名更改

    默认情况下生成的文件名是part-r-00000格式,想要自定义生成输出文件名可以使用org.apache.hadoop.mapreduce.lib.output.MultipleOutputs类用来 ...

  5. 题解P3711:【仓鼠的数学题】

    这题黑的丫!怎么会掉紫呢! noteskey 伯努利数... 这里 有介绍哟~ 写的非常详细呢~ 反正这题就是推柿子... 另外就是黈力算法的运用 QWQ 我们令 \(ANS(x)\) 为答案多项式, ...

  6. 深入理解JavaScript,这一篇就够了

    前言 JavaScript 是我接触到的第二门编程语言,第一门是 C 语言.然后才是 C++.Java 还有其它一些什么.所以我对 JavaScript 是非常有感情的,毕竟使用它有十多年了.早就想写 ...

  7. sublime自动保存设置

    首选项——用户设置 (Preferences:Settings - User) 行末添加"save_on_focus_lost": true 注意用逗号分隔 保存即可 save_o ...

  8. JAVA ArrayList实现随机生成数字,并把偶数放入一个列表中

    package Code429; import java.util.ArrayList;import java.util.Random; public class CodeArrayListPrint ...

  9. numpy中的meshgrid

    经常遇到meshgrid,一段时间不用就忘记了,记录之 meshgrid用于生成网格点的坐标矩阵(参考https://blog.csdn.net/lllxxq141592654/article/det ...

  10. intellij idea创建maven项目

    1.安装好JDK,Tomcat,安装好maven: 2.配置maven全局变量:file->Other Settings ->Default Settings->Build,Exec ...