首页 > 其他 > 详细

D - DZY Loves Hash CodeForces - 447A

时间:2017-07-05 00:04:54      阅读:353      评论:0      收藏:0      [点我收藏+]

 

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

Example

Input
10 5
0
21
53
41
53
Output
4
Input
5 5
0
1
2
3
4



题意:把n个数字放到0~p-1个位置,要求x放在x%p的位置;如果已经放过 输出x的位置,否则输出-1;
(吐槽一下这个题,一直wa就是找不到原因,最后发现原来是没有数组清零,真是太坑了;)
代码:
#include<iostream>
#include<cstring>
#include<string>
#include<sstream>
#include<algorithm>
using namespace std;


int main(){
   int n,p,k,flag=0;
   //char c[301][20];
   char m[305];

   unsigned long long x;
   cin>>p>>n;
   for(int i = 0;i<p;i++)
   {
       m[i]=0;
   }//数组一定要清零,
   for(int i=0;i<n;i++){
    cin>>x;
    k=x%p;
    if(m[k]==0)
        m[k]=1;
    else if(!flag)
        flag=i+1;
   }
   if(flag)cout<<flag<<endl;
   else cout<<-1<<endl;




}

 







 Output
-1

D - DZY Loves Hash CodeForces - 447A

原文:http://www.cnblogs.com/xuyibao/p/7118980.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!