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


相关推荐

  • OpenClaw话题两周涨15倍,超越Claude

    OpenClaw话题两周涨15倍,超越Claude

    2026年3月15日
    2
  • 二分类变量相关性分析spss_两个有序分类变量相关性的卡方检验-SPSS教程

    二分类变量相关性分析spss_两个有序分类变量相关性的卡方检验-SPSS教程一、问题与数据研究者想探索类风湿关节炎躯体感觉的症状数量与疼痛等级之间的关系,从一家大型医院入院治疗的类风湿关节炎病人中随机招募了364例研究对象。类风湿关节炎躯体感觉共有6种症状,研究者请研究对象报告其患有症状。类风湿关节炎门诊的医生使用疼痛量表对每个研究对象的疼痛进行评级。研究对象自报的类风湿关节炎躯体感觉症状数量在1-6个之间,为有序分类变量,变量名为symptoms。医生将研究对象的疼痛分…

    2022年5月17日
    133
  • VSCode创建Vue项目完整教程

    VSCode创建Vue项目完整教程文章目录一 配置环境 1 安装 VSCode2 安装 node js3 安装配置脚手架 vue cli 二 创建 vue 项目 1 命令方式创建 2 重新初始化依赖 3 启动项目总结一 配置环境 1 安装 VSCode 官网下载 https code visualstudio com 下载 VSCode 按照步骤安装 2 安装 node js 1 官网 https nodejs org en 下载 node js 按照步骤安装即可 node js 安装完成之后会同步安装 npm 2 配置环境变量 把 node j

    2026年3月20日
    2
  • 用python编写猴子吃桃问题_人工智能猴子摘香蕉

    用python编写猴子吃桃问题_人工智能猴子摘香蕉该楼层疑似违规已被系统折叠隐藏此楼查看此楼#include#include#includeintW;/*W-猴子的水平位置*/intx;/*x-当猴子在箱子顶上时取x=1;否则取x=0*/intc;/*c-是香蕉正下方的地板位置*/intY;/*Y-箱子的水平位置*/intz;/*z-当猴子摘到香蕉时取z=1;否则取z=0*/intascll=25;voidGo…

    2026年4月13日
    4
  • 信赖铃音的PS2游戏目录2017.6

    信赖铃音的PS2游戏目录2017.6信赖铃音PS2无损镜像游戏光碟分类目录淘宝最全1600个游戏上架大家扫我加我微信可以直接在微店或者微信红包购买PS2游戏哦!微信号298337705或者直接扫我或者保存图片长按识别二维码就可以加我了!感谢大家支持信赖铃音的无损PS2游戏光碟选好可以直接给我名单拍件数或者直接用购物车选购店址:https://playstation2.taobao.com/PS2搜索信赖铃音PS…

    2025年8月4日
    5
  • 如何零基础开始自学Python编程「建议收藏」

    如何零基础开始自学Python编程「建议收藏」转载——原作者:赛门喵链接:https://www.zhihu.com/question/29138020/answer/1411702420.明确目标我是真正零基础开始学Python的,从一

    2022年7月5日
    24

发表回复

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

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