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

Common Board

I tested my program MANY TIMES BUT GET WA!!! Please, HELP Me. Aidar. ABOUT 1133
Posted by Aidar 23 Feb 2002 22:24
program Fibonacci_sequence;

var
  i, Fi, j, Fj, n, t, x, y : longint;
  P, Q : array [-1..5000] of longint;

procedure Answer (What : longint);

begin
  Write (What);
  {$IFDEF TPDEBUG}WriteLn;{$ENDIF}
  Halt;
end;

begin
  Read (i, Fi, j, Fj, n);
  if i > j then begin
    t := i; i := j; j := t;
    t := Fi; Fi := Fj; Fj := t;
  end;
  Q [0] := Fi; Q [1] := 0;
  P [0] := 0; P [1] := 1;
  t := j - i;
  for x := 2 to t do begin
    P [x] := P [x - 1] + P [x - 2];
    Q [x] := Q [x - 1] + Q [x - 2];
  end;
  y := (Fj - Q [t]) div P [t];
  if (n >= i) and (n <= j) then Answer (Q [n - i] + P [n - i] * y);
  if (n < i) then begin
    P [-1] := P [1]; Q [-1] := Q [1];
    for x := 1 to i - n do begin
      P [x] := P [x - 2] - P [x - 1];
      Q [x] := Q [x - 2] - Q [x - 1];
    end;
    Answer (Q [i - n] + P [i - n] * y);
  end;
  if (n > j) then begin
    P [-1] := P [j - i - 1]; P [0] := P [j - i];
    Q [-1] := Q [j - i - 1]; Q [0] := Q [j - i];
    for x := 1 to n - j do begin
      P [x] := P [x - 1] + P [x - 2];
      Q [x] := Q [x - 1] + Q [x - 2];
    end;
    Answer (Q [n - j] + P [n - j] * y);
  end;
end.