Cod sursa(job #678674)

Utilizator ion_calimanUAIC Ion Caliman ion_caliman Data 12 februarie 2012 11:08:26
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.11 kb
#include <fstream>
using namespace std;

ifstream f("hashuri.in");
ofstream g("hashuri.out");

#define M 1666013

int A[M], N, pr[100];

int maxim=0;

void eratostene(int n)
{
    int nr=0, i, j;
    for (i=2; i<n; i++) pr[i] = 1;
    for (i=2; i*i<n; i++)
        if (pr[i])
            for (j=i; j<n; j+=i) pr[j] = 0;
    for (i=2; i<n; i++)
        if (pr[i]) pr[nr++] = i;
}

int H(int x, int p)
{
    return ((x%(M-p) + p)*pr[p] + p) % M;
}

void insereaza(int x)
{
    int p=0;
    while (A[H(x,p)]>0 && A[H(x,p)]!=x) p++;

    //if (p>maxim) maxim=p;

    A[H(x,p)] = x;
}

void sterge(int x)
{
    int p=0;
    while ((A[H(x,p)] || A[H(x,p)]==-1) && A[H(x,p)]!=x) p++;
    A[H(x,p)] = -1;
}

bool cauta(int x)
{
    int p=0;
    while ((A[H(x,p)] || A[H(x,p)]==-1) && A[H(x,p)]!=x) p++;
    return A[H(x,p)] == x;
}

int main()
{
    eratostene(100);
    int c,x;
    f >> N;
    for (;N--;){
        f >> c >> x;
        if (c==1) insereaza(x); else
        if (c==2) sterge(x); else
        if (c==3) g << cauta(x) << '\n';
    }
    //g << maxim;
    return 0;
}