Cod sursa(job #1029068)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 14 noiembrie 2013 23:06:12
Problema Dtcsu Scor Ascuns
Compilator cpp Status done
Runda Marime 3.02 kb
#include <cstdio>
#include <cassert>

#include <bitset>

using namespace std;

typedef long long int64;

const char IN_FILE[] = "dtcsu.in";
const char OUT_FILE[] = "dtcsu.out";
const int K = 276997;
const int MAX_BITS = 1024 * 1024 * 15 / 2;
const int U_COUNT = 5;
const int MAX_U = MAX_BITS / U_COUNT;
const int U[U_COUNT] = {666013, 1000003, 826663, 797593, 959473};

class Reader {
  public:
    Reader(FILE *stream, const int size = (1 << 16)):
            size(size),
            pointer(0),
            stream(stream) {
        buffer = new char[size];
        assert(fread(buffer, 1, size, this->stream) != 0);
    }

    template<class IntType>
    IntType NextInt() {
        IntType value = 0;
        bool negative = false;
        while ((Current() < '0' || Current() > '9') && Current() != '-')
            NextPosition();
        if (Current() == '-') {
            negative = true;
            NextPosition();
        }
        while(Current() >= '0' && Current() <= '9') {
            value = value * 10 + Current() - '0';
            NextPosition();
        }
        if (negative)
            value = -value;
        return value;
    }

    Reader &operator>>(short &value) {
        value = NextInt<short>();
        return *this;
    }

    Reader &operator>>(int &value) {
        value = NextInt<int>();
        return *this;
    }

    Reader &operator>>(long long &value) {
        value = NextInt<long long>();
        return *this;
    }

  private:
    int size;
    int pointer;
    char *buffer;
    FILE *stream;

    char Current() const {
        return buffer[pointer];
    }

    void NextPosition() {
        if(++pointer == size) {
            assert(fread(buffer, 1, size, stream) != 0);
            pointer = 0;
        }
    }
};

class Hash {
  public:
    Hash() {}

    void Insert(const int64 value) {
        for (int i = 0; i < U_COUNT; ++i)
            used[i][value % U[i]] = true;
    }

    bool Find(const int64 value) const {
        for (int i = 0; i < U_COUNT; ++i)
            if (!used[i][value % U[i]])
                return false;
        return true;
    }

  private:
    bitset<MAX_U> used[U_COUNT];
};

int main() {
    assert(freopen(IN_FILE, "r", stdin));
    assert(freopen(OUT_FILE, "w", stdout));
    Reader in = Reader(stdin);
    Hash hash;
    for (int i = 0; i < K; ++i) {
        int64 value;
        in >> value;
        hash.Insert(value);
    }
    int tests, count = 0;
    in >> tests;
    for (; tests > 0; --tests) {
        int64 value;
        in >> value;
        if (!hash.Find(value))
            continue;
        value /= (value & (-value));
        for (; (value / 15) * 15 == value; value /= 15);
        for (; (value / 3) * 3 == value; value /= 3);
        for (; (value / 5) * 5 == value; value /= 5);
        for (; (value / 7) * 7 == value; value /= 7);
        for (; (value / 11) * 11 == value; value /= 11);
        if (value == 1)
            ++count;
    }
    printf("%d\n", count);
    return 0;
}