Cod sursa(job #1034634)

Utilizator robert_dDragan Robert robert_d Data 17 noiembrie 2013 22:58:51
Problema Dtcsu Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.5 kb
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <vector>
using namespace std;

int roots[] = {2, 3, 5, 7, 11};

vector< vector< vector <vector <short int> > > > tree
    (13, vector< vector<vector<short int> > >
        (17, vector<vector<short int> >
            (19, vector <short int>(23, 0))
        )
    );

/*
 * Checks if a number is valid using repeated division.
 * Always works, but is slow.
 */
bool is_valid(long long val) {
    for (unsigned int i=0; i<5; ++i) {
        while (val % roots[i] == 0) {
            val /= roots[i];
        }
    }
    return (val == 1);
}

/*
 * Checks against the tree if the number is invalid.
 * Obs! this function can give false positives.
 */
inline bool is_invalid(long long val) {
    return tree[val % 13][val % 17][val % 19][ val % 23] == 0;
}

int main() {
    // input
    FILE * pFile;
    char mystring [20];

    pFile = fopen ("dtcsu.in" , "r");
    int i = 0, q, sat=0;
    long long val;
    
    // read and build tree
    while (i<276997) {
        fgets (mystring , 100 , pFile);
        val = atoll(mystring);
        tree[val % 13][val % 17][val % 19][ val % 23] = 1;
        ++i;
    }
    
    // start querying
    q = atoi(fgets (mystring , 20 , pFile));
    for (int i=0; i<q; ++i) {
        val = atoll(fgets (mystring , 20 , pFile));
        if (!is_invalid(val) && is_valid(val)) ++sat;
    }
    fclose (pFile);
    
    
    ofstream myfile;
    myfile.open ("dtcsu.out");
    myfile << sat << "\n";
    myfile.close();
    
    return 0;
}