Cod sursa(job #1544759)

Utilizator Gaci27Horvat Catalin Gaci27 Data 6 decembrie 2015 13:54:12
Problema Ridicare la putere in timp logaritmic Scor 10
Compilator fpc Status done
Runda Arhiva educationala Marime 0.63 kb
var n, p:longint;
    f, g:text;
    
function solve(n, p:longint):longint;
var half, full:longint;
begin
    If p=1
    then
    	solve := n mod 1999999973
    else begin
    	half := solve(n, p div 2) mod 1999999973; {2^50 -> half = 2^25}
        full := (half * half) mod 1999999973;	  {full = half^2 = 2^50}
        
        if p mod 2 = 1 then
        	full := (full * n) mod 1999999973;
        
        solve := full;
    end;    
end;

begin
	assign(f, 'lgput.in');  reset(f);
    assign(g, 'lgput.out'); rewrite(g);
    
    read(f, n, p);
    
    writeln(g, solve(n, p));
    
    close(f);
    close(g);
end.