Cod sursa(job #500038)

Utilizator crushackPopescu Silviu crushack Data 11 noiembrie 2010 11:26:08
Problema Fractal Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <stdio.h>
#include <fstream>
using namespace std;

const char IN[]="fractal.in",OUT[]="fractal.out";
//const char IN[]="hilbert.in",OUT[]="hilbert.out";
typedef unsigned long long ull;

int K,x,y;
ull Hi[20];

void InitializeH(ull *H)
{
	int i;
	H[0]=0;
	for (i=1;i<18;i++)
		H[i]=H[i-1]*4+3;
}

ull Hilbert(int n,int x,int y)
{
	if (n<=0 || n>15) return 0;
	if ( x <= ( 1<<(n-1) ) )
	{
		if ( y<= (1<<(n-1) ) )
			return Hilbert(n-1,y,x);
		return 1+Hi[n-1]+Hilbert(n-1,x,y-(1<<(n-1)));
	}
	if ( y<= (1<<(n-1)))
		return 3*(Hi[n-1]+1)+Hilbert(n-1,(1 << n - 1) - y + 1,(1 << n) - x + 1);
	return 2*(Hi[n-1]+1)+Hilbert(n-1,x-(1<<(n-1)),y-(1<<(n-1)));
}

int main()
{
	InitializeH(Hi);
	freopen(IN,"r",stdin);
	scanf("%d%d%d",&K,&x,&y);
	fclose(stdin);
	
	ofstream fout(OUT);
	fout<<Hilbert(K,x,y);
	fout.close();
	
	return 0;
}