Cod sursa(job #1336959)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 8 februarie 2015 14:44:22
Problema Dtcsu Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.62 kb
#include <bits/stdc++.h>

using namespace std;

///------------------------------------------------------
const int BS = 1 << 16;
char buffer[BS];
int position = BS;

inline char getChar()
{
    if ( position == BS )
    {
        position = 0;
        fread( buffer, BS, 1, stdin );
    }

    return buffer[ position++ ];
}

inline long long getNr()
{
    register long long a = 0;
    char ch;

    do
    {
        ch = getChar();

    } while ( !isdigit(ch) );

    do
    {
        a = (a << 3) + (a << 1) + ch - '0';
        ch = getChar();

    } while ( isdigit(ch) );

    return a;
}

struct BloomFilter
{
    static const int NumberOfPrimes = 5;
    static const int Primes[NumberOfPrimes];

    struct BitSet
    {
        int N;
        unsigned long long *b;

        explicit BitSet() : N(0), b(nullptr)
        {
        }

        BitSet(const int _N) : N(_N), b(new unsigned long long[N / 64 + 1])
        {
            for ( int i = 0; i < N / 64 + 1; ++i )
                b[i] = 0;
        }

        void set(const int pos)
        {
            b[pos >> 6] |= ( 1ULL << ( pos & 63 ) );
        }

        bool test(const int pos)
        {
            return b[pos >> 6] & ( 1ULL << ( pos & 63 ) );
        }
    };

    bitset<959473> filter[NumberOfPrimes];

    BloomFilter()
    {
        //for ( int i = 0; i < NumberOfPrimes; ++i )
            //filter[i] = BitSet( Primes[i] );
    }

    inline void insert(const long long key)
    {
        for ( int i = 0; i < NumberOfPrimes; ++i )
            filter[i].set(key % Primes[i]);
    }

    inline bool find(const long long key)
    {
        for ( int i = 0; i < NumberOfPrimes; ++i )
            if ( filter[i].test(key % Primes[i]) == false )
                return false;

        return true;
    }
};

const int BloomFilter::Primes[BloomFilter::NumberOfPrimes] = { 666013, 100003, 826663, 797593, 959473 };

const int N = 276997;
int Q;

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

    long long nr;
    BloomFilter B;

    for ( int i = 1; i <= N; ++i )
    {
        nr = getNr();
        B.insert(nr);
    }

    Q = getNr();
    int sol = 0;

    while ( Q-- )
    {
        nr = getChar();

        bool ok = B.find(nr);

        if ( ok == true )
        {
            nr /= (nr & (-nr));

            while ( (nr / 15) * 15 == nr ) nr /= 15;
            while ( (nr / 3) * 3 == nr ) nr /= 3;
            while ( (nr / 5) * 5 == nr ) nr /= 5;
            while ( (nr / 7) * 7 == nr ) nr /= 7;
            while ( (nr / 11) * 11 == nr ) nr /= 11;

            if ( nr == 1 ) sol++;
        }
    }

    printf("%d\n", sol);

    return 0;
}