Cod sursa(job #1034342)

Utilizator mihai995mihai995 mihai995 Data 17 noiembrie 2013 19:40:27
Problema Dtcsu Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.69 kb
#include <fstream>
#include <cstring>
using namespace std;

template<const int Size, const int nrHash>
class BloomFilter{
    unsigned char B[1 + (Size >> 3)];

    inline void setBit(unsigned long long x){
        x &= Size;
        B[x >> 3] |= 1 << (x & 7);
    }

    inline bool getBit(unsigned long long x){
        x %= Size;
        return B[x >> 3] & ( 1 << (x & 7) );
    }

    inline unsigned long long hash(unsigned long long x, unsigned long long i){
        return x + i * i * i;
    }

public:
    BloomFilter(){
        memset(B, 0, sizeof(B));
    }
    void update(unsigned long long x){
        for (unsigned long long i = 0 ; i < nrHash ; i++)
            setBit( hash(x, i) );
    }

    bool query(unsigned long long x){
        for (unsigned long long i = 0 ; i < nrHash ; i++)
            if (!getBit( hash(x, i) ))
                return false;
        return true;
    }
};

BloomFilter<4682861, 90> B;
const int buffSize = 1 << 17, putere[] = {5, 2, 3, 5, 7, 11};
char buff[buffSize];

void cleanup(unsigned long long& x, unsigned long long p){
    if (x < p) return;

    if (p < 0x77777777)
        cleanup(x, p * p);

    unsigned long long a = x / p;
    if (a * p == x)
        x = a;
}

bool okay(unsigned long long x){
    for (int i = 1 ; i <= putere[0] ; i++)
        cleanup(x, putere[i]);
    return x == 1;
}

ifstream in("dtcsu.in");
ofstream out("dtcsu.out");

int main(){
    int times = 276997, ans = 0;
    long long x;

    while (times--)
        in.getline(buff, buffSize);

    in >> times;
    while (times--){
        in >> x;
        ans += okay(x);
    }

    out << ans << "\n";
    return 0;
}