https://pintia.cn/problem-sets/994805342720868352/problems/994805389634158592

The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. The hash function is defined to be ( where TSize is the maximum size of the hash table. Quadratic probing (with positive increments only) is used to solve the collisions.

Note that the table size is better to be prime. If the maximum size given by the user is not prime, you must re-define the table size to be the smallest prime number which is larger than the size given by the user.

Input Specification:

Each input file contains one test case. For each case, the first line contains two positive numbers: MSize (≤) and N (≤) which are the user-defined table size and the number of input numbers, respectively. Then N distinct positive integers are given in the next line. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the corresponding positions (index starts from 0) of the input numbers in one line. All the numbers in a line are separated by a space, and there must be no extra space at the end of the line. In case it is impossible to insert the number, print "-" instead.

Sample Input:

4 4
10 6 4 15

Sample Output:

0 1 4 -

代码:

#include <bits/stdc++.h>
using namespace std; const int maxn = 1e4 + 10;
int N, M;
int num[maxn], prime[maxn];
int vis[maxn] = {0}; bool isprime(int x) {
if(x == 2) return true;
if(x <= 1) return false; for(int i = 2; i * i <= x; i ++) {
if(x % i == 0) return false;
}
return true;
} void Hash(int x) {
for(int i = 0; i < N; i ++) {
int cnt = (x + i * i) % M;
if(vis[cnt] == 0) {
vis[cnt] = 1;
printf("%d", cnt);
return ;
}
}
printf("-");
} int main() {
/*memset(prime, 1, sizeof(prime));
for(int i = 2; i * i <= maxn; i ++) {
for(int j = 2; j * i < maxn; j ++)
prime[j * i] = 0;
}*/ scanf("%d%d", &M, &N);
for(int i = 0; i < N; i ++)
scanf("%d", &num[i]); while(!isprime(M)) M ++; // mp.clear();
for(int i = 0; i < N; i ++) {
Hash(num[i]);
printf("%s", i != N - 1 ? " " : "");
}
return 0;
}

  二次方探查法   想当一个盲流子

 

PAT 甲级 1078 Hashing的更多相关文章

  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【hash】

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

  4. PAT 1145 1078| hashing哈希表 平方探测法

    pat 1145: 参考链接 Quadratic probing (with positive increments only) is used to solve the collisions.:平方 ...

  5. PAT 甲级 1145 Hashing - Average Search Time (25 分)(读不懂题,也没听说过平方探测法解决哈希冲突。。。感觉题目也有点问题)

    1145 Hashing - Average Search Time (25 分)   The task of this problem is simple: insert a sequence of ...

  6. PAT 甲级 1145 Hashing - Average Search Time

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

  7. PAT甲级——A1078 Hashing

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

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

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

  9. PAT 1078 Hashing[一般][二次探查法]

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

随机推荐

  1. [luogu3980] 志愿者招募

    题面 ​ 又一次考试网络流爆零...... ​ 这一题一看就是网络流, 但是要怎么构图呢? 考虑到途中的一些因素, 首先, 每一种志愿者控制的区间范围为\(S_{i}\)到\(T_{i}\), 所以, ...

  2. ubuntu16.04下的htk安装编译

    HTK(HMM Tools Kit)是一个剑桥大学开发的专门用于建立和处理HMM的实验工具包[1],主要应用于语音识别领域,也可以应用于语音合成.字符识别和DNA排序等领域.HTK经过剑桥大学.Ent ...

  3. 如何为Windows Forms应用程序添加启动参数(Start-Up Parameters)

    很多场合下,我们需要通过命令行或者快捷方式在Windows Forms程序启动时向其传递参数. 这些参数可能是用来加载某一个文档,或者是应用程序的初始化配置文件. 特别是对那些需要高度自定义配置的大程 ...

  4. redis cluster应用连接(password)

    application.properties 集群配置 application.properties #各Redis节点信息spring.redis.cluster.nodes=47.96.*.*:6 ...

  5. zabbix items 配置

    item是什么?它是我们对于host监控的基本条目,它属于不同的applications中,item的设置既可以针对具体的某个host主机,也可以针对模板进行设定(可以在多个主机进行复用). item ...

  6. RadioButtonFor控件

    mvc视图中的RadioButtonFor控件使用: 有几个单选子项就写几个RadioButtonFor,格式参照如下: @Html.RadioButtonFor(p => p.ScriptMo ...

  7. 探讨CAN总线的抗干扰能力

    探讨CAN总线的抗干扰能力 CAN总线经近20年的发展已步入壮年期,它不仅在汽车领域的应用占据一定优势,在其他工业应用上也生机勃勃.枝繁叶茂.究竟是什么原因使它这么成功?当人们发现它的局限性,又面临新 ...

  8. 第13章 GPIO—位带操作

    第13章     GPIO—位带操作 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fire ...

  9. [HAOI2008]排名系统 & [Zjoi2006]GameZ游戏排名系统 BZOJ1862&BZOJ1056

    分析: 平衡树裸题,(学完LCT感觉自己不会普通的Splay了...),维护每个节点的权值大小顺序,和时间戳顺序,之后map维护一下是否存在过,(懒得写字符串hash了). 附上代码: #includ ...

  10. Ubuntu 守护进程

    项目中用的Qt开发的GUI程序,需要随机自启动. 最初尝试过使用SuperVisor,但是会出现下面的错误. qt.qpa.screen: QXcbConnection: Could not conn ...