Cod sursa(job #1819127)

Utilizator nhatnam1711999nguyennhatnam nhatnam1711999 Data 30 noiembrie 2016 10:59:54
Problema Reguli Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.91 kb
{$coperators on}
uses math;
const
    fi='reguli.in';
    fo='reguli.out';
    maxN=500001;
var n:longint;
    a,b:array[1..maxN] of int64;
    z:array[0..maxN] of longint;
procedure doc;
var i:longint;
begin
    readln(n);
    readln(a[1]);
    for i:=2 to n do
    begin
        readln(a[i]);
        b[i-1]:=a[i]-a[i-1];
    end;
end;
function check(l:longint):boolean;
var i:longint;
begin
    for i:=1 to n-l-1 do
        if b[i]<>b[i+l] then exit(false);
    exit(true);
end;
function checksame:boolean;
var i:longint;
begin
    for i:=1 to n-2 do
        if b[i]<>b[i+1] then exit(false);
    exit(true);
end;
procedure sub1;
var l,i:longint;
begin
    if n>6000 then
    begin
        writeln(n-1);
        for i:=1 to n-1 do writeln(b[i]);
        exit;
    end;
    if checksame then begin writeln(1); writeln(b[1]); exit; end;
    for l:=1 to n-1 do if check(l) then break;
    writeln(l);
    for i:=1 to l do writeln(b[i]);
end;
procedure xuli;
var res,i,j,l,r: longint;
begin
    l:=1; r:=1;
    z[1]:=n-1;
    for i:=2 to n-1 do
        if i>r then
        begin
            l:=i; r:=i;
            while (r<n) and (b[r-l+1]=b[r]) do inc(r);
            z[i]:=r-l;
            dec(r);
        end else
        begin
            j:=i-l+1;
            If z[j]<r-i+1 then z[i]:=z[j] else
            begin
                l:=i;
                while (r<n) and (b[r-l+1]=b[r]) do inc(r);
                z[i]:=r-l;
                dec(r);
            end;
        end;
    res:=0;
    for i:=1 to n-1 do
        if z[i+1]=n-1-i then
        begin
            res:=i;
            break;
        end;
        writeln(res);
        for i:=1 to res do writeln(b[i]);

end;
BEGIN
    assign(input,fi); reset(input);
    assign(output,fo); rewrite(output);
    doc;
    //if n<=5000 then sub1
    //else xuli;
    xuli;
    close(input);
    close(output);
END.