Cod sursa(job #252197)

Utilizator MihaiBunBunget Mihai MihaiBun Data 3 februarie 2009 23:34:24
Problema Lista lui Andrei Scor 35
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.2 kb
program lista;
var f:text;
    as,ev:boolean;
    st:array[1..1000] of 0..26;
    mat:array[1..26,1..26] of 0..1;
    n,m,i,j:integer;
    nr:longint;
    a,b,c:char;
begin
  assign(f,'nrcuv.in');
  reset(f);
  readln(f,n,m);
  for i:=1 to m do
    begin
       read(f,a);read(f,c);readln(f,b);
       if mat[ord(a)-96,ord(b)-96]=0 then
       begin
       mat[ord(a)-96,ord(b)-96]:=1;
       mat[ord(b)-96,ord(a)-96]:=1;
       end;
    end;
  close(f);
  assign(f,'nrcuv.out');
  rewrite(f);
  nr:=0;
  j:=1;
  st[j]:=0;
  while j>0 do
  begin
    repeat
      if st[j]<26 then begin
                           st[j]:=st[j]+1;
                           as:=true
                        end
                   else as:=false;
      if as then if j>1 then if mat[st[j],st[j-1]]=1 then ev:=false
                                                     else ev:=true
                        else ev:=true
    until (not as) or (as and ev);
    if as then if j=n then nr:=(nr+1)mod 104659
                      else begin
                            j:=j+1;
                            st[j]:=0
                           end
          else j:=j-1
  end;
 writeln(f,nr);
 close(f);
end.