A. DZY Loves Hash

本文介绍了一个简单的哈希表应用案例,通过插入一系列数值并检测哈希冲突来确定第一个发生冲突的位置。使用C++实现了一个基本的哈希表,并演示了如何通过取模运算将数值映射到哈希表中。
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 from0 to p - 1. He wants to insertn numbers, in the order they are given, into the hash table. For thei-th number xi, DZY will put it into the bucket numberedh(xi), whereh(x) is the hash function. In this problem we will assume, thath(x) = x mod p. Operationa mod b denotes taking a remainder after divisiona 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 thei-th insertion, you should output i. If no conflict happens, just output -1.

Input

The first line contains two integers, p andn (2 ≤ p, n ≤ 300). Thenn 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

//水题不多说

//AC代码

#include<iostream>
#include<queue>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
using namespace std;
int main()
{
    int p,n,x,y,j;
    int z[301];
    //while(1)
    //{
        cin>>p>>n;
        map<int,int>Map;
        memset(z,0,sizeof(z));
        j=0;
        for(int i=1;i<=n;i++)
        {
            cin>>x;
            y=x%p;
            Map[y]+=1;
            if(Map[y]>1)
            {
                z[j++]=i;
            }
        }
        if(j!=0)
        {
            cout<<z[0]<<endl;
        }
        else
            cout<<-1<<endl;
    //}
    return 0;
}



 

Process: com.example.dzy, PID: 26008 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dzy/com.example.dzy.NavigationActivity}: java.lang.NullPointerException: Attempt to invoke virtual method &#39;android.content.res.Resources android.content.Context.getResources()&#39; on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method &#39;android.content.res.Resources android.content.Context.getResources()&#39; on a null object reference at com.example.dzy.Fragment_1.<init>(Fragment_1.java:44) at com.example.dzy.NavigationActivity.initTab(NavigationActivity.java:39) at com.example.dzy.NavigationActivity.onCreate(NavigationActivity.java:27) at android.app.Activity.performCreate(Activity.java:7802) at android.app.Activity.performCreate(Activity.java:7791) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
06-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值