Cod sursa(job #2206065)

Utilizator RobybrasovRobert Hangu Robybrasov Data 20 mai 2018 23:01:05
Problema Dtcsu Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.65 kb
#include <bits/stdc++.h>

#define MOD 39119
#define SMAX 16384
#define N 276997

using namespace std;

constexpr const uint8_t D[5] = {2, 3, 5, 7, 11};

struct node {
    uint32_t val;
    node* next;
    node(uint32_t v, node* nn) : val(v), next(nn) {}
};

char s[SMAX];
int p;
FILE *f;
node* H[MOD];

void read(uint64_t &x) {
    x = 0;
    for (; s[p] >= '0' && s[p] <= '9'; ++p) {
        x = 10 * x + s[p] - '0';
        if (p == SMAX - 1) {
            fread(s, 1, SMAX, f);
            p = -1;
        }
    }
    for (; s[p] < '0' || s[p] > '9'; ++p) {
        if (p == SMAX - 1) {
            fread(s, 1, SMAX, f);
            p = -1;
        }
    }
}

inline void insert(uint32_t x) {
    int pos = x % MOD;
    node* it = H[pos];
    for (; it != nullptr && it->val != x; it = it->next);
    if (it == nullptr) {
        node* nn = new node(x, H[pos]);
        H[pos] = nn;
    }
}

inline bool contains(uint32_t x) {
    int pos = x % MOD;
    node* it = H[pos];
    for (; it != nullptr && it->val != x; it = it->next);
    return it != nullptr;
}

inline bool isTrulyMember(uint64_t x) {
    for (int i = 0; i < 5 && x > 1; ++i) {
        for (; x > 1 && x % D[i] == 0; x /= D[i]);
    }
    return x == 1;
}

int main() {
    f = fopen("dtcsu.in", "r");
    FILE* g = fopen("dtcsu.out", "w");

    fread(s, 1, SMAX, f);
    p = 0;

    for (int i = 0; i < N; ++i) {
        uint64_t x;
        read(x);
        insert((uint32_t) x);
    }
    uint64_t n;
    read(n);
    int k = 0;
    while (n--) {
        uint64_t x;
        read(x);
        if (contains((uint32_t) x) && isTrulyMember(x)) {
            ++k;
        }
    }
    fprintf(g, "%d\n", k);

    return 0;
}