Cod sursa(job #1178961)

Utilizator gabrielinelusGabriel-Robert Inelus gabrielinelus Data 27 aprilie 2014 16:41:19
Problema Fractal Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <cstdio>
#include <algorithm>
using namespace std;

int divide (int x,int y ,int K)
{
    if(K == 1){
        if(x == 1 && y == 1) return 0;
        if(x == 1 && y == 2) return 3;
        if(x == 2 && y == 1) return 1;
        return 2;
    }
    K--;
    if(x <= (1 << K) && y <= ( 1 << K )){
        swap(x,y);
        return divide(x,y,K);
    }
    if(y <= (1 << K)){
        x -= 1 << K;
        return ( 1 << (2*K) ) + divide(x,y,K);
    }
    if(x <= (1 << K)){
        y -= 1 << K;
        swap(x,y);
        x = (1 << K) - x + 1;
        y = (1 << K) - y + 1;
        return 3 * (1 << (2*K) ) + divide(x,y,K);
    }
    x -= 1 << K;
    y -= 1 << K;
    return 2*(1 << (2*K)) + divide(x,y,K);
}

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

    int K,X,Y;
    scanf("%d%d%d",&K,&Y,&X);
    printf("%d\n",divide(X,Y,K));

    return 0;
}