Cod sursa(job #1527986)

Utilizator alexandru.ghergutAlexandru-Gabriel Ghergut alexandru.ghergut Data 18 noiembrie 2015 22:13:19
Problema Dtcsu Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.6 kb
#include <fstream>
#include <bitset>
using namespace std;

const unsigned long long maxVal = 10e18;
const int maxBits = (1 << 21);
bitset<maxBits> bitHash;
int factors[] = {2, 3, 5, 7, 11};

unsigned int h(unsigned long long x)
{
    int pos = 0, nextBit = 0;
    unsigned int r = 0;
    while (x)
    {
        if (pos % 2 == 0)
        {
            r |= (x & 1) << nextBit;
            nextBit++;
        }
        x >>= 1;
        pos++;
    }
    return r % maxBits;
}

unsigned int g(unsigned long long x)
{
    int pos = 0, nextBit = 0;
    unsigned int r = 0;
    while (x)
    {
        if (pos % 2 != 0)
        {
            r |= (x & 1) << nextBit;
            nextBit++;
        }
        x >>= 1;
        pos++;
    }
    return r % maxBits;
}

bool check(unsigned long long x)
{
    int i;
    for (i = 0; i < 5; i++)
        while (x != 1 && x % factors[i] == 0)
            x /= factors[i];
    return x == 1;
}

unsigned long long next(ifstream &f)
{
    string line;
    getline(f, line);
    unsigned long long result = 0;
    for (int i = 0; i < line.size(); i++)
        result = result * 10 + (line[i] - '0');
    return result;
}

int main()
{
    unsigned long long N;
    int Q, k = 0, i;
    const int entries = 276997;
    ifstream f("dtcsu.in");
    for (i = 0; i < entries; i++)
    {
        N = next(f);
        bitHash[h(N)] = 1;
        bitHash[g(N)] = 1;
    }
    Q = next(f);
    for (i = 0; i < Q; i++)
    {
        N = next(f);
        if (bitHash[h(N)] && bitHash[g(N)] && check(N))
            k++;
    }
    f.close();

    ofstream g("dtcsu.out");
    g << k;
    g.close();
    return 0;
}