1078 Hashing (25)

#include<bits/stdc++.h>
using namespace std;
const int MSIZE=10010;
bool isprime(int a){
	int temp=sqrt(a);
	if(a==1) return false;
	if(a==2) return true;
	for(int i=2;i<=temp;i++){
		if(a%i==0){
			return false;
		}
	}
	return true;
}
int main()
{
	#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	#endif
	int n,m;
	cin>>n>>m;
	while(isprime(n)==false){
		n++;
	}
	int hash[MSIZE]={};
	map<int,int> mp;
	vector<int> ppp;
	for(int i=0;i<m;i++){
		int temp;
		cin>>temp;
		if(hash[temp%n]==0){
			mp[temp]=temp%n;
			hash[temp%n]=1;
		}else{
			int i=1;int flag=0;
			while(i<=n){
				if(hash[(temp+i*i)%n]==0){
					mp[temp]=(temp+i*i)%n;
					hash[(temp+i*i)%n]=1;flag=1;break;
				}
				i++;
			}
			if(flag==0){
				mp[temp]=-1;
			}
		}
		ppp.push_back(temp);
	}
	for(int i=0;i<ppp.size();i++){
		if(i==0){
			if(mp[ppp[i]]!=-1){
				cout<<mp[ppp[i]];
			}else{
				cout<<"-";
			}
		}else{
			if(mp[ppp[i]]!=-1){
				cout<<' '<<mp[ppp[i]];
			}else{
				cout<<" -";
			}
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/csg3140100993/article/details/81808357