新型高精,5*TLE+50,求优化!

P1045 [NOIP2003 普及组] 麦森数

1517460958dyc @ 2016-08-05 12:11:21

program mersenne;
var
  i,j,n,s,s1:longint;
  a:array[1..10000]of qword;
  t:string;
function nl(x:qword):integer;
var
 tx:string;
begin
  str(x,tx);
  nl:=length(tx);
end;
begin
  readln(n);
  fillchar(a,sizeof(a),0);
  a[1]:=1;
  s:=1;
  for i:=1 to n do
  begin
    for j:=s downto 1 do
    begin
      a[j]:=2*a[j];
      if a[j]>=10000000000 then 
      begin
        a[j]:=a[j]-10000000000;
        if j=s then inc(s);
        a[j+1]:=a[j+1]+1;
      end;
    end;
  end;
  a[1]:=a[1]-1;
  str(a[s],t);
  s1:=10*(s-1)+length(t);
  write(s1); 
  if s1<=500 then
  begin
    for i:=500 downto s1+1 do
    begin
      if (i mod 50)=0 then writeln;
      write(0);
    end;
    for i:=1 to length(t) do
    begin
      write(t[i]);
      if ((s1-i+1) mod 50)=1 then writeln;
    end;
    for i:=s-1 downto 1 do
    begin
      if (i mod 5)=0 then writeln;
      for j:=10 downto nl(a[i])+1 do write('0');
      write(a[i]);
    end;
  end
  else
    for i:=50 downto 1 do
    begin
      if (i mod 5)=0 then writeln;
      for j:=10 downto nl(a[i])+1 do write('0');
      write(a[i]);
    end;
end.

by kkksc03 @ 2016-08-05 12:26:04

快速幂


by MTer @ 2017-02-17 22:55:35

先一次性成2的59次;


by Crab_Tang @ 2023-08-20 13:24:33

楼上的楼上惊现kkksc03.


|