Cod sursa(job #152426)

Utilizator belgun_adrianBelgun Dimitri Adrian belgun_adrian Data 9 martie 2008 14:17:59
Problema Ridicare la putere in timp logaritmic Scor 10
Compilator fpc Status done
Runda Arhiva educationala Marime 0.82 kb
var n,p:longword;

const mare=1999999973;

procedure citire;
        var f:text;
        begin
        assign(f,'lgput.in');
         reset(f);
         readln(f,n,p);
         close(f);
        end;

procedure scrie(k:longword);
        var f:text;
        begin
        assign(f,'lgput.out');
         rewrite(f);
         writeln(f,k);
         close(f);
        end;

function put(x,k,n:longword):longword;
        var a,b:longword;
        begin
        if k=0 then
           put:=1
        else
           if k mod 2 =0 then begin
              a:=put(x, k div 2, n);
              put:=a*a mod n
           end else begin
              a:=put(x, k div 2, n);
              b:=(a*a) mod n;
              put:=b*x mod n;
              end;
        end;

begin
citire;
scrie(put(n,p,mare));
end.