Pagini recente » Cod sursa (job #82615) | Cod sursa (job #1274970) | Cod sursa (job #2949116) | Cod sursa (job #2107602) | Cod sursa (job #1034634)
#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;
}