您的位置:首页 > 其它

A. DZY Loves Hash

2014-07-15 20:37 288 查看
A. DZY Loves Hash

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

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.

Input
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
Output a single integer — the answer to the problem.

Sample test(s)

Input
10 5
0
21
53
41
53


Output
4


Input
5 5
0
1
2
3
4


Output
-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;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  CF_Div. 2_#254