Cod sursa(job #53949)

Utilizator gurneySachelarie Bogdan gurney Data 23 aprilie 2007 20:18:10
Problema Fractal Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.22 kb
program fractal;
  const
    fin='fractal.in';
    fout='fractal.out';
    nmax=15;
var
  x,y,n,i,j,k:int64;
  count:int64;

function cadran(k,x,y:int64):int64;
  var
    xx:int64;
  begin
    xx:=1 shl (k-1);
    if (x<=xx) and (y<=xx) then
      cadran:=1
    else if (x<=xx)and(y>xx) then
      cadran:=2
    else if (x>xx)and(y>xx) then
      cadran:=3
    else
      cadran:=4;
  end;

procedure cauta(c,k,x,y:int64);
  var
    xx,cad,aux:int64;
  begin
    if k<>0 then
      begin
        xx:=1 shl (k-1);
        if x>xx then
          x:=x-xx;
        if y>xx then
          y:=y-xx;
        if c=1 then
          begin
            aux:=x;
            x:=y;
            y:=aux;
          end
        else if c=4 then
          begin
            aux:=x;
            x:=xx-y+1;
            y:=xx-aux+1;
          end;
        if k<>1 then
          count:=count+xx*xx*(c-1)
        else
          count:=count+xx*(c-1);
        cauta(cadran(k-1,x,y),k-1,x,y);
      end;
  end;

begin
  assign(input,fin);
    reset(input);
    readln(k,x,y);
  close(input);
  assign(output,fout);
    rewrite(output);
    cauta(cadran(k,x,y),k,x,y);
    writeln(count);
  close(output);
end.