Cod sursa(job #1496803)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 5 octombrie 2015 17:18:12
Problema Dtcsu Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.36 kb
#include <fstream>
#include <iostream>
#include <bitset>
#include <array>
#include <cstdio>
#include <functional>
using namespace std;

template <int dim>
class parsator{
	ifstream& f;
	array<char, dim> buf;
	int poz;
public:
	parsator(ifstream& ff): f(ff), poz(0){
		f.read(&buf[0], dim); }
	parsator<dim>& operator>>(int& rez){
		rez = 0;
		while(!isdigit(buf[poz])){
			if(++poz == dim){
				f.read(&buf[0], dim);
				poz = 0; } }
		while(isdigit(buf[poz])){
			rez *= 10;
			rez += buf[poz] - '0';
			if(++poz == dim){
				f.read(&buf[0], dim);
				poz = 0; } }
		return *this; } };

template <int dim>
class bloom_filter{
	bitset<dim> buf;
	hash<int> hs;
public:
	bloom_filter(): buf(0){}
	void insert(int x){
		for(int i = 0; i < 4; ++i, x = hs(x)){
			buf[x%dim] = true; } }
	bool query(int x){
		bool rez = true;
		for(int i = 0; i < 4 && rez; ++i, x = hs(x)){
			if(!buf[x%dim]){
				rez = false; } }
		return rez; } };

constexpr int nr_nr = 276997;
constexpr int factori[] = {2, 3, 5, 7, 11};

int main(){
	ifstream f("dtcsu.in");
	ofstream g("dtcsu.out");
	parsator<1000> ps(f);
	bloom_filter<500000> bf;
	for(int i = 0, x; i < nr_nr; ++i){
		ps >> x;
		bf.insert(x); }
	int q, rez = 0;
	ps >> q;
	for(int i = 0, x; i < q; ++i){
		ps >> x;
		if(bf.query(x)){
			for(const auto y : factori){
				while(x%y == 0){
					x /= y; } } }
		if(x == 1){
			++rez; } }
	g << rez;
	return 0; }