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

题目大意:有一堆数扔进Hash表。求第一次出现2个数在1个格子中的情况

模拟

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (300+10)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
int p,n;
bool b[MAXN];
int main()
{
// freopen("DZY Loves Hash.in","r",stdin);
// freopen(".out","w",stdout); cin>>p>>n;
MEM(b)
For(i,n)
{
int x;
cin>>x;
x%=p;
if (b[x]) {cout<<i<<endl; return 0;}
else b[x]=1;
}
cout<<"-1"<<endl; return 0;
}

CF 447A(DZY Loves Hash-简单判重)的更多相关文章

  1. CF A. DZY Loves Hash

    A. DZY Loves Hash time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  2. [CodeForces - 447A] A - DZY Loves Hash

    A - DZY Loves Hash DZY has a hash table with p buckets, numbered from 0 to p - 1. He wants to insert ...

  3. Codeforces Round #FF (Div. 2):Problem A - DZY Loves Hash

    A. DZY Loves Hash time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  4. CF447A DZY Loves Hash 模拟

    DZY has a hash table with p buckets, numbered from 0 to p - 1. He wants to insert n numbers, in the ...

  5. BFS以及hash表判重的应用~

    主要还是讲下hash判重的问题吧 这道题目用的是除法求余散列方式 前几天看了下算法导论 由于我们用的是线性再寻址的方式来解决冲突问题 所以hash表的大小(余数的范围)要包含我们要求的范围 对mod的 ...

  6. CF 444C DZY Loves Physics(图论结论题)

    题目链接: 传送门 DZY Loves Chemistry time limit per test1 second     memory limit per test256 megabytes Des ...

  7. CF 445B DZY Loves Chemistry(并查集)

    题目链接: 传送门 DZY Loves Chemistry time limit per test:1 second     memory limit per test:256 megabytes D ...

  8. CF 444A(DZY Loves Physics-低密度脂蛋白诱导子图)

    A. DZY Loves Physics time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9. CF 444B(DZY Loves FFT-时间复杂度)

    B. DZY Loves FFT time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

随机推荐

  1. SVN系列学习(三)-TortoiseSVN的基本操作

    1.添加(Add) 在ZJHZXS_01中,新建一个记事本,在记事本中写上一下内容,然后保存,再打开,再保存 这个时候,在选中文件夹ZJHZXS_01,并右击[SVN Commit] 提交成功,加了一 ...

  2. 浅谈Java中的hashcode方法以及equals方法

    哈希表这个数据结构想必大多数人都不陌生,而且在很多地方都会利用到hash表来提高查找效率.在Java的Object类中有一个方法: public native int hashCode(); 根据这个 ...

  3. Java—break跳出语句

    在开发代码时,常常会产生这样的疑惑:break跳出语句是如何应用的呢? 使用break的场景有两种:一.switch语句中.二.循环语句. 这里就不介绍switch语句,主要说一下break在循环中的 ...

  4. CSS——行内元素的margin与padding

    行内元素: 1.margin:0 20px:只可以定义左右. 2.pading:20px 20px 20px 20px:上下左右都有效 例如span: <!DOCTYPE html> &l ...

  5. VS2013支持多字节

    使用插件 下载地址:https://www.microsoft.com/zh-cn/search/DownloadResults.aspx?rf=sp&q=mbcs

  6. c#仿照qq登录界面编辑框内容操作

    using System; using System.Drawing; using System.Windows.Forms; namespace 案例演示 { public partial clas ...

  7. picturebox中添加图片

    private void Form1_Load(object sender, EventArgs e) { radioButton2.Checked = true; } private void ra ...

  8. Java类及成员

    Java类及成员 类 类是对一类事物的的描述,是抽象的概念上的定义:类是创建对象的模板: public class TestClass { public static void main(String ...

  9. BZOJ 2276: [Poi2011]Temperature 单调队列

    Code: #include<bits/stdc++.h> #define maxn 3000000 using namespace std; void setIO(string s) { ...

  10. linu下nginx的安装

    这里用到的环境是nginx-1.8.0,linux用的是CentOS-7-x86_64-DVD-1804.iso版本 1   什么是nginx Nginx ("engine x") ...