ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1302. Delta-wave

Wht's my error?
Posted by Agent_TWX 19 Jul 2005 18:27
program t1320;


var
 u,v,t : int64;
 f,maxl,maxr : int64;
 il1,il2,jl1,jl2 : int64;

function GetLevel1 (i:integer):int64;
var t : int64;
begin
 t := (Trunc (sqrt ((i))));
 if sqr (t)<i then Result := t+1
 else Result := t;
end;

function GetLevel2 (i:int64):int64;
var
b : byte;
begin
f := GetLevel1 (i);
b := ((f and 1)*2) + (i and 1);
if (b=0) or (b=3) then Result := 2*F-1
else Result := 2*(F-1);
end;

function GetMaxOnLev (Level : int64):int64;
begin
Result := sqr (Level);
end;

function GetMinOnLev (Level : int64) : int64;
begin
Result := sqr (Level-1)+1;
end;

function ComputePath : int64;
begin
il1 := GetLevel1 (u); il2 := GetLevel2 (u);
jl1 := GetLevel1 (v); jl2 := GetLevel2 (v);
if (u=v) then begin REsult :=0; exit; end;
if (u=1) then begin Result := jl2-il2; exit; end;
if (il2=jl2) then begin Result := v-u; exit; end;
 if (il2 and 1)=1 then
 begin
  maxl := GetMinOnLev (jl1)-GetMinOnLev(il1)+u;
  maxr := GetMaxOnLev (jl1)-GetMaxOnLev(il1)+u;
 end
else
 begin
  maxl := GetMinOnLev (jl1)+(u-1)-GetMinOnLev(il1);
  maxr := GetMaxOnLev (jl1)-GetMaxOnLev(il1)+u+1;
 end;
if v < maxl then Result := jl2-il2+(maxl-v) else
if v > maxr then Result := jl2-il2-(v-maxr) else
Result := jl2-il2;
end;

begin
Read (u,v);
if (u>v) then begin t:=u; u:=v; u:=t; end;
WriteLn (ComputePath);
end.

Edited by author 19.07.2005 18:28