PAT 1078 Hashing

Source: Internet
Author: User

# include <stdio.h># include <algorithm># include <string.h># include <iostream># include <    Math.h>using namespace Std;int prime[10100];int vis[10110];int cot;void init_prime () {memset (vis,0,sizeof (VIS));    cot=0;            for (int i=2; i<=10100; i++) {if (!vis[i]) {prime[cot++]=i;        for (int j=i*2; j<=10100; j+=i) vis[j]=1;    }}}int Main () {int n,size1,i,a,ans,j;    int map[10100];        while (~SCANF ("%d%d", &size1,&n)) {init_prime ();        memset (map,0,sizeof (map));                for (i=0; i<cot; i++) {if (prime[i]>=size1) {size1=prime[i];            Break            }} for (i=0; i<n; i++) {scanf ("%d", &a);            Ans=a%size1;                if (!map[ans]) {if (i==0) printf ("%d", ans);    else printf ("%d", ans);            Map[ans]=1; } else/* two-time method of the formula: hi= (H (key) +i*i)%size1 1≤i≤size1-1/= Di=i2 that the probe sequence is d=h (key), d+1^2 , d+2^2,...d+ (size1-1) ^2*/{for (j=1; j<size1; J + +) {int kk= (                    ANS+J*J)%size1;                        if (!map[kk]) {map[kk]=1;                        if (i==0) printf ("%d", KK);                        else printf ("%d", KK);                    Break }} if (J==size1) {if (i==0) printf (                    "-");                else printf ("-");    }}} printf ("\ n"); } return 0;}

PAT 1078 Hashing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.