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. 《阿里巴巴 Java 开发手册》划重点!

    [强制]小数类型为 decimal,禁止使用 float 和 double. 说明:float 和 double 在存储的时候,存在精度损失的问题,很可能在值的比较时,得到不 正确的结果.如果存储的数 ...

  2. opengl渲染管线梳理

    opengl渲染管线梳理 http://www.cnblogs.com/zhanglitong/p/3238989.html 坐标系变换和矩阵 http://www.cppblog.com/guoji ...

  3. weex+vue2.x 踩坑实录(不定期更新)

    执行 npm start 显示空白页面 这个是开始使用weex就出现的一个大坑,说实话对新手真的很不友好. 1.打开控制台显示:Cannot assign to read only property ...

  4. 解决:linux 固定ip 导致ping 外网unknown host

    首先说下问题产生场景:最近搞jenkins搭建持续集成,搞完后发现服务器ip(ifconfig 红色)老是变化,一怒之下果断修改ip [root@bogon etc]# ifconfigeth0 Li ...

  5. Jmeter—控件

    Jmeter有许多控件,可以在我们模拟测试请求时使用. Jmeter共有这8类控件: 配置元件—Http请求默认值 作用:仅设置一次目标URL服务器地址,之后不需要每次请求都写完整的,仅写相对地址就可 ...

  6. 字典树Trie树

    摘自大佬博客 https://www.cnblogs.com/TheRoadToTheGold/p/6290732.html 给出n个单词和m个询问1.查询某个前缀是否出现过2.查询某个单词是否出现过 ...

  7. HDU 2767 Proving Equivalences(至少增加多少条边使得有向图变成强连通图)

    Proving Equivalences Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  8. [Jsoi2016]最佳团体 BZOJ4753 01分数规划+树形背包/dfs序

    分析: 化简一下我们可以发现,suma*ans=sumb,那么我们考虑二分ans,之后做树形背包上做剪枝. 时间复杂度证明,By GXZlegend O(nklogans) 附上代码: #includ ...

  9. 20155328 《网络攻防》 实验一:PC平台逆向破解(5)M

    20155328 <网络攻防> 实验一:PC平台逆向破解(5)M 实践目标 实践对象:linux可执行文件pwn1. 正常执行时,main调用foo函数,foo函数会简单回显任何用户输入的 ...

  10. 7、Class文件的格式

    Class文件的格式 1.magic(魔数) 身份标识,用来标记这是不是一个CLASS文件 CLASS的魔数比较有浪漫气息,是0xCAFEBABE(咖啡宝贝),也标识着将来JAVA咖啡商标: 2.之后 ...