Cod sursa(job #493479)

Utilizator AndreyPAndrei Poenaru AndreyP Data 18 octombrie 2010 14:27:37
Problema Fractal Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <cstdio>
#include <algorithm>
using namespace std;
#define pii pair< int,int >
#define fs first
#define sc second
#define mp make_pair

const int steps[3][3] = { {0,0,0} , {0,0,3} , {0,1,2} };

pii rezolva(int k,int x,int y) {
    if(k==1)
        return mp(steps[x][y],3);

    int jum=1<<(k-1);
    pii aux;

    if(x<=jum) {
        if(y<=jum) {
            aux = rezolva(k-1,y,x);
            return mp(aux.fs,(aux.sc<<2)+3);
        }

        y -= jum;
        aux = rezolva(k-1,(1<<(k-1))-y+1,(1<<(k-1))-x+1);
        return mp(aux.fs+3*aux.sc+3,(aux.sc<<2)+3);
    }

    x -= jum;
    if(y<=jum) {
        aux = rezolva(k-1,x,y);
        return mp(aux.fs+aux.sc+1,(aux.sc<<2)+3);
    }

    y -= jum;
    aux = rezolva(k-1,x,y);
    return mp(aux.fs+(aux.sc<<1)+2,(aux.sc<<2)+3);
}

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

    int k,x,y;
    scanf("%d%d%d",&k,&x,&y);
    printf("%d\n",rezolva(k,y,x).fs);

    return 0;
}