CF A. 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
//题意就是找相等的数,输出第二个的位置,但是要是最先发现的。
例如:10 5
      1 2 2 2 1
输出是3而不是5,因为先找到2和2相等,如果只用for循环,找到的是1和1相等输出是5.
第4个样例卡了很久,没看懂题目。。。。
#include <iostream>
using namespace std;
int main()
{   __int64 a[400];
    int n,t,i,j,p,k;
    while(scanf("%d%d",&p,&n)!=EOF)
    {   memset(a,0,sizeof(a));
        t=0;
        for(i=0;i<n;i++)
        {
            scanf("%I64d",&a[i]);
            a[i]=a[i]%p;
        }
        k=n;
        for(i=0;i<n-1;i++)
        {
            for(j=i+1;j<n;j++)
                if(a[i]==a[j])
                {
                    k=k<(j+1)?k:(j+1);
                        t=1;
                }

        }
        if(t==1)
            printf("%d\n",k);
       if(t==0)
           printf("-1\n");
    }
    return 0;
}

CF A. DZY Loves Hash

时间: 2024-10-08 01:13:20

CF A. DZY Loves Hash的相关文章

[2016-04-13][codeforces][447][A][DZY Loves Hash]

[2016-04-13][codeforces][447][A][DZY Loves Hash].md 时间:2016-04-13 23:35:11 星期三 题目编号:[2016-04-13][codeforces][447][A][DZY Loves Hash] 题目大意:问hash是否冲突 分析:模拟一遍即可 #include<cstdio> #include<cstring> using namespace std; const int maxn = 300 + 10; in

Cf 444C DZY Loves Colors(线段树)

DZY loves colors, and he enjoys painting. On a colorful day, DZY gets a colorful ribbon, which consists of n units (they are numbered from 1 to n from left to right). The color of thei-th unit of the ribbon is i at first. It is colorful enough, but w

(CF)Codeforces445A DZY Loves Chessboard(纯实现题)

转载请注明出处:http://blog.csdn.net/u012860063? viewmode=contents 题目链接:http://codeforces.com/problemset/problem/445/A DZY loves chessboard, and he enjoys playing with it. He has a chessboard of n rows and m columns. Some cells of the chessboard are bad, oth

Cf 444C DZY Loves Colors(段树)

DZY loves colors, and he enjoys painting. On a colorful day, DZY gets a colorful ribbon, which consists of n units (they are numbered from 1 to n from left to right). The color of thei-th unit of the ribbon is i at first. It is colorful enough, but w

D - DZY Loves Hash CodeForces - 447A

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

Codeforces Round #FF (Div. 2) A. 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

Codeforces Round #FF(255) DIV2 C - DZY Loves Sequences

A - DZY Loves Hash 水题,开辟一个数组即可 #include <iostream> #include <vector> #include <algorithm> #include <string> using namespace std; int main(){ int p,n; cin >> p >> n; vector<bool> buckets(302,false); bool flag = fal

Codeforces446C DZY Loves Fibonacci Numbers(线段树 or 分块?)

第一次看到段更斐波那契数列的,整个人都不会好了.事后看了题解才明白了一些. 首先利用二次剩余的知识,以及一些数列递推式子有下面的 至于怎么解出x^2==5(mod 10^9+9),我就不知道了,但是要用的时候可以枚举一下,把这些参数求出来之后就题目就可以转化为维护等比数列. 由于前面的常数可以最后乘,所以就等于维护两个等比数列好了. 下面我们来看如何维护一个等比数列.假如我对区间[L,R]的加上1,2,4,8...2^n的话,那么我只需要加一个标记x表示这个区间被加了多少次这样的2^n. 举个例

CF444C DZY Loves Colors

考试完之后打的第一场CF,异常惨烈呀,又只做出了一题了.A题呆滞的看了很久,领悟到了出题者的暗示,应该就是两个点的时候最大吧,不然的话这题肯定特别难敲,YY一发交上去然后就过了.然后就在不停地YY B题,赛后听了英姐的答案,看了题解,发现其实自己是捕捉到了正确的解题思路的,但是因为不知道怎么算出期望的复杂度,因而就没敢敲,哪里会想到算复杂度会算期望的情况的呢- -0 然后就来说下坑爹的C题了,比赛的时候看了觉得是线段树,它支持段更,但是每次段更的时候对于每个点,更新的差值|x-y|要被记录下来,