题意:

输入两个正整数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. linux命令 EOF

    在shell脚本中,通常将EOF与 << 结合使用,表示后续的输入作为子命令或子Shell的输入,直到遇到EOF为止,再返回到主Shell. EOF只是一个分界符,当然也可以用abcde替 ...

  2. css+div上下左右自适应居中

    主要记录自己日常积累的布局相关的东西,持续更新中. 1.登录框上下左右自适应居中 以前想要把登录表单始终放置在页面的中间,花了不少心思,一直以来用的解决方法都是用js,感觉有点麻烦不是很好,于是在网上 ...

  3. android 代码设置图标背景色(圆形图标)和图标颜色

    原图: 1.改变图标颜色: mImageView.setColorFilter(Color.parseColor("#ff0000")); 效果: 2.改变图标背景色(主要是圆形图 ...

  4. Html5 Canvas 使用

    <div> <img id="scream" src="dali.jpg" alt="The Scream" width= ...

  5. 用python实现密码校验程序

    密码需要符合下面的要求: 8个字符以上,包含数字,大小写,开头不能为特殊字符. #! /usr/bin/pythonimport re password = str(input()) def lenO ...

  6. 等差数列Arithmetic Progressions题解(USACO1.4)

    Arithmetic Progressions USACO1.4 An arithmetic progression is a sequence of the form a, a+b, a+2b, . ...

  7. 关于Ajax请求的JS封装函数

    每次连接ajax都要重复写很多代码,所以写了一个JS封装函数,如下: 再来解释一下其中obj对象的参数形式: obj={ 'type':提交方式,    get/post 'url' : 提交地址, ...

  8. Gym安装ubuntu16.04

    Step1:将gym克隆到计算机上: git clone https://github.com/openai/gym.git 如果你的电脑没有安装git,那么键入 sudo apt install g ...

  9. bugku 矛盾 30

    首先打开网址链接会发现一串代码 然后进行分析代码的意思首先是一个函数查一下这个函数 然后会发现现代码第一句写的是输入数字,然后会发现第二行有一个感叹号意思是输入的如果不是数字则回复数字 如果输入数字则 ...

  10. Speech Bandwidth Extension With WaveNet

    利用WAVENET扩展语音带宽 作者:Archit Gupta, Brendan Shillingford, Yannis Assael, Thomas C. Walters 博客地址:https:/ ...