HDU OJ 4334 Trouble 【hash】

原題連接:http://acm.hdu.edu.cn/showproblem.php?pid=4334

思路:利用hash 離散化,解決衝突方法爲+1;

AC代碼:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;
const __int64 inf=1e15+1;
const int Max=900000;
__int64 ans[7][208];
__int64 Hash[Max+10];
bool loop[Max+10];
int main()
{
    int a,b,c,n,m;
    __int64 k;
    scanf("%d",&m);
    while(m--)
    {
        int ok=0;
        memset(loop,0,sizeof(loop));
        scanf("%d",&n);
        for(a=1;a<=5;a++)
            for(b=1;b<=n;b++)
            {
                scanf("%I64d",&ans[a][b]);
            }
        for(a=1;a<=n;a++)
            for(b=1;b<=n;b++)
            {
                __int64 x= (ans[1][a]+ans[2][b]);
                __int64 y=abs(x%Max);
                for(int i=y;;i++)
                {
                    if(i>=Max)
                        i=i%Max;
                    if(loop[i]==true)
                    {
                        if(Hash[i]==x)
                            break;
                        else
                          continue;
                    }
                    else
                    {
                        loop[i]=true;
                        Hash[i]=x;
                        break;
                    }
                }
            }
        for(a=1;a<=n;a++)
        {
            for(b=1;b<=n;b++)
            {
                for(c=1;c<=n;c++)
                {
                    __int64 x= ans[3][a]+ans[4][b]+ans[5][c];
                    __int64 y=abs(x%Max);
                    for(int i=y;;i++)
                    {
                        if(i>=Max)
                            i=i%Max;
                        if(loop[i]==0)
                            break;
                        else
                        {
                            if(Hash[i]==-x)
                            {
                                ok=1;
                                break;
                            }
                        }
                        if(ok)
                            break;
                    }
                    if(ok)
                        break;
                }
                if(ok)
                    break;
            }
            if(ok)
                break;
        }
        if(ok)
            printf("Yes\n");
        else
            printf("No\n");
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章