题意:

输入两个正整数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. OpenGL基本图元类型

    GL_POINTSGL_LINESGL_LINE_STRIPGL_LINE_LOOPGL_TRIANGLESGL_TRIANGLE_STRIPGL_TRIANGLE_FANGL_QUADSGL_QUA ...

  2. 开启macOS的原生写入Ntfs的功能

    在使用原生写入Ntfs功能前,测试了第三方的ntfs-3g的方案超级不稳定,害怕数据丢失果断放弃. 基于osxfuse的ntfs-3g:https://github.com/osxfuse/osxfu ...

  3. html5的placeholder属性(IE如何兼容placeholder属性)

    界面UI推荐 jquery html5 实现placeholder兼容password  IE6 html5的placeholder属性(IE如何兼容placeholder属性) 2013-01-05 ...

  4. 跨表更新,Mysql Update Join

    背景 项目新导入了一批人员数据,这些人的有的部门名称发生了变化,有的联系方式发生了变化,暂且称该表为t_dept_members, 系统中有另外一张表 t_user_info 记录了人员信息. 要求将 ...

  5. MySql -- default 默认约束

    常用数据库约束: 一.default 默认约束: 二.not null:非空约束,指定某列不为NULL: 三.unique:唯一约束,指定某列和几列组合的数据不能重复: 四.primary key:主 ...

  6. rancher布控集群启动失败

    rancher布控集群启动失败 待办 报告缺少某个文件.多线程启动任务部署的时候某些线程跑在前边了, 导致问题出现 解决思路:等待,等待响应的job重启就ok了,都是一些job在跑,失败了会重新开始的 ...

  7. 问题 D: 家庭问题

    问题 D: 家庭问题 时间限制: 1 Sec  内存限制: 128 MB[命题人:admin] 题目描述 有n个人,编号为1,2,……n,另外还知道存在K个关系.一个关系的表达为二元组(α,β)形式, ...

  8. 95. 不同的二叉搜索树 II、96. 不同的二叉搜索树

    95 Tg:递归 这题不能算DP吧,就是递归 一个问题:每次的树都要新建,不能共用一个根节点,否则下次遍历对根左右子树的改动会把已经放进结果数组中的树改掉.. class Solution: def ...

  9. 计算机网络 - TCP_NODELAY 和 TCP_CORK, TCP_NOPUSH

    参考 https://www.cnblogs.com/biyeymyhjob/p/4670502.html https://stackoverflow.com/questions/3761276/wh ...

  10. servlet中Request与response使用

    服务器根据请求自动创建传入HttpServletRequest对象和HttpServletResponse对象 @Override protected void service(HttpServlet ...