Cod sursa(job #986413)

Utilizator narcis_vsGemene Narcis - Gabriel narcis_vs Data 18 august 2013 18:24:28
Problema Fractal Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <fstream>
#include <cassert>
#define In "fractal.in"
#define Out "fractal.out"

using namespace std;
int nr[20];
ofstream g(Out);
inline long long Div(int k,int x,int y)
{
    if(k==1)
    {
        if(x==1 && y==1)
            return 0LL;
        if(x==1 && y==2)
            return 1LL;
        if(x==2 && y==2)
            return 2LL;
        return 3LL;
    }
    ///prima regiune
    const int m = 1 << (k-1);
    if(x<=m && y<=m)
        return Div(k-1,y,x);
    ///a 2-a regiune
    if(x>m&&y<=m)
        return nr[k-1] + 1 + Div(k-1,x-m,y);
    ///a 3-a regiune
    if(x>m && y>m)
        return 2LL*nr[k-1] + 2LL + Div(k-1,x-m,y-m);
    assert(x<m && y>m);
    y-=m;
    int aux = x;
    x = m-y+1;
    y = aux;
    return 3LL*nr[k-1] + 3LL + 1LL*nr[k-1]-Div(k-1,x,y);
}
int main()
{
    int k, x , y, i;
    ifstream f(In);
    f>>k>>x>>y;
    f.close();
    nr[1] = 3;
    for(i = 2;i <= 15; ++i)
        nr[i] = 4*nr[i-1]+3;
    swap(x,y);
    g<<Div(k,x,y)<<"\n";
    g.close();
    return 0;
}