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 1064. Binary Search

AC program
Posted by Drizzt 28 Jul 2003 14:20
program Binary (input, output);
  var lN, lK, lI, lMax : longint;
      arr : array [0.. 50] of longint;
      res : array [1.. 50] of byte;
  begin
    arr [0] := 1;
    arr [1] := 2;
    for lI := 1 to 44 do res [lI] := 0;
    readln (lN, lK);
    for lI := 2 to lN do arr [lI] := arr [lI - 1] + arr [lI - 2];
    if lK > arr [lN] then writeln (-1)
                     else begin
                            dec (lK);
                            lMax := lN - 1;
                            while (lK >= 1) and (lMax >= 0) do
                              begin
                                if arr [lMax] <= lK then
                                  begin
                                    res [lMax + 1] := 1;
                                    lK := lK - arr [lMax];
                                  end;
                                dec (lMax);
                              end;
                            for lI := lN downto 1 do write (res [lI]);
                          end;
  end.
Sorry,I put a wrong program!
Posted by Drizzt 28 Jul 2003 14:48
program Search (input, output);
  const
    MaxN = 10000;
  var a : array [0.. MaxN - 1] of longint;
      N, I, L: longint;
      flag : array [1.. MaxN] of boolean;
      Duan, X, Y, Int1 : longint;
      Bln1, IsFound : boolean;
  procedure BinarySearch (X : longint);
    var P, Q, I : longint;
    begin
      P := 0;
      Q := N - 1;
      L := 0;
      while P <= Q do
        begin
          I := (P + Q) div 2;
          inc (L);
          if a [I] = X then
            begin
              exit
            end;
          if X < a [I] then Q := I - 1
                       else P := I + 1
        end;
      IsFound := false;
    end;
  begin
    readln (X, Y);
    for I := 1 to MaxN do
      a [I - 1] := I - 1;
    fillchar (flag, sizeof (flag), false);
    for N := X to MaxN do
      begin
        IsFound := true;
        BinarySearch (X);
        if not IsFound then continue;
        if L = Y then flag [N] := true;
      end;
    Duan := 0;
    Bln1 := false;
    for I := 1 to MaxN do
      begin
        if flag [I] and not Bln1 then
          inc (Duan);
        Bln1 := flag [I];
      end;
    writeln (Duan);
    Int1 := -1;
    Bln1 := false;
    for I := 1 to MaxN do
      begin
        if flag [I] and not Bln1 then
          Int1 := I;
        if Bln1 and not flag [I] then
          begin
            writeln (Int1, ' ', I - 1);
            Int1 := -1;
          end;
        Bln1 := flag [I];
      end;
    if flag [MaxN] then
      writeln (Int1, ' ', MaxN);
  end.