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


相关推荐

  • 《开源安全运维平台-OSSIM最佳实践》已经上市

    《开源安全运维平台-OSSIM最佳实践》已经上市经多年潜心研究开源技术,历时三年创作的《开源安全运维平台OSSIM最佳实践》一书即将出版。该书用100多万字记录了作者10多年的OSSIM研究应用成果,重点展示了开源安全管理平台OSSIM在大型企业网运维管理中的实践。国内目前也有各式各样的运维系统,经过笔者对比分析得出这些工具无论在功能上、性能上还是在安全和稳定性易用性上都无法跟OSSIM系统想媲美,而且很多国内的开源安全运维项目在发布几年后就逐步淡出了舞台,而OSSIM持续发展了十多年。

    2022年10月25日
  • web UI自动化之PO模式

    web UI自动化之PO模式PO是什么:PO模式,PageObject的缩写,页面对象,设计框架的思想,分层思想在PO下,应用程序的每一个页面都有一个对应的pageclass每一个pageclass维护着该web页的元素集和操作这些元素的方法pageclass中的方法命名最好根据对应的业务场景进行,例如通常登录后我们需要等待几秒钟PO的优势:PO提供了一种业务流程与页面元素操作分离的模式,这使得测试代码变得更加清晰页面对象与用例分离,使得我们更好的复用对象可复用的页面方法代码会变得更加优化更加有效的命名

    2022年6月3日
  • H3C交换机、路由器常用配置命令大全

    H3C交换机、路由器常用配置命令大全H3C交换机配置命令大全1、system-view进入系统视图模式2、sysname为设备命名3、displaycurrent-configuration当前配置情况4、language-modeChinese|English中英文切换5、interfaceEthernet1/0/1进入以太网端口视图6、portlink-ty

    2022年6月20日
  • 线程join方法用处「建议收藏」

    线程join方法用处「建议收藏」参考博客:https://www.cnblogs.com/lcplcpjava/p/6896904.html第一种情况(不使用join):ThreadJoinTestt1=newThreadJoinTest(“小明”);ThreadJoinTestt2=newThreadJoinTest(“小东”);t1.start();

    2022年5月24日
  • rwx

    rwx常用的linux文件权限:444r–r–r–600rw——-644rw-r–r–666rw-rw-rw-700rwx——744rwxr–r–755rwxr-xr-x777rwxrwxrwx从左至右,1-3位数字代表文件所有者的权限,4-6位数字代表同组用户的权限,7-9数字代表其他用户的权限。而具体的权限是由数字来表示的,读取的权限等于4,用r表示;写…

    2022年6月13日

发表回复

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

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