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
Output
Input
Output
//水题不多说
//AC代码
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 A. DZY Loves Hash
- Codeforces Round #FF (Div. 2):Problem A - DZY Loves Hash
- Codeforces Round #FF (Div. 2) 447A - DZY Loves Hash(模拟)
- CodeForces 447-A. DZY Loves Hash
- Codeforces Round #FF (Div. 2) A. DZY Loves Hash
- A. DZY Loves Hash
- CF 447A(DZY Loves Hash-简单判重)
- CF D. DZY Loves Strings(字符串hash 暴力)
- Codeforces Round #FF (Div. 2/A)/Codeforces447A_DZY Loves Hash(哈希)
- Codeforces Round #254 (Div. 1) D. DZY Loves Strings hash 暴力
- Codeforces Round #FF (Div. 2) Problem A DZY Loves Hash
- CF 447A(DZY Loves Hash-简单判重)
- CF A. DZY Loves Hash
- cf-#FF-A-DZY Loves Hash
- Codeforces Round #FF (Div. 2):Problem A - DZY Loves Hash
- Codeforces Round #FF (Div. 2)A. DZY Loves Hash
- Codeforces Round #FF (Div. 2)A. DZY Loves Hash
- Codeforces Round #FF (Div. 2)C. DZY Loves Sequences
- Codeforces Round #FF 446 C. DZY Loves Fibonacci Numbers
- bzoj3512: DZY Loves Math IV