Cod sursa(job #1805351)

Utilizator catu_bogdan_99Catu Bogdan catu_bogdan_99 Data 13 noiembrie 2016 18:08:08
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.36 kb
#include <cstdio>
#include <vector>
using namespace std;

#define MOD 100003

vector < int > v[ MOD + 10 ];
int key;

void add( int x);
void sterge( int x );
vector<int>::iterator cauta( int x );

int putere ( int p, int n ) {
    int x = 1;

    while(p){
        if(p%2){
            x=(x*n)%MOD;
            p--;
        } else{
                n=(n*n)%MOD;
                p/=2;
          }
    }

    return x;

}

int hash32shift (int key) {

    key %= MOD;
    return putere( key, key );

}


int main()
{
    freopen("hashuri.in","r",stdin);
    freopen("hashuri.out","w",stdout);

    int n, i, j, x, y;

    scanf("%d",&n);
    while( n-- ){
        scanf("%d%d",&x,&y);
        key = hash32shift( y );

        if( x == 1 ) add( y );
        else if( x == 2 ) sterge( y );
        else printf( "%d\n",cauta( y ) != v[ key ].end() );
    }

    return 0;

}


vector<int>::iterator cauta( int x )
{
    vector<int>::iterator it;

    for( it = v[ key ].begin(); it != v[ key ].end(); ++it)
        if ( *it == x )
            return it;

    return v[ key ].end();

}

void add( int x )
{

    if( cauta( x ) == v[ key ].end())
        v[ key ].push_back( x );

}

void sterge( int x )
{
    vector<int>::iterator it = cauta( x );

    if( it != v[ key ].end() )
        v[ key ].erase( it );
}