题意:

输入两个正整数M和N(M<=10000,N<=M)表示哈希表的最大长度和插入的元素个数。如果M不是一个素数,把它变成大于M的最小素数,接着输入N个元素,输出它们在哈希表中的位置(从0开始),如有冲突采取二次探测法处理冲突。

trick:

测试点1包含M为1的数据,1不是素数。。。

AAAAAccepted code:

 #include<bits/stdc++.h>
using namespace std;
int a[];
int ans[];
int vis[];
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int m,n;
cin>>m>>n;
int flag=;
for(int i=;i*i<=m;++i)
if(m%i==){
flag=;
break;
}
if(flag||m==)
for(int i=m+;;++i){
int flag2=;
for(int j=;j*j<=i;++j)
if(i%j==){
flag2=;
break;
}
if(!flag2){
m=i;
break;
}
}
for(int i=;i<=n;++i)
cin>>a[i];
for(int i=;i<=n;++i){
int tamp=a[i]%m;
if(!vis[tamp]){
vis[tamp]=;
ans[i]=tamp;
}
else{
int flag3=;
for(int j=;j<n;++j)
if(!vis[(tamp+j*j)%m]){
vis[(tamp+j*j)%m]=;
ans[i]=(tamp+j*j)%m;
flag3=;
break;
}
if(!flag3)
ans[i]=-;
}
}
for(int i=;i<=n;++i){
if(ans[i]==-)
cout<<"-";
else
cout<<ans[i];
if(i<n)
cout<<" ";
}
return ;
}

【PAT甲级】1078 Hashing (25 分)(哈希表二次探测法)的更多相关文章

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

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

  2. pat 甲级 1078. Hashing (25)

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

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

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

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

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

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

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

  6. PAT甲级1078 Hashing【hash】

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

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

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

  8. PAT 甲级 1078 Hashing

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

  9. 1078 Hashing (25分)

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

随机推荐

  1. [CF755B] PolandBall and Game - 博弈论

    [CF755B] Description 两个人轮流说单词,每个人只能说自己的的单词库里的单词.被任意方说过的单词不能再说.一个单词只会在某个人的单词库中出现一次,但是可能同时出现在两个人的单词库中. ...

  2. (转)hashmap hashtable 的区别 Hash table 内部的数据结构

    转自:http://www.cnblogs.com/carbs/archive/2012/07/04/2576995.html Hashtable 和 HashMap 做为 Map 的基本特性 两者都 ...

  3. python之路之面向对象

    一.编程 1.面向对象编程和函数式编程 a.面向对象编程之self(重点) b.面向对象的封装  c.继承 (1)继承的优先级 d.重载 e.多态  2.查看socketserver源码执行过程

  4. 第三十篇 玩转数据结构——字典树(Trie)

          1.. Trie通常被称为"字典树"或"前缀树" Trie的形象化描述如下图: Trie的优势和适用场景 2.. 实现Trie 实现Trie的业务无 ...

  5. pikaqiu练习平台(CSRF(跨站请求伪造) )

    CSRF(跨站请求伪造)   CSRF(跨站请求伪造)概述 Cross-site request forgery 简称为“CSRF”,在CSRF的攻击场景中攻击者会伪造一个请求(这个请求一般是一个链接 ...

  6. 怎么把项目发布到github上

    方法一:在github上新建一个项目,然后在本地任意个文件夹(最好新建)右键 git bash here   ,再之后 git clone https://github.com/CKTim/BlueT ...

  7. [Netcat] 断线重连 自动重连

    今天想做些操作,所以想到了nc 但是nc太过于轻量级 所以 导致我没有找到他的断线重连功能 然后我就想到了windows的神器之一 vbs脚本 vbs代码如下 Dim a,b set a= WScri ...

  8. winform学习(2)窗体属性

    窗体也属于控件(controls) 主窗体:在Main函数中创建的窗体,当关闭主窗体时,整个程序也就关闭了. 如何打开控件属性面板: ①在该控件上单击鼠标右键--属性. ②选中该控件,按F4 窗体常用 ...

  9. Python记:通用的序列操作之成员资格(听起来倒是有些抽象的!)

    ______________________________永远守护这一尘不染的真心! 要检查特定的值是否包含在序列中,可使用运算符in.它检查是否满足指定的条件,并返回相应的值:满足时返回True, ...

  10. 第十八篇 Linux环境下常用软件安装和使用指南

          提醒:如果之后要安装virtualenvwrapper的话,可以直接跳到安装virtualenvwrapper的方法,而不需要先安装好virtualenv   安装virtualenv和生 ...