Cod sursa(job #28641)

Utilizator andrewgPestele cel Mare andrewg Data 8 martie 2007 09:39:15
Problema Kperm Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.89 kb
const maxn = 5000;
      modul = 666013;

var f:text;
    n,k,i,j,p,nr:longint;
    sol:int64;
    fact:array[0..maxn]of int64;

procedure readdata;
begin
   assign(f,'kperm.in');
   reset(f);
   readln(f,n,k);
   close(f);
end;

procedure solve;
begin
   sol:=1;
   fact[0]:=1;
   for i:=1 to n do
   begin
      fact[i]:=(fact[i-1]*i) mod modul;
   end;
   j:=n div k;
   nr:=0;
   p:=j*k;
   for i:=p+1 to p+k do
   begin
      if i<=n then
      begin
         sol:=(sol*fact[j+1]) mod modul;
         inc(nr);
      end
         else sol:=(sol*fact[j]) mod modul;
   end;
   sol:=(sol*fact[nr]) mod modul;
   sol:=(sol*fact[k-nr]) mod modul;
end;

procedure writedata;
begin
   assign(f,'kperm.out');
   rewrite(f);
   writeln(f,sol);
   close(f);
end;

begin
   readdata;
   if k mod 2=1 then solve
                else sol:=0;
   writedata;
end.