Cod sursa(job #1294465)

Utilizator IonSebastianIon Sebastian IonSebastian Data 17 decembrie 2014 17:05:19
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include <cstdio>

using namespace std;

const int MAX_N = 1000000;
const int p1 = 666013, p2 = 300007;

FILE *in, *out;

int h[MAX_N];

void adauga(int x)
{
    int poz = x%p1, m = x%p2;
    while(h[poz] > 0)
    {
        poz += m;
        if(poz >= MAX_N)
            poz -= MAX_N;
    }
    h[poz] = x;
}

int exista(int x)
{
    int poz = x%p1, m = x%p2;
    while(h[poz] != 0)
    {
        if(h[poz] == x) return poz;
        poz += m;
        if(poz >= MAX_N)
            poz -= MAX_N;
    }
    return -1;
}

void sterge(int x)
{
    int poz = exista(x);
    if(poz != -1)
        h[poz] = -1;
}

int main()
{
    in = fopen("hashuri.in", "r");
    out = fopen("hashuri.out", "w");
    int n, x, t;
    fscanf(in, "%d", &n);
    while(n > 0)
    {
        fscanf(in, "%d%d", &t,&x);
        if(t == 1)
            adauga(x);
        else if(t == 2)
            sterge(x);
        else
            fprintf(out, "%c\n", (exista(x)==-1)?'0':'1');
        n--;
    }
    return 0;
}