zoukankan      html  css  js  c++  java
  • CF 447A(DZY Loves Hash-简单判重)

    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;
    }
    



  • 相关阅读:
    让美国震惊的10大营销案例
    嵌入式培训为什么选凌阳教育?
    推荐几本互联网行业的经典书目
    谈谈被误解的友情链接交换条件【转】
    20 tips for building modern sites while supporting old versions of IE
    国外PHP大师给初学者的8条建议
    专访许雪松:深入理解嵌入式开发
    周宁:做一个生意之前,请自问自己6个问题
    《时代》百大影响力人物:任正非李开复上榜
    TPL DataFlow初探(二)
  • 原文地址:https://www.cnblogs.com/tlnshuju/p/7047459.html
Copyright © 2011-2022 走看看