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)
上一篇 2022年1月31日 下午7:00
下一篇 2022年1月31日 下午8:00


相关推荐

  • ireport使用教程_layout怎么导入图片

    ireport使用教程_layout怎么导入图片ireport插入图片1.在模板上拖一个image组件,设置它的image Expression为变量$P{logo},如图示,属性下面的is lazy勾上。  不然有可能最后页面渲染出来的image的src为nullimage_0_0_0。2.给变量logo的值。  StringbasePath=request.getScheme()+”://”+requ

    2025年10月20日
    7
  • 计算机保护插件无法安装,电脑无法安装ActiveX控件怎么办「建议收藏」

    计算机保护插件无法安装,电脑无法安装ActiveX控件怎么办「建议收藏」ActiveX控件是网站常用的一款网页辅助工具,有时候我们可能需要安装它,但是却发现浏览器阻止了它安装,那么你知道电脑无法安装ActiveX控件怎么办吗?下面是学习啦小编整理的一些关于电脑无法安装ActiveX控件的相关资料,供你参考。电脑无法安装ActiveX控件的解决方法:1、首先建议将相应网站加入可信站点。2、其次建议选中可信站点。自定义级别——找到“下载未签名的ActiveX控件”——选中…

    2022年5月14日
    99
  • 中小型企业局域网设计

    中小型企业局域网设计中小型企业局域网设计                                                        引言 ………………………………………………………………………………2一、公司简介………………………………………………………………………3二、企业需求分析…………………………………………………………………3三、总体设计方案…………………………………

    2022年7月12日
    20
  • 阻容降压电路计算

    阻容降压电路计算阻容降压电路正确计算将交流市电转换为低压直流的常规方法是采用变压器降压后再整流滤波,当受体积和成本等因素的限制时,最简单实用的方法采用电容降压式电源。上图内容引用网上的。   /****************非常规算法,待验证**********************************/ 看到这里,很多朋友一定想说:不就是阻容么,计算容抗然后电压除以容抗不就成了

    2022年6月20日
    41
  • Java 面试之算法[通俗易懂]

    Java 面试之算法[通俗易懂]二分查找intBinarySearch(DataTypea[],intlow,inthigh,DataTypex){if(low&amp;amp;amp;amp;gt;high){return-1;//查找失败}mid=(low+high)/2;//折半if(a[mid]==x){r…

    2022年7月18日
    24
  • 两地 三中心

    两地 三中心1、两地三中心同城双中心+异地灾备中心,“两地三中心”的灾备模式,方案兼具高可用性和灾难备份的能力。同城双中心是指在同城或邻近城市建立两个可独立承担关键系统运行的数据中心,双中心具备基本等同的业务处理能力并通过高速链路实时同步数据,日常情况下可同时分担业务及管理系统的运行,并可切换运行;灾难情况下可在基本不丢失数据的情况下进行灾备应急切换,保持业务连续运行。与异地灾备模式相比较,同城双中心具有投资成本低、建设速度快、运维管理相对简单、可靠性更高等优点。异地灾备中心是指在异地的城市建立一.

    2022年6月30日
    32

发表回复

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

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