Cod sursa(job #1336963)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 8 februarie 2015 14:46:37
Problema Dtcsu Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.6 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);
        long long X = nr;
        if ( ok )
        {
            X /= (X&(-X));
            for ( ; (X/15)*15 == X ; X /= 15 );
            for ( ; (X/3)*3 == X ; X /= 3 );
            for ( ; (X/5)*5 == X ; X /= 5 );
            for ( ; (X/7)*7 == X ; X /= 7 );
            for ( ; (X/11)*11 == X ; X /= 11 );
            if ( X == 1 ) ++sol;
        }
    }

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

    return 0;
}