Cod sursa(job #1104621)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 10 februarie 2014 21:40:32
Problema Ciuperci Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.11 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <bitset>
#include <string.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <deque>

using namespace std;

const char infile[] = "ciuperci.in";
const char outfile[] = "ciuperci.out";

ofstream fout(outfile);

const int MAXN = 100005;
const int MOD = 666013;
const int U = 31;
const int oo = 0x3f3f3f3f;

typedef vector<int> Graph[MAXN];
typedef vector<int> :: iterator It;

const inline int min(const int &a, const int &b) { if( a > b ) return b;   return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b;   return a; }
const inline void Get_min(int &a, const int b)    { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b)    { if( a < b ) a = b; }

long long N;
vector <pair<long long, int> > Hash[U];

class Scanner {
  public:
    Scanner(string file, int buffer_size = 32768):
            buffer_size_(buffer_size) {
        file_ = fopen(file.c_str(), "r");
        buffer_ = new char[buffer_size];
        pointer_ = buffer_ + buffer_size_;
    }

    Scanner& operator>>(long long &object) {
        object = 0;
        while (next() < '0' or next() > '9')
            advance();
        while (next() >= '0' and next() <= '9') {
            object = object * 10 + next() - '0';
            advance();
        }
        return *this;
    }

  private:
    char next() {
        if (pointer_ == buffer_ + buffer_size_) {
            pointer_ = buffer_;
            fread(buffer_, 1, buffer_size_, file_);
        }
        return *pointer_;
    }

    void advance() {
        ++pointer_;
    }

    FILE *file_;
    int buffer_size_;
    char *buffer_, *pointer_;
};
Scanner fin(infile);

inline int SearchHash(const long long & x) {
    int Key = (x % U);
    for(vector <pair<long long, int> > :: iterator it = Hash[Key].begin(), fin = Hash[Key].end() ; it != fin ; ++ it)
        if(it->first == x)
            return it->second;
    return -1;
}

inline void AddHash(const pair<long long, int> & x) {
    int Key = N % U;
    for(vector <pair<long long, int> > :: iterator it = Hash[Key].begin(), fin = Hash[Key].end(); it != fin ; ++ it)
        if(it->first == x.first) {
            it->second += x.second;
            return;
        }
    Hash[Key].push_back(x);
}

int Solve(const long long &x) {
    if(x <= 2)
        return x;
    int val = SearchHash(x);
    if(val != -1)
        return val;
    if(x & 1) {
        val = Solve((x - 1) >> 1);
        val = (1LL * val * val) % MOD;
    }
    else
        val = (1LL * Solve((x - 1) >> 1) * Solve(((x - 1) >> 1) + 1) << 1) % MOD;
    AddHash(make_pair(x, val));
    return val;
}

int main() {
    long long T;
    fin >> T;
    while(T --) {
        fin >> N;
        fout << Solve(N) << '\n';
        for(int i = 0 ; i < U ; ++ i)
            vector< pair<long long, int> > ().swap(Hash[i]);
    }
    //fin.close();
    fout.close();
    return 0;
}