CF 447A(DZY Loves Hash-简单判重)
1 second
256 megabytes
standard input
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.
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 a single integer — the answer to the problem.
10 5
0
21
53
41
53
4
5 5
0
1
2
3
4
-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-简单判重)的更多相关文章
- CF A. DZY Loves Hash
A. DZY Loves Hash time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- [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 ...
- 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 ...
- 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 ...
- BFS以及hash表判重的应用~
主要还是讲下hash判重的问题吧 这道题目用的是除法求余散列方式 前几天看了下算法导论 由于我们用的是线性再寻址的方式来解决冲突问题 所以hash表的大小(余数的范围)要包含我们要求的范围 对mod的 ...
- CF 444C DZY Loves Physics(图论结论题)
题目链接: 传送门 DZY Loves Chemistry time limit per test1 second memory limit per test256 megabytes Des ...
- CF 445B DZY Loves Chemistry(并查集)
题目链接: 传送门 DZY Loves Chemistry time limit per test:1 second memory limit per test:256 megabytes D ...
- CF 444A(DZY Loves Physics-低密度脂蛋白诱导子图)
A. DZY Loves Physics time limit per test 1 second memory limit per test 256 megabytes input standard ...
- CF 444B(DZY Loves FFT-时间复杂度)
B. DZY Loves FFT time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
随机推荐
- Linq学习(零)-错误汇总
问题一: Cannot execute text selection: CS0009 Metadata file 'C:\Users\Kimisme\Documents\LINQPad Plugins ...
- 转 使用Hibernate操作数据库时报:No CurrentSessionContext configured! 异常
没有currentSession配置错误,即在我们使用currentSession的时候要在hibernate.cfg.xml中进行相关的事务配置:1.本地事务<property name=&q ...
- css+background实现 图片宽高自适应,拉伸裁剪不变形
图片宽高不固定 ,一样实现自适应,拉伸裁剪不变形,适应各大兼容性. 下面咱们在网上找两张宽高不一样的照片: No.1 ...
- JS高级——词法作用域
作用域 1.js中没有块级作用域 2.如果有块级作用域,那么下面代码将会是undefined undefined <script> for (var i = 0; i < 10; i ...
- Git与SVN版本控制系统
关于版本控制 什么是版本控制?版本控制是一种记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的系统.在本书所展示的例子中,我们仅对保存着软件源代码的文本文件作版本控制管理,但实际上,你可以对任 ...
- Java中IO对象的输入输出流
输入流: public void inputDemo () throws IOException { //文件名称 String fileName = "d:\\aaa.txt"; ...
- c#中通过事件实现按下回车跳转控件
//接受用户输入参数后回车事件 private void tb_KeyPress(object sender, KeyPressEventArgs e) { ) { SendKeys.Send(&qu ...
- ie9以下提示用户升级浏览器
<!--[if lt IE 9]> <div style='border: 4px solid #FFF500; background: #FDFDC8; text-align: c ...
- 《hello-world》第八次团队作业:Alpha冲刺-Scrum Meeting 2
项目 内容 这个作业属于哪个课程 2016级计算机科学与工程学院软件工程(西北师范大学) 这个作业的要求在哪里 实验十二 团队作业8:软件测试与Alpha冲刺 团队名称 <hello--worl ...
- Mysql学习总结(40)——MySql之Select用法汇总
一.条件筛选 1.数字筛选:sql = "Select * from [sheet1$] Where 销售单价 > 100" 2.字符条件:sql = "Selec ...