CF 447A(DZY Loves Hash-简单判重)
DZY has a hash table with p buckets, numbered from 0 to p - 1. He wants to insert n numbers, in the order they are given, into the hash table. For the i-th number xi, DZY will put it into the bucket numbered h(xi), where h(x) is the hash function. In this problem we will assume, that h(x) = x mod p. Operation a mod b denotes taking a remainder after division a 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 the i-th insertion, you should output i. If no conflict happens, just output -1.
InputThe first line contains two integers, p and n (2 ≤ p, n ≤ 300). Then n lines follow. The i-th of them contains an integer xi (0 ≤ xi ≤ 109).
OutputOutput a single integer — the answer to the problem.
Sample test(s) input10 5 0 21 53 41 53output
4input
5 5 0 1 2 3 4output
-1
题目大意:有一堆数扔进Hash表。求第一次出现2个数在1个格子中的情况
模拟
#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<functional> #include<iostream> #include<cmath> #include<cctype> #include<ctime> using namespace std; #define For(i,n) for(int i=1;i<=n;i++) #define Fork(i,k,n) for(int i=k;i<=n;i++) #define Rep(i,n) for(int i=0;i<n;i++) #define ForD(i,n) for(int i=n;i;i--) #define RepD(i,n) for(int i=n;i>=0;i--) #define Forp(x) for(int p=pre[x];p;p=next[p]) #define Lson (x<<1) #define Rson ((x<<1)+1) #define MEM(a) memset(a,0,sizeof(a)); #define MEMI(a) memset(a,127,sizeof(a)); #define MEMi(a) memset(a,128,sizeof(a)); #define INF (2139062143) #define F (100000007) #define MAXN (300+10) long long mul(long long a,long long b){return (a*b)%F;} long long add(long long a,long long b){return (a+b)%F;} long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;} typedef long long ll; int p,n; bool b[MAXN]; int main() { // freopen("DZY Loves Hash.in","r",stdin); // freopen(".out","w",stdout); cin>>p>>n; MEM(b) For(i,n) { int x; cin>>x; x%=p; if (b[x]) {cout<<i<<endl; return 0;} else b[x]=1; } cout<<"-1"<<endl; return 0; }
- CF 447A(DZY Loves Hash-简单判重)
- CF A. DZY Loves Hash
- CF A. DZY Loves Hash
- Codeforces Round #FF (Div. 2) 447A - DZY Loves Hash(模拟)
- cf-#FF-A-DZY Loves Hash
- CF D. DZY Loves Strings(字符串hash 暴力)
- E--DZY Loves Chemistry(CF-445B
- CF 254Div1 C.DZY Loves Colors
- A. DZY Loves Chessboard (CF)
- CF 444B(DZY Loves FFT-时间复杂度)
- CF 445B(DZY Loves Chemistry-求连通块)
- CF Round #FF (Div. 2) C DZY Loves Sequences_DP
- A. DZY Loves Hash
- Cf 444C DZY Loves Colors(段树)
- CF 444B(DZY Loves FFT-时间复杂度)
- Codeforces Round #FF (Div. 2/A)/Codeforces447A_DZY Loves Hash(哈希)
- Codeforces Round #FF (Div. 2)A. DZY Loves Hash
- CF 444A(DZY Loves Physics-诱导子图的密度)
- A. DZY Loves Hash
- CF 444C DZY Loves Colors