设为首页 加入收藏

TOP

CF 447A(DZY Loves Hash-简单判重)
2015-07-20 18:07:20 来源: 作者: 【 】 浏览:6
Tags:447A DZY Loves Hash- 简单

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 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.

Input

The 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).

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



题目大意:有一堆数扔进Hash表,求第一次出现2个数在1个格子中的情况

模拟


#include
  
   
#include
   
     #include
    
      #include
     
       #include
      
        #include
       
         #include
        
          #include
         
           #include
          
            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
           
            =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<
            
             


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDU 1232:畅通问题(并查集) 下一篇wikioi 1051哈希表

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: