D - DZY Loves Hash CodeForces - 447A
Posted 徐义宝
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了D - DZY Loves Hash CodeForces - 447A相关的知识,希望对你有一定的参考价值。
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.
Example
10 5
0
21
53
41
53
4
5 5
0
1
2
3
4
题意:把n个数字放到0~p-1个位置,要求x放在x%p的位置;如果已经放过 输出x的位置,否则输出-1;
(吐槽一下这个题,一直wa就是找不到原因,最后发现原来是没有数组清零,真是太坑了;)
代码:
#include<iostream> #include<cstring> #include<string> #include<sstream> #include<algorithm> using namespace std; int main(){ int n,p,k,flag=0; //char c[301][20]; char m[305]; unsigned long long x; cin>>p>>n; for(int i = 0;i<p;i++) { m[i]=0; }//数组一定要清零, for(int i=0;i<n;i++){ cin>>x; k=x%p; if(m[k]==0) m[k]=1; else if(!flag) flag=i+1; } if(flag)cout<<flag<<endl; else cout<<-1<<endl; }
-1
以上是关于D - DZY Loves Hash CodeForces - 447A的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #FF (Div. 2) A. DZY Loves Hash