Cod sursa(job #465751)

Utilizator tibi9876Marin Tiberiu tibi9876 Data 25 iunie 2010 12:52:45
Problema Ratphu Scor 30
Compilator fpc Status done
Runda Stelele Informaticii 2010, gimnaziu si clasa a IX-a, Ziua 1 Marime 0.76 kb
var x,a:array[1..18] of longint;
    k,i,n,p,q,cod,t:longint;
    c:char;

procedure testsol;
var i,t:integer;
begin
t:=0;
for i:=1 to n do
t:=(t*10+a[x[i]]) mod p;
if t=0 then inc(q);
end;

function test(k:integer):boolean;
var i:integer;
begin
test:=true;
for i:=1 to k-1 do
if x[i]=x[k] then
begin
test:=false;
break;
end;
end;

procedure back(k:integer);
var i:integer;
begin
for i:=1 to n do
begin
x[k]:=i;
if test(k) then
begin
if k=n then testsol
else back(k+1);
end;
end;
end;

begin
assign(input,'ratphu.in');reset(input);
assign(output,'ratphu.out');rewrite(output);
n:=0;
while not eoln(input) do
begin
inc(n);
read(c);
if c=' ' then break;
val(c,a[n],cod);
end;
dec(n);
read(p);
q:=0;
back(1);
write(q);
end.