本文主要是介绍A. DZY Loves Hash,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
DZY has a hash table with p buckets, numbered from0 to p - 1. He wants to insertn numbers, in the order they are given, into the hash table. For thei-th number xi, DZY will put it into the bucket numberedh(xi), whereh(x) is the hash function. In this problem we will assume, thath(x) = x mod p. Operationa mod b denotes taking a remainder after divisiona by b.
However, each bucket can contain no more than one element. If DZY wants to insert an number into a bucket which is already filled, we say a "conflict" happens. Suppose the first conflict happens right after thei-th insertion, you should output i. If no conflict happens, just output -1.
The first line contains two integers, p andn (2 ≤ p, n ≤ 300). Thenn lines follow. The i-th of them contains an integer xi(0 ≤ xi ≤ 109).
Output a single integer — the answer to the problem.
10 5 0 21 53 41 53
4
5 5 0 1 2 3 4
-1
//水题不多说
//AC代码
#include<iostream>
#include<queue>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
using namespace std;
int main()
{
int p,n,x,y,j;
int z[301];
//while(1)
//{
cin>>p>>n;
map<int,int>Map;
memset(z,0,sizeof(z));
j=0;
for(int i=1;i<=n;i++)
{
cin>>x;
y=x%p;
Map[y]+=1;
if(Map[y]>1)
{
z[j++]=i;
}
}
if(j!=0)
{
cout<<z[0]<<endl;
}
else
cout<<-1<<endl;
//}
return 0;
}
这篇关于A. DZY Loves Hash的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!