Cod sursa(job #2325876)

Utilizator manciu_ionIon Manciu manciu_ion Data 23 ianuarie 2019 00:23:12
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.28 kb
#include <iostream>
#include <fstream>
#include <vector>

#define InputFile "hashuri.in"
#define OutputFile "hashuri.out"

using namespace std;

const int kBase = 1000033;
const int kHashKey = 131071;

std::vector< int > hashes[kHashKey + 1];

bool IsPrime(int number);
bool add(int value);
void remove(int value);
bool HasValue(int value);
inline bool IsDivisor(int number, int divisor);
inline int GetHashKey(int number, int base, int hash);

ifstream fin(InputFile);
ofstream fout(OutputFile);

int main() {

    //(IsPrime((1 << 17) - 1) == true)?std::cout << "true":std::cout<< "false";

    int numberofoperations;
    int tipoperation;
    int value;

    fin >> numberofoperations;

    while (numberofoperations > 0){
        fin >> tipoperation >> value;

        switch (tipoperation){
            case 1: add(value);
                break;
            case 2: remove(value);
                break;
            case 3: (HasValue(value) == true)? fout << "1\n": fout << "0\n";
                break;
        }
        --numberofoperations;
    }

    return 0;
}

inline int GetHashKey(int number, int base, int hash){
    return (number * base) & hash;
}

void remove(int value){
    int key = GetHashKey(value, kBase, kHashKey);
    int indf = (int)hashes[key].size() - 1;
    for (int ind = 0; ind <= indf; ++ind){
        if (hashes[key][ind] == value){
            hashes[key][ind] = hashes[key][indf];
            hashes[key][indf] = -1;
            return;
        }
    }
}

bool HasValue(int value){
    int key = GetHashKey(value, kBase, kHashKey);
    for (auto &hash: hashes[key]){
        if (hash == value){
            return true;
        }
    }

    return false;
}

bool add(int value){
    int key = GetHashKey(value, kBase, kHashKey);
    for (auto &hahs: hashes[key]){
        if (hahs == value){
            return false;
        }else if (hahs == -1){
            hahs = value;
            return true;
        }
    }

    hashes[key].push_back( value );

    return true;
}

bool IsPrime(int number){
    std::cout << number << "\n";
    for(int divisor = 2; divisor * divisor <= number; divisor++){
        if ( IsDivisor(number, divisor) == true ){
            return false;
        }
    }
    return true;
}

inline bool IsDivisor(int number, int divisor){
    return  number % divisor == 0;
}