Cod sursa(job #1598869)

Utilizator tudi98Cozma Tudor tudi98 Data 13 februarie 2016 13:49:58
Problema Ciuperci Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.37 kb
#include <fstream>
#include <utility>
#include <vector>
using namespace std;
#define Mod 666013
#define key first
#define value second

class Hash {
    public:
        void Insert(long long key,int value) {
            H[h(key)].push_back(make_pair(key,value));
        }

        void Clear() {
            for (int i = 0; i < Modulo; i++)
                H[i].clear();
        }

        int getVal(long long key) {
            for (auto i: H[h(key)])
                if (i.key == key) return i.value;
            return -1;
        }

    private:
        static const int Modulo = 503;
        vector< pair<long long,int> > H[Modulo];

        int h(int x) {
            return x % Modulo;
        }
};

Hash H;

int Solve(long long N)         // number of ways to build a binary tree with N nodes
{
    if (N == 1) return 1;
    if (N == 2) return 2;

    int x = H.getVal(N);
    if (x != -1) return x;

    N--;                         // substract root
    x = Solve(N/2);
    if (N&1LL)
        x = 2LL * x * Solve(N/2+1) % Mod;
    else x = 1LL * x * x % Mod;

    H.Insert(N+1,x);
    return x;
}

int main()
{
    ifstream fin("ciuperci.in");
    ofstream fout("ciuperci.out");

    long long N;
    int t;

    fin >> t;
    while (t--)
    {
        H.Clear();
        fin >> N;
        fout << Solve(N) << "\n";
    }
}