Cod sursa(job #1036508)

Utilizator goguGogu Marian gogu Data 19 noiembrie 2013 13:57:42
Problema Dtcsu Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.46 kb
#include <cstdio>
#include <bitset>
#include <unordered_set>
#include <vector>
#include <ctime>
#include <cstring>
 
using namespace std;
 
#define NR_GOOD 276997
#define MOD1 ((2 << 20)  + 1)
 
std::bitset<MOD1> posOk;

char readNextCh() {
   static int DIM = 1 << 14;
   static char *buf = new char[DIM];
   static int poz = DIM;

   if (poz >= DIM) {
      fread(buf, 1, DIM, stdin);
      poz = 0;
   }

   return buf[poz++];
}
 
unsigned long long cit() {
   unsigned long long val = 0;
   int poz = 0, temp = 0, cnt = 0;
   const int pow10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
   
   static char ch = 0;
   while (ch < '0' || ch > '9') {
      ch = readNextCh();
   }
   
   while (ch >= '0' && ch <= '9') {
      temp = temp * 10 + (ch - '0');
      cnt++;
      if (cnt == 9) {
         val = val * pow10[cnt] + temp;
         cnt = 0;
         temp = 0;
      }
      ch = readNextCh();
   }
   return val * pow10[cnt] + temp;
}
 
unordered_set<unsigned long long> seen;
 
bool isGood(unsigned long long val) {
//   unsigned long poz;
//#ifdef WIN32
//   _BitScanForward64(&poz, val);
//#else
//   poz = ffsll(val) - 1;
//#endif
//   val = val >> poz;
   
   val /= (val & (-val));

   //while (val % 2 == 0)  val /= 2;
   return seen.find(val) != seen.end();
}
 
void genAll(unsigned long long num) {
   int a[] = {3, 5, 7, 11};
   if (seen.find(num) != seen.end()) {
      return;
   }
   seen.insert(num);
   for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++) {
      unsigned long long valMax = 1000 * 1000 * 1000;
      valMax *= valMax;
      if (valMax / a[i] >= num) {
         genAll(num * a[i]);
      }
   }
}

unsigned myHash(unsigned long long val) {
   val ^= (val >> 22) ^ (val >> 44);
   return val % MOD1;
}
 
int main() {
   double start = clock();
   freopen("dtcsu.in", "rb", stdin);
   freopen("dtcsu.out", "wb", stdout);
   unsigned long long good;
   for (int i = 0; i < NR_GOOD; i++) {
      good = cit();
      posOk[myHash(good)] = true;
   }
   genAll(1);
   fprintf(stderr, "Sizeof table: %d\n",(int)seen.size());
   int nrSol = 0, nrQ;
   nrQ = (int)cit();
   for (int i = 0; i < nrQ; i++) {
      unsigned long long val = cit();
      if (posOk[myHash(val)] && isGood(val)) {
         nrSol++;
      }
   }
   printf("%d\n", nrSol);
   fprintf(stderr, "Duration: %.3f sec\n", (clock() - start) / CLOCKS_PER_SEC);
   return 0;
}