Cod sursa(job #1363519)

Utilizator Al3ks1002Alex Cociorva Al3ks1002 Data 27 februarie 2015 00:27:20
Problema Ciuperci Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.53 kb
#include<cstdio>
#include<vector>
#include<iostream>

#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second

using namespace std;

const int mod = 666013;
const int hmod = 31;
const int DIM = 10005;

char buff[DIM];
int poz;

void read(ll &x)
{
    x = 0;

    while(buff[poz] < '0' || buff[poz] > '9')
        if(++poz == DIM) fread(buff, 1, DIM, stdin), poz = 0;

    while(buff[poz] >= '0' && buff[poz] <= '9')
    {
        x = x * 10 + buff[poz] - '0';
        if(++poz == DIM) fread(buff, 1, DIM, stdin), poz = 0;
    }
}

vector<pair<ll, int>> H[hmod + 5];

void insert(ll x, int y)
{
    int r = x & hmod;
    H[r].pb(mp(x, y));
}

int find(ll x)
{
    int r = x & hmod;
    for(auto it : H[r])
        if(it.fi == x)
            return it.se;

    return 0;
}

int solve(ll x)
{
    if(x <= 2) return x;

    int ans = find(x);
    if(ans) return ans;

    if(x & 1) ans = (1LL * solve((x - 1) / 2) * solve((x - 1) / 2)) % mod;
    else ans = (2LL * solve(x / 2) * solve((x - 1) / 2)) % mod;

    insert(x, ans);

    return ans;
}

int main()
{
    freopen("ciuperci.in", "r", stdin);
    freopen("ciuperci.out", "w", stdout);

    ll t;
    read(t);

    for(; t; t--)
    {
        ll x;
        read(x);
        printf("%d\n", solve(x));

        for(int i = 0; i < hmod + 5; i++)
            H[i].clear();
    }

    return 0;
}