一些做过的题

来源:互联网 发布:js游戏制作教程 编辑:程序博客网 时间:2024/04/29 13:02

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 Input

Input

10 5021534153

Output

4

Input

5 501234

Output

-1

题目大意:找到第一个余数重复出现的编号;将一个数组全初始化为0,余数作为数组下标,如a[1],a[2]...,重复出现的就给count加,此处count就是记录的第一个重复出现的。

 

#include<stdio.h>int a[303];int main(){int p,n,i,count=-1,flag=1;scanf("%d%d",&p,&n);for(i=0;i<n;i++){int x;scanf("%d",&x);if(flag){if(a[x%p]==0)a[x%p]=1;else{count=i+1;flag=0;}}}printf("%d",count);return 0;}


 

0 0
原创粉丝点击