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


相关推荐

  • CBoard框架使用总结一[通俗易懂]

    CBoard框架使用总结一[通俗易懂]文章内容1.功能介绍2.源码结构分析3.总结1.功能介绍1.1.整体界面(CBoard支持中英文版本)左侧功能依次为:已创建的数据面板:包含已经创建的DashBoard配置功能:DashBoard配置功能集管理:主要是用户管理(Spring-Security)1.2.DashBoard配置功能主要包括:数据源定义:支持Elasticsearch、saiku、TextF

    2025年7月24日
    3
  • linux mysql数据库备份以及还原[通俗易懂]

    linux mysql数据库备份以及还原[通俗易懂]1备份命令mysqldump-h127.0.0.1-P3306-uroot-p123456databasename>database.sql2数据库还原命令mysql-h127.0.0.1-P3306-uroot-p123456databasename<database.sql

    2022年5月11日
    40
  • ubuntu 卸载命令_强制卸载电脑软件

    ubuntu 卸载命令_强制卸载电脑软件Ubuntu命令卸载软件_李柏林的博客-CSDN博客_ubuntu卸载程序1.打开一个终端,输入dpkg–list,按下Enter键,终端输出以下内容,显示的是你电脑上安装的所有软件。2.在终端中找到你需要卸载的软件的名称,列表是按照首字母排序的。3.在终端上输入命令sudoapt-get–purgeremove包名(–purge是可选项,写上这个属性是将软件及其配置文件一并删除,如不需要删除配置文件,可执行sudoapt-getr…https://blog.csdn.net/

    2025年10月11日
    4
  • java setvisible_java value

    java setvisible_java value如果查询返回多个值用list()方法publicvoidtestQuery(){Configurationconfig=newConfiguration().configure();SessionFactoryfactory=config.buildSessionFactory();//创建SessionFactorySessionsession=factory.open…

    2022年9月30日
    3
  • idea打包详解_vue打包后图片不显示

    idea打包详解_vue打包后图片不显示1.点击File->ProjectStructure2.选择打包类型3.指定jar包运行的mainclass,并指定生META-INF文件的位置(一般放在resource目录下)4.配置依赖包的存放目录:鼠标右击<outputroot>创建libs文件夹,并将依赖包拖入libs文件夹(注:如果更改了依赖包的位置,classpath中的内容必须做出对应的修改)5.检查各项配置无误选择ok:框选位置依次表示为jar包名;jar输出位置;指定的编译文件,ma

    2022年10月3日
    2
  • python3字典的排序

    python3字典的排序平常学习了字典(dict),感觉还行。但一到用的时候,就感觉模棱两可。于是就总结了字典的常见用法,以后可熟记于心。—————更新日记:2019-05-21通一表述:字典有两个参数,key,value,下面所描述,键:key,值:value欢迎批评指正!—————-…

    2022年6月26日
    24

发表回复

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

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