Cod sursa(job #2939831)

Utilizator andreipirjol5Andrei Pirjol andreipirjol5 Data 14 noiembrie 2022 10:44:35
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.28 kb
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

FILE *fin, *fout;

const int MOD = 666019;
const int B = 31;

vector<int> Hash[MOD + 5];

int hash_value(int n)
{
    int b = 1, s = 0;

    while(n)
    {
        int last = n % 10;

        s = (s + (last * b)) % MOD;

        b = (b * B) % MOD;

        n /= 10;
    }

    return s;
}

int main()
{
    fin = fopen("hashuri.in", "r");
    fout = fopen("hashuri.out", "w");

    int n;
    fscanf(fin, "%d", &n);

    for(int i = 1; i <= n; i++)
    {
        int op, nr;
        fscanf(fin, "%d%d",&op, &nr);

        if(op == 1)
        {
            Hash[hash_value(nr)].push_back(nr);
        }
        else if(op == 2)
        {
            int h = hash_value(nr);

            auto it = find(Hash[h].begin(), Hash[h].end(), nr);

            if(it != Hash[h].end())
                Hash[h].erase(it);
        }
        else
        {
            int h = hash_value(nr);

            auto it = find(Hash[h].begin(),Hash[h].end(), nr);

            if(it != Hash[h].end())
                fprintf(fout, "1");
            else fprintf(fout, "0");

            fprintf(fout, "\n");

        }
    }

    fclose(fin);
    fclose(fout);
    return 0;
}