zoukankan      html  css  js  c++  java
  • 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 bHowever, 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

    Input
    10 5
    0
    21
    53
    41
    53
    Output
    4
    Input
    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;
    
    
    
    
    }






     Output
    -1
  • 相关阅读:
    Python中如何取字典中的键值
    Python中random模块的用法案例
    Python中模块import的使用案例
    Python中模块的定义及案例
    Python中from … import …语句
    Python中模块调用说明
    Python中模块、类、函数、实例调用案例
    Python中读写文件三部曲
    Python中特殊函数__str__()
    Python--网络编程-----基于UDP协议的套接字不会发生粘包
  • 原文地址:https://www.cnblogs.com/xuyibao/p/7118980.html
Copyright © 2011-2022 走看看