#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
using namespace std;
typedef long long ll;
bool isprime(int x){
for(int i=2;i*i<=x;i++){
if(x%i==0) return false;
}
if(x==0 || x==1) return false;
else
return true;
}
int main()
{
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif // LOCAL
bool h[101000]={false};
int n,m;
cin>>n>>m;
int k=n+1;
if(!isprime(n)){
while(!isprime(k)) k++;
}else k=n;
for(int i=0;i<m;i++){
int x;
cin>>x;
int pos=x%k;
if(h[pos]==false){ cout<<pos;h[pos]=true; }
else{
int d=1;
while(h[(pos+d*d)%k]){
d++;
if(d>k) break;
}
if(h[(pos+d*d)%k]==false){
cout<<(pos+d*d)%k;h[(pos+d*d)%k]=true;
}else cout<<"-";
}
if(i!=m-1) cout<<" ";
}
return 0;
}
【PAT】1078. Hashing
最新推荐文章于 2025-09-22 19:40:54 发布
本文介绍了一个基于素数探测的哈希应用算法。该算法首先确定一个素数作为哈希表大小,并通过线性探测解决冲突。文章提供了一个完整的C++实现,包括判断素数的函数和主程序流程。
489

被折叠的 条评论
为什么被折叠?



