Cod sursa(job #467089)

Utilizator MihaiBunBunget Mihai MihaiBun Data 28 iunie 2010 11:26:22
Problema Pod Scor 15
Compilator fpc Status done
Runda Stelele Informaticii 2010, clasele X-XII, Ziua 2 Marime 2.82 kb
program pod;
var f:text;
    s:array[1..1001] of longint;
    a:array[1..50] of longint;
    n,m,k,i,b,c,j,x,g:longint;
    e:boolean;

procedure sort(l,r:longint);
var u,v,y,z:longint;
begin
  u:=l;
  v:=r;
  y:=s[(l+r) div 2];
  repeat
    while s[u]<y do u:=u+1;
    while y<s[v] do v:=v-1;
    if u<=v then begin
                   z:=s[u];
                   s[u]:=s[v];
                   s[v]:=z;
                   u:=u+1;
                   v:=v-1;
                 end;
  until u>v;
  if l<v then sort(l,v);
  if u<r then sort(u,r);
end;

function cauta(q:longint):boolean;
var u,v,mij:longint;
    ee:boolean;
begin
  u:=1;
  v:=m;
  ee:=false;
  repeat
    mij:=(u+v)div 2;
    if s[mij]=q then ee:=true;
    if s[mij]<q then u:=mij+1;
    if s[mij]>q then v:=mij-1
  until ee or (u>v);
  cauta:=ee;
end;

begin
  assign(f,'pod.in');
  reset(f);
  readln(f,n,m,k);
  for i:=1 to m do begin
                    read(f,s[i]);
                    if s[i]=1 then c:=1;
                    if s[i]=k then b:=1;
                   end;
  sort(1,m);
  close(f);
  assign(f,'pod.out');
  rewrite(f);
  if (c=1)and(b=1) then write(f,0);
  if (c=1)and(b=0) then begin
                          for i:=1 to k-1 do a[i]:=0;
                          a[k]:=1;
                          for i:=k+1 to n do begin
                                               e:=cauta(i);
                                               if e then x:=0
                                                    else x:=(a[k]+a[1])mod 9901;
                                               for j:=1 to k-1 do a[j]:=a[j+1];
                                               a[k]:=x;
                                             end;
                          write(f,a[k]);
                        end;
  if c=0 then begin
                a[1]:=1;
                i:=1;
                repeat
                  i:=i+1;
                  e:=cauta(i);
                  if e then begin
                             a[i]:=0;
                             g:=1;
                            end
                       else a[i]:=1;
                until (i=k-1)or(g=1);
                for j:=i+1 to k-1 do a[j]:=0;
                e:=cauta(k);
                if e then a[k]:=0
                     else a[k]:=1+a[k-1];
                for i:=k+1 to n do begin
                                               e:=cauta(i);
                                               if e then x:=0
                                                    else x:=(a[k]+a[1])mod 9901;
                                               for j:=1 to k-1 do a[j]:=a[j+1];
                                               a[k]:=x;
                                             end;
                          write(f,a[k]);

              end;
  close(f);
end.