Codeforces Round #FF (Div. 2):Problem A – DZY Loves Hash「建议收藏」

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



题意就是找相等的数,输出第二个的位置,可是要是最先发现的。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<sstream>
#include<cmath>

using namespace std;

#define f1(i, n) for(int i=0; i<n; i++)
#define f2(i, m) for(int i=1; i<=m; i++)
#define f3(i, n) for(int i=n; i>=0; i--)
#define M 1005

const int INF = 0x3f3f3f3f;

int main()
{

    int p, n, m, i;
    int ans[M];
    memset(ans, 0, sizeof(ans));
    scanf("%d%d",&p,&n);
    for (i=1; i<=n; i++)
    {
        scanf("%d",&m);
        if (ans[m%p]++ == 1)
            break;
    }
    printf("%d\n",(i>n)?-1:i);

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115706.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • setfacl命令基本用法[通俗易懂]

    setfacl命令基本用法[通俗易懂]setfacl命令可以用来细分linux下的文件权限。chmod命令可以把文件权限分为u,g,o三个组,而setfacl可以对每一个文件或目录设置更精确的文件权限。换句话说,setfacl可以更精确的控制权限的分配。比如:让某一个用户对某一个文件具有某种权限。这种独立于传统的u,g,o的rwx权限之外的具体权限设置叫ACL(AccessControlList)ACL可以针

    2022年6月16日
    45
  • animation rotate_canvas scale

    animation rotate_canvas scaleScaleAnimation、RotateAnimation、ScaleAnimation、TranslateAnimation详解

    2022年10月15日
    0
  • SSIS 实用表达式部分总结

    SSIS 实用表达式部分总结

    2021年11月26日
    69
  • Notepad++ 下载

    Notepad++ 下载DownloadNotepad++,Notepad++,Notepad下载,最新官方正式版Notepad++,remplacantdeNotepad++,Notepad2,netpad,opensource,webeditor,htmleditor,xmleditor,phpeditor,aspeditor,javascripteditor,javaeditor,c++editor,c#editor

    2022年4月27日
    37
  • js三目运算符

    js三目运算符js三目运算符js三目运算符的正常表达为variable=boolean_expression?true_value:false_value;当boolean_expression传入的不是表达式而是变量时,是如何判断的?在es5文档中找到了解释:先将boolean_expression进行计算拿到结果赋给lref,然后根据ToBoolean(lref)拿到是tr

    2022年6月29日
    24
  • 2元参数matlab图,实验二用matlab绘制一元函数与二元函数的图象-6页word资料

    2元参数matlab图,实验二用matlab绘制一元函数与二元函数的图象-6页word资料实验二用matlab绘制一元函数与二元函数的图象1.平面曲线的表示形式对于平面曲线,常见的有三种表示形式,即以直角坐标方程],[),(baxxfy∈=,以参数方程],[),(),(battyytxx∈==,和以极坐标],[),(barr∈=??表示等三种形式。2.曲线绘图的MATLAB命令可以用helpplot,helpfplot查阅有关这些命令…

    2022年9月3日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号