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


相关推荐

  • php 7.1 openssl_decrypt() 代替 mcrypt_module_open() 方法

    php 7.1 openssl_decrypt() 代替 mcrypt_module_open() 方法

    2021年11月8日
    59
  • W25Q128FV译文(一)[通俗易懂]

    该文章包括W25Q128FV译文的前六章内容,第7章状态寄存器翻译及第八章指令部分翻译链接:https://blog.csdn.net/z123canghai/article/details/88726856第八章指令剩余部分及第九章相关时序翻译链接:https://blog.csdn.net/z123canghai/article/details/88726856目录一、概述…

    2022年4月4日
    488
  • VCL 控件分类_验证控件的分类

    VCL 控件分类_验证控件的分类TForm右下角小窗体中调整form显示位置。动态窗体:主窗体和动态生成的窗体(Project|Options|Forms)在一个头文件中添加另一个头文件(File|UseUnit)newTForm2(this);(this:指以此为容器)ShowModal(),Show();(是否当前窗体关闭后才能操作父窗体:模态方式,非模态方式)Close();(关闭窗体)(在Eve

    2022年9月25日
    3
  • pycharm如何安装numpy库_四上入库

    pycharm如何安装numpy库_四上入库NumPy(NumericalPython)是Python语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。NumPy是一个运行速度非常快的数学库,主要用于数组计算。(一)打开PyCharm,点击设置(二)选择左侧栏目中的“项目:pythonProject”–“Python解释器”,点击右侧“+”(三)输入“numpy”,安装即可…

    2022年8月27日
    6
  • javascript console_js decorator

    javascript console_js decoratorJSNavigatorappNameappVersionuserAgentplatform

    2025年11月1日
    4
  • arcmap重采样_ipproto_raw

    arcmap重采样_ipproto_raw参考文献:AcceleratedHypothesisGenerationforMulti-structureRobustFitting假设Input:{xi}i=1N\{x_i\}_{i=1}^N{xi​}i=1N​代表输入的N组数据,由N组数据随机采样生成了M个模型θ1,θ2,…θM{\theta_1,\theta_2,…\theta_M}θ1​,θ2​,…θM​,对于每一个个输入数据xix_ixi​,我们计算模型的残差得到该模型的分数r(i)=[r1(i),r2(i)…rM

    2022年9月22日
    2

发表回复

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

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