【PAT甲级】1078 Hashing (25 分)(哈希表二次探测法)
题意:
输入两个正整数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 分)(哈希表二次探测法)的更多相关文章
- PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive int ...
- pat 甲级 1078. Hashing (25)
1078. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of t ...
- PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)
1070 Mooncake (25 分) Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...
- PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)
1032 Sharing (25 分) To store English words, one method is to use linked lists and store a word let ...
- PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*
1029 Median (25 分) Given an increasing sequence S of N integers, the median is the number at the m ...
- PAT甲级1078 Hashing【hash】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805389634158592 题意: 给定哈希表的大小和n个数,使用 ...
- PAT Advanced 1078 Hashing (25) [Hash ⼆次⽅探查法]
题目 The task of this problem is simple: insert a sequence of distinct positive integers into a hash t ...
- PAT 甲级 1078 Hashing
https://pintia.cn/problem-sets/994805342720868352/problems/994805389634158592 The task of this probl ...
- 1078 Hashing (25分)
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
随机推荐
- jmeter 登陆--查询存在否-->新建客户-->查询存在否 + 压测
1.登陆 正则表达式提取器和json提取器,都是后置处理器提取token(都可以在响应中以regexp tester 和 json path tester查看提取的对不对) beanshell 后置处 ...
- dyt说反话(注意字符串输入)
题目内容: dyt喜欢对lrh说的话说反话,现给出lrh说的k句话,输出dyt所说的反话. 输入格式 第一行是样例个数k(k<10) 接下来k行,每行包含lrh说的一句话(每句话长度不超过50, ...
- C++-HDU1166-敌兵布阵[数据结构][树状数组]
单点修改+区间查询=树状数组 空间复杂度O(n) 时间复杂度O(mlogn) #include <set> #include <map> #include <cmath& ...
- object转为string
object为null时 错误:object.tostring() 空指针 正确:(string)object
- 【C语言】利用二维数组输出成绩
目的:用二维数组输出下面成绩 希望你可以成为第五名童鞋! 代码: #include<stdio.h> int main() { /* 创建一个带有 4行 5 列的数组 */ ][] = { ...
- 10-Java-JSTL标签库的使用
使用JSTL标签库使用 第一步:引入相关jar包到WEB-INF/lib/,JSTL标签库(standard.jar,jstl.jar) 第二步:在JSP文件中通过 taglib指令引入标签库,例如: ...
- Codeforces A. Serval and Bus
inputstandard inputoutputstandard outputIt is raining heavily. But this is the first day for Serval, ...
- AVR单片机教程——DAC
本文隶属于AVR单片机教程系列. 单片机的应用场景时常涉及到模拟信号.我们已经会使用ADC把模拟信号转换成数字信号,本讲中我们要学习使用DAC把数字信号转换成模拟信号.我们还将搭建一个简单的功率放 ...
- c#修改项目名称
1.修改解决方案名称 右键,重命名 2.修改项目名称 右键,重命名 3.修改程序集名称和默认命名空间 项目,属性 4.替换解决方案中的名称 编辑,替换,替换范围默认整个解决方案 5.用记事本打开.sl ...
- .NTE Core Web API Example
Source from :https://www.codeproject.com/Articles/1260600/Speed-up-ASP-NET-Core-WEB-API-application- ...