Cod sursa(job #1363528)

Utilizator mirceadinoMircea Popoveniuc mirceadino Data 27 februarie 2015 00:35:38
Problema Ciuperci Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.78 kb
#include<algorithm>
#include<bitset>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<deque>
#include<fstream>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>

using namespace std;

#define dbg(x) (cout<<#x<<" = "<<(x)<<'\n')
#ifdef HOME
const string inputFile = "input.txt";
const string outputFile = "output.txt";
#else
const string problemName = "ciuperci";
const string inputFile = problemName + ".in";
const string outputFile = problemName + ".out";
#endif

typedef long long int lld;
typedef pair<int, int> PII;
typedef pair<int, lld> PIL;
typedef pair<lld, int> PLI;
typedef pair<lld, lld> PLL;

const int MOD = 666013;
const int HASH = 1009;

int Q;
lld N;
vector<PII> H[HASH + 5];

int find(int a, lld b) {
    int r = b % HASH;
    for(auto x : H[r])
        if(x.first == a)
            return x.second;
    return -1;
}

void insert(int a, lld b, int val) {
    int r = b % HASH;
    H[r].push_back(make_pair(a, val));
}

int solve(int exp_full, lld n) {
    if(n == 1)
        return (1LL << exp_full) % MOD;

    if(n == (1LL << exp_full))
        return 1;

    int f = find(exp_full, n);

    if(f != -1)
        return f;

    int ans = ((1 + n % 2) * solve(exp_full - 1, n / 2) * 1LL * solve(exp_full - 1, n - n / 2)) % MOD;

    insert(exp_full, n, ans);

    return ans;
}

int main() {
    lld p, a, b;

#ifndef ONLINE_JUDGE
    freopen(inputFile.c_str(), "r", stdin);
    freopen(outputFile.c_str(), "w", stdout);
#endif

    scanf("%d", &Q);

    while(Q--) {
        scanf("%lld", &N);
        p = log2(N + 0.5);
        a = (1LL << p);
        b = N - a + 1;
        printf("%d\n", solve(p, b));
    }

    return 0;
}