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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • if (donutString.indexOf(“dozen”) != -1)是什么意思

    if (donutString.indexOf(“dozen”) != -1)是什么意思

    2021年10月29日
    65
  • native2ascii命令详解[通俗易懂]

    native2ascii命令详解[通俗易懂]1、native2ascii简介:native2ascii是sunjavasdk提供的一个工具。用来将别的文本类文件(比如.txt,.ini,.properties,.java等等)编码转为Unicode编码。为什么要进行转码,原因在于程序的国际化。Unicode编码的定义:Unicode(统一码、万国码、单一码)是一种在计算机上使用的字符编码。它为每种语言中的每个字符设定了统一并

    2025年11月2日
    2
  • 音视频编解码常用知识点

    音视频编解码常用知识点目录视频播放器原理流媒体协议封装格式(容器)编解码转码帧(Frame)帧率(Framerate)分辨率比特率(码率)采样率采样位数声道数有损压缩和无损压缩帧内压缩和帧间压缩对称编码和不对称编码音频编码声音数字化三要素音频编码标准视频编码色彩空间RGB色彩空间YUV色彩空间压缩原理熵与冗余帧内编码…

    2022年7月13日
    26
  • AJAX

    AJAX

    2022年4月2日
    39
  • java runtimeexception check_CheckException和RuntimeException

    java runtimeexception check_CheckException和RuntimeExceptionjava文档中对RuntimeException的定义是:RuntimeException是那些可能在Java虚拟机正常运行期间抛出的异常的超类。可能在执行方法期间抛出但未被捕获的RuntimeException的任何子类都无需在throws子句中进行声明。java中Exception分为两类,一类是CheckException一类是UncheckException。并且java的E…

    2022年7月24日
    12
  • c语言数组合并「建议收藏」

    c语言数组合并「建议收藏」c语言数组合并;注意,在函数中计算数组的长度可能会出错,尽量调用数组长度值#include&lt;stdio.h&gt;#include&lt;stdlib.h&gt;voidmergelist(int*a,intlen_a,int*b,intlen_b,int*c);//两个数组合并voidmergelist(int*a,intlen_a,int*b,int…

    2025年6月14日
    3

发表回复

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

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