MYSTERY - Số huyền bí

Tác giả: RR

Ngôn ngữ: Pascal

const
  base=20122007;
var
  res,a,i,gh,tmp:longint;

function lt3(k:longint):longint;
    var
      mid:longint;
    begin
      if k=0 then exit(1);
      if k=1 then exit(3);
      mid:=lt3(k shr 1);
      mid:=(int64(mid)*mid) mod base;
      if k and 1=1 then mid:=(mid*3) mod base;
      exit(mid);
    end;

begin
  read(a);
  gh:=trunc(sqrt(a)); res:=1;
  for i:=1 to gh do
    if a mod i=0 then
      begin
        tmp:=lt3(i); dec(tmp); if tmp<0 then tmp:=base-1;
        res:=(int64(res)*tmp) mod base;
        if i*i<>a then
          begin
            tmp:=lt3(a div i); dec(tmp); if tmp<0 then tmp:=base-1;
            res:=(int64(res)*tmp) mod base;
          end;
      end;
  writeln(res);
end.

Download