Cod sursa(job #907733)

Utilizator aladinaladin aladinn aladin Data 8 martie 2013 11:36:10
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.05 kb
#include <cstdio>
#define MOD 666013

inline int newpoz(int poz)
{
	return (poz*5+1)&MOD;
}	

inline int insert(int *hh,int &x)
{
	int poz=x & MOD,steps=0;
	while ((hh[poz]!=0) && (hh[poz]!=x) && (steps<MOD))
		poz=newpoz(poz),++steps;
	if (steps>=MOD)
		return 0;
	hh[poz]=x;
	return poz;
}

inline int del(int *hh,int &x)
{
	int poz=x & MOD,steps=0;
	while ((hh[poz]!=0) && (hh[poz]!=x) && (steps<MOD))
		poz=newpoz(poz),++steps;
	if (steps>=MOD)
		return 0;
	if (hh[poz]!=x) return -1; 
	hh[poz]=-1;
	return poz;
}

inline int getpoz(int *hh, int x)
{
	int poz=x & MOD, steps = 0;
	while ((hh[poz]!=0) && (hh[poz]!=x) && (steps<MOD))
		poz=newpoz(poz),++steps;
	if (hh[poz]!=x) return 0;
	return poz;
}
	
	
	
	

int main()
{
	int t,x,tip;
	int *hh = new int[MOD+1];
	
	freopen("hashuri.in","r",stdin);
	freopen("hashuri.out","w",stdout);
	scanf("%d",&t);
	while (t--)
	{
		scanf("%d %d",&tip,&x);
		if (tip==1) insert(hh,x); else
			if (tip==2) del(hh,x); else
				printf("%d\n",getpoz(hh,x)>0);
	}
	return 0;
}