PAT (Advanced Level) 1078. Hashing (25)

Posted Fighting Heart

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT (Advanced Level) 1078. Hashing (25)相关的知识,希望对你有一定的参考价值。

二次探测法。表示第一次听说这东西。。。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<algorithm>
using namespace std;

const int maxn=20000;
bool flag[maxn];
long long m,n;

bool prime(long long x)
{
    if(x==1) return 0;
    if(x==2) return 1;
    for(long long i=2;i*i<=x;i++)
        if(x%i==0) return 0;
    return 1;
}

int main()
{
    memset(flag,0,sizeof flag);
    scanf("%lld%lld",&m,&n);

    while(1)
    {
        if(prime(m)) break;
        m++;
    }

    for(long long i=1;i<=n;i++)
    {
        long long num; scanf("%lld",&num);

        int p=-1;
        for(long long j=0;j<m;j++){

            if(flag[(num+j*j)%m]==0)
            {
                flag[(num+j*j)%m]=1;
                p=(num+j*j)%m; break;
            }
        }
        if(p==-1) printf("-");
        else printf("%d",p);

        if(i<n) printf(" ");
        else printf("\n");
    }
    return 0;
}

 

以上是关于PAT (Advanced Level) 1078. Hashing (25)的主要内容,如果未能解决你的问题,请参考以下文章

PAT Advanced 1078 Hashing (25) [Hash ?次?探查法]

PAT (Advanced Level) 1025. PAT Ranking (25)

PAT Advanced Level 1044

PAT Advanced Level 1043

PAT Advanced Level 1079

PAT Advanced Level 1095